igor-dv / jest-specific-snapshot Goto Github PK
View Code? Open in Web Editor NEWJest matcher for multiple snapshot files per test
License: MIT License
Jest matcher for multiple snapshot files per test
License: MIT License
jest.retryTimes(3)
describe('foo', () => {
it('works', () => {
expect(1).toMatchSpecificSnapshot('foo.snap')
throw new Error()
})
})
expected: only 1 snapshot is written
actual: each retry has its own snapshot
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`foo works 1`] = `1`;
exports[`foo works 2`] = `1`;
exports[`foo works 3`] = `1`;
exports[`foo works 4`] = `1`;
related: jestjs/jest#7493
In one test file, I have a few describe()
and a few test.concurrent()
under each describe. In each test.concurrent()
, I have it save to different snapshot files.
For example, for 10 total test, there will be 10 snapshot files.
However, I see that the snapshots are being saved to incorrect files with also incorrect names despite passing in the hint as well (they get mixed up with the test names).
Are there any workarounds to this issue?
There seems to be something wrong here installing via yarn berry.
➤ YN0018: │ jest-specific-snapshot@npm:4.0.0: The remote archive doesn't match the expected checksum
// package.json
"jest-specific-snapshot": "^4.0.0",
I was so excited to find this matcher and then I read the limitations. Specifically:
.toMatchSpecificSnapshot does ignore serializers. It means that you cannot run with e.g. jest-styled-components and similar that make use of expect.addSnapshotSerializer.
Bummer... I am using styled-components
. I am not too well-versed in extending jest
or its inner working, but I am happy to try making this work with Styled Components. Can you point me in the right direction to get started?
expected result (content of snapshot file)
snapshot data
no need to `escape` "anything \"in\" here"
actual result
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`test name 1`] = `
snapshot data
unnecessary \`escapes\` "all \\"over\\" the place"
`;
challenges
jest-snapshot/State.save()
use case
in my tests i record API calls to a html canvas elemet (using jest-canvas-mock)
and im storing the events as javascript source code
benefits:
snapshot is human-readable and machine-readable
recorded events can easily be replayed to a live canvas element
There's an important fix in pretty-format
: jestjs/jest#9196
I've been using a JSON serializer in https://github.com/storybookjs/storybook/ for some of my test cases:
expect.addSnapshotSerializer({
print: (val: any) => JSON.stringify(val, null, 2),
test: (val) => typeof val !== 'string',
});
I've verified this works in standard jest snapshotting, and I'm pretty sure (but not 100% positive) that it used to work in jest-specific-snapshot
with older versions of jest
. But it's getting ignored with [email protected]
and using the standard Jest serializer.
Help a brother out? 🤷♂️
Obsolete snapshots are snapshots that were created by a test that no longer exists/had its name changed. These snapshots are not detected when running jest
. This doesn't matter that much when running jest normally, they'll be removed the next time you update snapshots. However, when running on CI, it's often desirable to fail CI if there are obsolete snapshots. This is the behaviour that jest --ci
does.
I believe these are referred to as unchecked
snapshots within the jest source code. These are tracked in the snapshotState
, so potentially adding them to the commonSnapshotState
could fix the issue. I tried to do this but was unsuccessful.
jest
to create the snapshotsjest --ci
jest --ci
should fail as there is an obsolete snapshot, but it passes and doesn't show any messaging about the obsolete snapshotI added enzyme-to-json as my serializer.
I tried to map out some properties with
map: (json) => ({
...json,
props: omitBy(json.props, (val, key) => key === "className"),
}),
These map out fine for normal snapshots, but using yours I can never get it to work. Can you advise?
You can read about the implementation here
medium.com has the bad habit of hiding content behind paywalls
please copy documentation to github repo / wiki / pages
So.. i've been tracking down a build issue I had with my setup.
It uses CRA to run a storybook, however.. CRA has not updated to jest 24.0.0 and is stuck on 23.6.0 because of some issue.
This package is used by @storybook/addon-storyshorts and uses the 1.0.0 of this package.
However, because you are requesting the latest possible version of jest-snapshot which in turn results in using [email protected] .. it crashes.
The reason is that an output file has been renamed from default_resolver.js to defaultResolver.js.
Blabla, long story short, could you please enforce a stricter version of jest-snapshot? To prevent breaking issues from coming through?
Hello,
I am trying to set up this custom matcher in one of my projects, but I cannot make the --updateSnapshot
flag to work.
I read this in the limitations:
In order to handle the --updateSnapshot (-u) parameter provided from CLI, there is an abuse of the SnapshotState._updateSnapshot private field. TBD - try to use the globalConfig to get this state.
But it is not still clear to me what should I do. I tried to set up that value in a globalConfig
but it didn‘t work. Could it be possible to create a section with clear steps to achieve this?
Regards and thanks in advance.
running storybook/[email protected] that depends on jest-specific-snapshot I get this weird behavior :
TypeError: Cannot read property 'close' of undefined
> 3 | import initStoryshots from '@storybook/addon-storyshots';
at Object.<anonymous> (node_modules/chalk/index.js:72:74)
at Object.<anonymous> (node_modules/jest-specific-snapshot/dist/index.js:12:21)
at Object.<anonymous> (node_modules/@storybook/addon-storyshots/dist/test-bodies.js:29:1)
at Object.<anonymous> (node_modules/@storybook/addon-storyshots/dist/index.js:18:19)
looks like at line 12 jest-specific-snapshot/dist/index.js is only importing jest-snapshot
var _jestSnapshot = require('jest-snapshot');
Any idea why I get this error ?
I'm using [email protected]
I'm in the process of adding mac and windows runners to the storybook unit tests (storybookjs/storybook#16744), and I've come across some odd behavior which I can also reproduce locally on my own mac, on the latest version of storybook. But I don't see any failures for it in the existing storybook unit tests, which have only been running on linux, which is why I say this is mac specific.
We have a describe.each
test, https://github.com/storybookjs/storybook/blob/a9f9abc0a7cd71744db4ea68cd2834d5b86325bc/lib/core-server/src/core-presets.test.ts#L132-L178, which creates multiple snapshot files for each test case. About one time in four, the wrong title is used in one or two of the snapshots, usually it's something like this:
diff --git a/lib/core-server/src/__snapshots__/vue-3-cli_manager-prod b/lib/core-server/src/__snapshots__/vue-3-cli_manager-prod
index 852c93c75b..9a5ed48d4d 100644
--- a/lib/core-server/src/__snapshots__/vue-3-cli_manager-prod
+++ b/lib/core-server/src/__snapshots__/vue-3-cli_manager-prod
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
-exports[`vue-3-cli manager production mode 1`] = `
+exports[`vue-3-cli preview dev mode 1`] = `
Object {
"entry": Array [
"ROOT/lib/core-client/dist/esm/globals/polyfills.js",
I also see this same intermittent behavior when I run in windows (in a vm on my mac), as well. Perhaps this is a jest issue and not a problem with this tool, but I wanted to bring it up here to see if you might have any insight into what might be going on. Any ideas you might have would be greatly appreciated! Thanks!
I tried to install this extension globally in the jest configuration file but got the following error:
ReferenceError: afterAll is not defined
Can you explain, how I can add "toMatchSpecificSnapshot" globally?
[Update] It seems that I managed to do what I need using the "setupTestFrameworkScriptFile" parameter in the configuration file. It work for Jest v.22
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.