Skip to content

Commit

Permalink
Added a dedicated error for a situation when toHaveStyleRule receiv…
Browse files Browse the repository at this point in the history
…es an array (#2749)
  • Loading branch information
Andarist committed Jun 7, 2022
1 parent 62dd16b commit fd22cac
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/silly-hornets-chew.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@emotion/jest': patch
---

Added a dedicated error for a situation when `toHaveStyleRule` receives an array.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"flow": "flow",
"flow:check": "flow check --flowconfig-name=.flowconfig-ci",
"preinstall": "node ./scripts/ensure-yarn.js",
"postinstall": "opencollective postinstall && preconstruct dev && manypkg check",
"postinstall": "patch-package && opencollective postinstall && preconstruct dev && manypkg check",
"changeset": "changeset",
"version-packages": "changeset version",
"release": "yarn build && changeset publish"
Expand Down Expand Up @@ -237,6 +237,7 @@
"normalize.css": "^8.0.0",
"npm-run-all": "^4.0.2",
"opencollective": "^1.0.3",
"patch-package": "^6.4.7",
"polished": "^1.2.1",
"prettier": "^2.3.0",
"raf": "^3.4.0",
Expand Down
5 changes: 5 additions & 0 deletions packages/jest/src/matchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ function toHaveStyleRule(
value: *,
options?: { target?: string | RegExp, media?: string } = {}
) {
if (Array.isArray(received)) {
throw new Error(
'`toHaveStyleRule` expects to receive a single element but it received an array.'
)
}
const { target, media } = options
const classNames = getClassNamesFromNodes([received])
const cssString = getStylesFromClassNames(classNames, getStyleElements())
Expand Down
21 changes: 21 additions & 0 deletions packages/jest/test/matchers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,27 @@ describe('toHaveStyleRule', () => {

expect(tree.children[0]).toHaveStyleRule('color', 'hotpink')
})

it('should throw a friendly error when it receives an array', () => {
const tree = renderer
.create(
<>
<div
css={css`
color: hotpink;
`}
/>
{'Some text'}
</>
)
.toJSON()

expect(() =>
expect(tree).toHaveStyleRule('color', 'hotpink')
).toThrowErrorMatchingInlineSnapshot(
`"\`toHaveStyleRule\` expects to receive a single element but it received an array."`
)
})
;(isReact16 ? describe : describe.skip)('enzyme', () => {
it('supports enzyme `mount` method', () => {
const Component = () => (
Expand Down
14 changes: 14 additions & 0 deletions patches/jest-cli++jest-snapshot+27.4.5.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
diff --git a/node_modules/jest-cli/node_modules/jest-snapshot/build/InlineSnapshots.js b/node_modules/jest-cli/node_modules/jest-snapshot/build/InlineSnapshots.js
index 5f9c3d5..1ec2ce5 100644
--- a/node_modules/jest-cli/node_modules/jest-snapshot/build/InlineSnapshots.js
+++ b/node_modules/jest-cli/node_modules/jest-snapshot/build/InlineSnapshots.js
@@ -219,7 +219,8 @@ const saveSnapshotsForFile = (snapshots, sourceFilePath, prettier) => {
filename: sourceFilePath,
plugins,
presets,
- root: path.dirname(sourceFilePath)
+ // Jest 27-28 currently can't locate our `babel.config.js` correctly, see: https://github.com/facebook/jest/issues/11741
+ // root: path.dirname(sourceFilePath)
});

if (!ast) {
40 changes: 39 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6642,6 +6642,11 @@
resolved "https://registry.npmjs.org/@xtuc/long/-/long-4.2.2.tgz#d291c6a4e97989b5c61d9acf396ae4fe133a718d"
integrity sha512-NuHqBY1PB/D8xU6s/thBgOAiAP7HOYDQ32+BFZILJ8ivkUkAHQnWfn6WhL79Owj1qmUnoN/YPhktdIoucipkAQ==

"@yarnpkg/lockfile@^1.1.0":
version "1.1.0"
resolved "https://registry.yarnpkg.com/@yarnpkg/lockfile/-/lockfile-1.1.0.tgz#e77a97fbd345b76d83245edcd17d393b1b41fb31"
integrity sha512-GpSwvyXOcOOlV70vbnzjj4fW5xW/FdUF6nQEt1ENy7m4ZCczi1+/buVUPAqmGfqznsORNFzUMjctTIp8a9tuCQ==

JSONStream@^1.3.4, JSONStream@^1.3.5:
version "1.3.5"
resolved "https://registry.npmjs.org/JSONStream/-/JSONStream-1.3.5.tgz#3208c1f08d3a4d99261ab64f92302bc15e111ca0"
Expand Down Expand Up @@ -13813,6 +13818,13 @@ find-yarn-workspace-root2@1.2.16:
micromatch "^4.0.2"
pkg-dir "^4.2.0"

find-yarn-workspace-root@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/find-yarn-workspace-root/-/find-yarn-workspace-root-2.0.0.tgz#f47fb8d239c900eb78179aa81b66673eac88f7bd"
integrity sha512-1IMnbjt4KzsQfnhnzNd8wUEgXZ44IzZaZmnLYx7D5FZlaHt2gW20Cri8Q+E/t5tIj4+epTBub+2Zxu/vNILzqQ==
dependencies:
micromatch "^4.0.2"

flat-cache@^2.0.1:
version "2.0.1"
resolved "https://registry.npmjs.org/flat-cache/-/flat-cache-2.0.1.tgz#5d296d6f04bda44a4630a301413bdbc2ec085ec0"
Expand Down Expand Up @@ -19299,6 +19311,13 @@ kind-of@^6.0.0, kind-of@^6.0.2, kind-of@^6.0.3:
resolved "https://registry.yarnpkg.com/kind-of/-/kind-of-6.0.3.tgz#07c05034a6c349fa06e24fa35aa76db4580ce4dd"
integrity sha512-dcS1ul+9tmeD95T+x28/ehLgd9mENa3LsvDTtzm3vyBEO7RPptvAD+t44WVXaUjTBRcrpFeFlC8WCruUR456hw==

klaw-sync@^6.0.0:
version "6.0.0"
resolved "https://registry.yarnpkg.com/klaw-sync/-/klaw-sync-6.0.0.tgz#1fd2cfd56ebb6250181114f0a581167099c2b28c"
integrity sha512-nIeuVSzdCCs6TDPTqI8w1Yre34sSq7AkZ4B3sfOBbI2CgVSB4Du4aLQijFU2+lhAFCwt9+42Hel6lQNIv6AntQ==
dependencies:
graceful-fs "^4.1.11"

klaw@^1.0.0:
version "1.3.1"
resolved "https://registry.npmjs.org/klaw/-/klaw-1.3.1.tgz#4088433b46b3b1ba259d78785d8e96f73ba02439"
Expand Down Expand Up @@ -22600,7 +22619,7 @@ open@^6.2.0:
dependencies:
is-wsl "^1.1.0"

open@^7.0.2, open@^7.0.3:
open@^7.0.2, open@^7.0.3, open@^7.4.2:
version "7.4.2"
resolved "https://registry.yarnpkg.com/open/-/open-7.4.2.tgz#b8147e26dcf3e426316c730089fd71edd29c2321"
integrity sha512-MVHddDVweXZF3awtlAS+6pgKLlm/JgxZ90+/NBurBoQctVOOB/zDdVjcyPzQ+0laDGbsWgrRkflI65sQeOgT9Q==
Expand Down Expand Up @@ -23370,6 +23389,25 @@ password-prompt@^1.0.4:
ansi-escapes "^3.1.0"
cross-spawn "^6.0.5"

patch-package@^6.4.7:
version "6.4.7"
resolved "https://registry.yarnpkg.com/patch-package/-/patch-package-6.4.7.tgz#2282d53c397909a0d9ef92dae3fdeb558382b148"
integrity sha512-S0vh/ZEafZ17hbhgqdnpunKDfzHQibQizx9g8yEf5dcVk3KOflOfdufRXQX8CSEkyOQwuM/bNz1GwKvFj54kaQ==
dependencies:
"@yarnpkg/lockfile" "^1.1.0"
chalk "^2.4.2"
cross-spawn "^6.0.5"
find-yarn-workspace-root "^2.0.0"
fs-extra "^7.0.1"
is-ci "^2.0.0"
klaw-sync "^6.0.0"
minimist "^1.2.0"
open "^7.4.2"
rimraf "^2.6.3"
semver "^5.6.0"
slash "^2.0.0"
tmp "^0.0.33"

path-browserify@0.0.1:
version "0.0.1"
resolved "https://registry.npmjs.org/path-browserify/-/path-browserify-0.0.1.tgz#e6c4ddd7ed3aa27c68a20cc4e50e1a4ee83bbc4a"
Expand Down

0 comments on commit fd22cac

Please sign in to comment.