Skip to content

Commit

Permalink
fix: support multiple snapshots per test (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
dword-design authored Mar 13, 2021
1 parent 8b92558 commit 7ab91a8
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 72 deletions.
10 changes: 6 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ export const configureToMatchImageSnapshot = common => (
'Did you forget to pass `this` into expect().toMatchImageSnapshot(this)?'
)
}
const snapshotState = new jestSnapshot.SnapshotState(undefined, {
updateSnapshot: process.env.SNAPSHOT_UPDATE ? 'all' : 'new',
})
if (!context.snapshotState) {
context.snapshotState = new jestSnapshot.SnapshotState(undefined, {
updateSnapshot: process.env.SNAPSHOT_UPDATE ? 'all' : 'new',
})
}
const matcher = jestToMatchImageSnapshot.bind({
currentTestName: makeTestTitle(context.test),
snapshotState,
snapshotState: context.snapshotState,
testPath: context.test.file,
})
const result = matcher(received, { ...common, ...options })
Expand Down
90 changes: 81 additions & 9 deletions src/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default {
})
`
)
await execa.command('mocha index.spec.js')
await execa.command('mocha --timeout 5000 index.spec.js')
const snapshot = await readFile(
P.join('__foo_image_snapshots__', 'index-spec-js-works-1-snap.png')
)
Expand Down Expand Up @@ -93,7 +93,7 @@ export default {
.toBuffer()
)
await expect(
execa.command('mocha index.spec.js', { all: true })
execa.command('mocha --timeout 5000 index.spec.js', { all: true })
).rejects.toThrow(
'Expected image to match or be a close match to snapshot but was 100% different from snapshot (2304 differing pixels).'
)
Expand Down Expand Up @@ -137,7 +137,78 @@ export default {
.png()
.toBuffer()
)
await execa.command('mocha index.spec.js')
await execa.command('mocha --timeout 5000 index.spec.js')
}),
'multiple snapshots per test': () =>
withLocalTmpDir(async () => {
await outputFile(
'index.spec.js',
endent`
const sharp = require('${packageName`sharp`}')
const expect = require('expect')
const { toMatchImageSnapshot: self } = require('../src')
expect.extend({ toMatchImageSnapshot: self })
it('works', async function () {
const img1 = await sharp({
create: {
background: { b: 0, g: 255, r: 0 },
channels: 3,
height: 48,
width: 48,
},
})
.png()
.toBuffer()
const img2 = await sharp({
create: {
background: { b: 255, g: 0, r: 0 },
channels: 3,
height: 48,
width: 48,
},
})
.png()
.toBuffer()
expect(img1).toMatchImageSnapshot(this)
expect(img2).toMatchImageSnapshot(this)
})
`
)
await execa.command('mocha --timeout 5000 index.spec.js')
expect(
await readFile(
P.join('__image_snapshots__', 'index-spec-js-works-1-snap.png')
)
).toMatchImage(
await sharp({
create: {
background: { b: 0, g: 255, r: 0 },
channels: 3,
height: 48,
width: 48,
},
})
.png()
.toBuffer()
)
expect(
await readFile(
P.join('__image_snapshots__', 'index-spec-js-works-2-snap.png')
)
).toMatchImage(
await sharp({
create: {
background: { b: 255, g: 0, r: 0 },
channels: 3,
height: 48,
width: 48,
},
})
.png()
.toBuffer()
)
}),
'no existing snapshots': () =>
withLocalTmpDir(async () => {
Expand Down Expand Up @@ -165,11 +236,12 @@ export default {
})
`
)
await execa.command('mocha index.spec.js')
const snapshot = await readFile(
P.join('__image_snapshots__', 'index-spec-js-works-1-snap.png')
)
expect(snapshot).toMatchImage(
await execa.command('mocha --timeout 5000 index.spec.js')
expect(
await readFile(
P.join('__image_snapshots__', 'index-spec-js-works-1-snap.png')
)
).toMatchImage(
await sharp({
create: {
background: { b: 0, g: 255, r: 0 },
Expand Down Expand Up @@ -221,7 +293,7 @@ export default {
.png()
.toBuffer()
)
await execa.command('mocha index.spec.js', {
await execa.command('mocha --timeout 5000 index.spec.js', {
env: { SNAPSHOT_UPDATE: true },
})
const snapshot = await readFile(
Expand Down
71 changes: 12 additions & 59 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1069,11 +1069,11 @@
"@babel/plugin-proposal-pipeline-operator" "^7.5.0"
"@babel/preset-env" "^7.6.3"
"@dword-design/functions" "^2.2.2"
"@vue/babel-preset-jsx" "github:dword-design/jsx#fork-babel-preset-jsx"
"@vue/babel-preset-jsx" dword-design/jsx#fork-babel-preset-jsx
babel-plugin-add-module-exports "^1.0.2"
babel-plugin-module-resolver "^4.0.0"
babel-plugin-transform-imports "^2.0.0"
babel-plugin-wildcard "github:dword-design/babel-plugin-wildcard#fork"
babel-plugin-wildcard dword-design/babel-plugin-wildcard#fork
depcheck-package-name "^1.0.0"
find-up "^5.0.0"
param-case "^3.0.3"
Expand Down Expand Up @@ -1115,7 +1115,7 @@
commitizen "^4.0.3"
constant-case "^3.0.3"
cz-conventional-changelog "^3.1.0"
depcheck "github:dword-design/depcheck#fork"
depcheck dword-design/depcheck#fork
depcheck-detector-execa "^1.2.1"
depcheck-detector-package-name "^1.0.0"
depcheck-package-name "^1.0.0"
Expand Down Expand Up @@ -1178,7 +1178,7 @@
depcheck-package-name "^1.0.0"
eslint-config-airbnb-base "^14.2.1"
eslint-config-prettier "^7.0.0"
eslint-import-resolver-babel-module "github:dword-design/eslint-import-resolver-babel-module#fork"
eslint-import-resolver-babel-module dword-design/eslint-import-resolver-babel-module#fork
eslint-plugin-import "^2.22.1"
eslint-plugin-json-format "^2.0.1"
eslint-plugin-prefer-arrow "^1.1.6"
Expand Down Expand Up @@ -1206,7 +1206,7 @@
resolved "https://registry.yarnpkg.com/@dword-design/functions/-/functions-1.2.0.tgz#fa4ed4643a4f801f0f984d09bcef32c3b71afffb"
integrity sha512-TE6zdexGjlj6t5CaVtfh28rTSdQOOxhmRzkj0++cc7G4xe3dJ3xVvT9k6O0ECrPnHOMypkoCcXOo5SNFmBUQRw==
dependencies:
endent "github:dword-design/endent#fork"
endent dword-design/endent#fork
lodash "^4.17.15"
tinycolor2 "^1.4.1"

Expand Down Expand Up @@ -1635,9 +1635,8 @@
lodash.kebabcase "^4.1.1"
svg-tags "^1.0.0"

"@vue/babel-preset-jsx@github:dword-design/jsx#fork-babel-preset-jsx":
"@vue/babel-preset-jsx@dword-design/jsx#fork-babel-preset-jsx":
version "1.1.2"
uid a73cb9961541a60a66b1b99a69a32e00bc66d336
resolved "https://codeload.github.com/dword-design/jsx/tar.gz/a73cb9961541a60a66b1b99a69a32e00bc66d336"
dependencies:
"@vue/babel-helper-vue-jsx-merge-props" "^1.0.0"
Expand All @@ -1656,7 +1655,6 @@

"@vue/babel-sugar-inject-h@dword-design/jsx#fork-babel-sugar-inject-h":
version "1.1.2"
uid cc36a2200c059120f37944615fb2ec0e22f8f3cc
resolved "https://codeload.github.com/dword-design/jsx/tar.gz/cc36a2200c059120f37944615fb2ec0e22f8f3cc"
dependencies:
"@babel/plugin-syntax-jsx" "^7.2.0"
Expand Down Expand Up @@ -2101,9 +2099,8 @@ babel-plugin-transform-imports@^2.0.0:
"@babel/types" "^7.4"
is-valid-path "^0.1.1"

"babel-plugin-wildcard@github:dword-design/babel-plugin-wildcard#fork":
babel-plugin-wildcard@dword-design/babel-plugin-wildcard#fork:
version "6.0.0"
uid "691d11923ee2dcf63e86f205b0a2c998883e3a3c"
resolved "https://codeload.github.com/dword-design/babel-plugin-wildcard/tar.gz/691d11923ee2dcf63e86f205b0a2c998883e3a3c"
dependencies:
rimraf "^2.6.2"
Expand Down Expand Up @@ -3051,7 +3048,7 @@ debug@^3.1.0:
dependencies:
ms "^2.1.1"

debuglog@*, debuglog@^1.0.1:
debuglog@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/debuglog/-/debuglog-1.0.1.tgz#aa24ffb9ac3df9a2351837cfb2d279360cd78492"
integrity sha1-qiT/uaw9+aI1GDfPstJ5NgzXhJI=
Expand Down Expand Up @@ -3100,7 +3097,6 @@ [email protected]:

dedent@dword-design/dedent#fork:
version "0.7.0"
uid c8d46e5017d60504657c2d01b59082c0329a12a7
resolved "https://codeload.github.com/dword-design/dedent/tar.gz/c8d46e5017d60504657c2d01b59082c0329a12a7"
dependencies:
babel-plugin-add-module-exports "^1.0.2"
Expand Down Expand Up @@ -3220,9 +3216,8 @@ depcheck-parser-babel@^1.2.0:
"@babel/core" "^7.7.7"
fs-extra "^9.0.0"

"depcheck@github:dword-design/depcheck#fork":
depcheck@dword-design/depcheck#fork:
version "0.0.1"
uid "8a9eb41006c2c6f9cbd79e4e68bf265e1c1df1dc"
resolved "https://codeload.github.com/dword-design/depcheck/tar.gz/8a9eb41006c2c6f9cbd79e4e68bf265e1c1df1dc"
dependencies:
"@babel/parser" "^7.8.4"
Expand Down Expand Up @@ -3434,9 +3429,8 @@ end-of-stream@^1.0.0, end-of-stream@^1.1.0, end-of-stream@^1.4.1:
dependencies:
once "^1.4.0"

endent@dword-design/endent#fork, "endent@github:dword-design/endent#fork":
endent@dword-design/endent#fork:
version "1.4.0"
uid be5433db67eaec07c6c9d00ba3bed2ed946dc664
resolved "https://codeload.github.com/dword-design/endent/tar.gz/be5433db67eaec07c6c9d00ba3bed2ed946dc664"
dependencies:
dedent dword-design/dedent#fork
Expand Down Expand Up @@ -3576,9 +3570,8 @@ eslint-config-prettier@^7.0.0:
resolved "https://registry.yarnpkg.com/eslint-config-prettier/-/eslint-config-prettier-7.2.0.tgz#f4a4bd2832e810e8cc7c1411ec85b3e85c0c53f9"
integrity sha512-rV4Qu0C3nfJKPOAhFujFxB7RMP+URFyQqqOZW9DMRD7ZDTFyjaIlETU3xzHELt++4ugC0+Jm084HQYkkJe+Ivg==

"eslint-import-resolver-babel-module@github:dword-design/eslint-import-resolver-babel-module#fork":
eslint-import-resolver-babel-module@dword-design/eslint-import-resolver-babel-module#fork:
version "5.1.2"
uid "97550c5b7626a9cf582ea2392b9749702a27473d"
resolved "https://codeload.github.com/dword-design/eslint-import-resolver-babel-module/tar.gz/97550c5b7626a9cf582ea2392b9749702a27473d"
dependencies:
pkg-up "^2.0.0"
Expand Down Expand Up @@ -4966,7 +4959,7 @@ import-lazy@^2.1.0:
resolved "https://registry.yarnpkg.com/import-lazy/-/import-lazy-2.1.0.tgz#05698e3d45c88e8d7e9d92cb0584e77f096f3e43"
integrity sha1-BWmOPUXIjo1+nZLLBYTnfwlvPkM=

imurmurhash@*, imurmurhash@^0.1.4:
imurmurhash@^0.1.4:
version "0.1.4"
resolved "https://registry.yarnpkg.com/imurmurhash/-/imurmurhash-0.1.4.tgz#9218b9b2b928a238b13dc4fb6b6d576f231453ea"
integrity sha1-khi5srkoojixPcT7a21XbyMUU+o=
Expand Down Expand Up @@ -6167,11 +6160,6 @@ lockfile@^1.0.4:
dependencies:
signal-exit "^3.0.2"

lodash._baseindexof@*:
version "3.1.0"
resolved "https://registry.yarnpkg.com/lodash._baseindexof/-/lodash._baseindexof-3.1.0.tgz#fe52b53a1c6761e42618d654e4a25789ed61822c"
integrity sha1-/lK1OhxnYeQmGNZU5KJXie1hgiw=

lodash._baseuniq@~4.6.0:
version "4.6.0"
resolved "https://registry.yarnpkg.com/lodash._baseuniq/-/lodash._baseuniq-4.6.0.tgz#0ebb44e456814af7905c6212fa2c9b2d51b841e8"
Expand All @@ -6180,33 +6168,11 @@ lodash._baseuniq@~4.6.0:
lodash._createset "~4.0.0"
lodash._root "~3.0.0"

lodash._bindcallback@*:
version "3.0.1"
resolved "https://registry.yarnpkg.com/lodash._bindcallback/-/lodash._bindcallback-3.0.1.tgz#e531c27644cf8b57a99e17ed95b35c748789392e"
integrity sha1-5THCdkTPi1epnhftlbNcdIeJOS4=

lodash._cacheindexof@*:
version "3.0.2"
resolved "https://registry.yarnpkg.com/lodash._cacheindexof/-/lodash._cacheindexof-3.0.2.tgz#3dc69ac82498d2ee5e3ce56091bafd2adc7bde92"
integrity sha1-PcaayCSY0u5ePOVgkbr9Ktx73pI=

lodash._createcache@*:
version "3.1.2"
resolved "https://registry.yarnpkg.com/lodash._createcache/-/lodash._createcache-3.1.2.tgz#56d6a064017625e79ebca6b8018e17440bdcf093"
integrity sha1-VtagZAF2JeeevKa4AY4XRAvc8JM=
dependencies:
lodash._getnative "^3.0.0"

lodash._createset@~4.0.0:
version "4.0.3"
resolved "https://registry.yarnpkg.com/lodash._createset/-/lodash._createset-4.0.3.tgz#0f4659fbb09d75194fa9e2b88a6644d363c9fe26"
integrity sha1-D0ZZ+7CddRlPqeK4imZE02PJ/iY=

lodash._getnative@*, lodash._getnative@^3.0.0:
version "3.9.1"
resolved "https://registry.yarnpkg.com/lodash._getnative/-/lodash._getnative-3.9.1.tgz#570bc7dede46d61cdcde687d65d3eecbaa3aaff5"
integrity sha1-VwvH3t5G1hzc3mh9ZdPuy6o6r/U=

lodash._root@~3.0.0:
version "3.0.1"
resolved "https://registry.yarnpkg.com/lodash._root/-/lodash._root-3.0.1.tgz#fba1c4524c19ee9a5f8136b4609f017cf4ded692"
Expand Down Expand Up @@ -6262,11 +6228,6 @@ lodash.map@^4.5.1:
resolved "https://registry.yarnpkg.com/lodash.map/-/lodash.map-4.6.0.tgz#771ec7839e3473d9c4cde28b19394c3562f4f6d3"
integrity sha1-dx7Hg540c9nEzeKLGTlMNWL09tM=

lodash.restparam@*:
version "3.6.1"
resolved "https://registry.yarnpkg.com/lodash.restparam/-/lodash.restparam-3.6.1.tgz#936a4e309ef330a7645ed4145986c85ae5b20805"
integrity sha1-k2pOMJ7zMKdkXtQUWYbIWuWyCAU=

lodash.toarray@^4.4.0:
version "4.4.0"
resolved "https://registry.yarnpkg.com/lodash.toarray/-/lodash.toarray-4.4.0.tgz#24c4bfcd6b2fba38bfd0594db1179d8e9b656561"
Expand Down Expand Up @@ -7041,7 +7002,6 @@ npm@^6.14.9:
cmd-shim "^3.0.3"
columnify "~1.5.4"
config-chain "^1.1.12"
debuglog "*"
detect-indent "~5.0.0"
detect-newline "^2.1.0"
dezalgo "~1.0.3"
Expand All @@ -7056,7 +7016,6 @@ npm@^6.14.9:
has-unicode "~2.0.1"
hosted-git-info "^2.8.8"
iferr "^1.0.2"
imurmurhash "*"
infer-owner "^1.0.4"
inflight "~1.0.6"
inherits "^2.0.4"
Expand All @@ -7075,14 +7034,8 @@ npm@^6.14.9:
libnpx "^10.2.4"
lock-verify "^2.1.0"
lockfile "^1.0.4"
lodash._baseindexof "*"
lodash._baseuniq "~4.6.0"
lodash._bindcallback "*"
lodash._cacheindexof "*"
lodash._createcache "*"
lodash._getnative "*"
lodash.clonedeep "~4.5.0"
lodash.restparam "*"
lodash.union "~4.6.0"
lodash.uniq "~4.5.0"
lodash.without "~4.4.0"
Expand Down

0 comments on commit 7ab91a8

Please sign in to comment.