jest: Inline Snapshots with non literal can't be found
🐛 Bug Report
When I try to add a test with an inline snapshot:
it('works with inline snapshots', () => {
expect({a: 1}).toMatchInlineSnapshot();
});
I get this error:
Jest: Couldn't locate all inline snapshots.
I can’t repro this in repl.it because that version does not have inline snapshots yet:
TypeError: expect(...).toMatchInlineSnapshot is not a function
I cloned the github repo and added a test to inline_snapshots.test.js
test('saveInlineSnapshots() replaces empty function call with a template literal for objects', () => {
const filename = path.join(__dirname, 'my.test.js');
fs.readFileSync = (jest.fn(
() => `expect({a: 'a'}).toMatchInlineSnapshot();\n`,
): any);
saveInlineSnapshots(
[
{
frame: {column: 11, file: filename, line: 1},
snapshot: `{a: 'a'}`,
},
],
prettier,
babelTraverse,
);
expect(fs.writeFileSync).toHaveBeenCalledWith(
filename,
"expect({a: 'a'}).toMatchInlineSnapshot(`{a: 'a'}`);\n",
);
});
and this reproes the issue:
FAIL packages/jest-snapshot/src/__tests__/inline_snapshots.test.js
✓ saveInlineSnapshots() replaces empty function call with a template literal (67ms)
✕ saveInlineSnapshots() replaces empty function call with a template literal for objects (19ms)
✓ saveInlineSnapshots() replaces existing template literal - babylon parser (4ms)
✓ saveInlineSnapshots() replaces existing template literal - flow parser (2ms)
✓ saveInlineSnapshots() replaces existing template literal - typescript parser (2ms)
✓ saveInlineSnapshots() replaces existing template literal with property matchers (3ms)
✓ saveInlineSnapshots() throws if frame does not match (2ms)
✓ saveInlineSnapshots() throws if multiple calls to to the same location (2ms)
✓ saveInlineSnapshots() uses escaped backticks (2ms)
● saveInlineSnapshots() replaces empty function call with a template literal for objects
Jest: Couldn't locate all inline snapshots.
at Object.parse (packages/jest-snapshot/src/inline_snapshots.js:168:11)
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 11
- Comments: 28 (18 by maintainers)
Commits related to this issue
- Add test cases for #6744 — committed to azz/jest by azz 6 years ago
- Add test cases for #6744 — committed to azz/jest by azz 6 years ago
- Add test cases for #6744 (#6772) — committed to jestjs/jest by azz 6 years ago
- Merge remote-tracking branch 'upstream/master' into pr/6334 * upstream/master: (122 commits) fix: don't report promises as open handles (#6716) support serializing `DocumentFragment` (#6705) Al... — committed to rhysawilliams2010/jest by thymikee 6 years ago
- enable sourceMaps to make jest's inline snapshot work https://github.com/facebook/jest/issues/6744#issuecomment-861722016 — committed to sitek94/gilded-rose-typescript by sitek94 2 years ago
What helped for me with Jest 27 and typescript is to add
sourceMap: true
in mytsconfig.json
. It immediately fixed the problem.ts-jest
used to have it on by default, but changed it in v27.@SimenB I don’t have a minimal repro yet. I can reproduce it in react’s codebase though:
Produces
Applying
Fixes the issue.
So it looks like the stack trace is based on the code after transpilation (particularly after
@babel/plugin-transform-shorthand-properties
).I’m seeing this problem as well:
Jest: 27.4.7 ts-jest: 27.1.2
repo and branch with error : https://github.com/rkesters/typescript-json-validator/tree/rkesters/deps2
run: npx jest index
I have sourceMap set to true.
Error Message:
FAIL src/tests/index.test.ts ● Test suite failed to run
Finding myself here again, having gotten burned by this issue while trying to rewrite some other React DevTools tests to use inline snapshots. They’re so much better (when they work!)
As far as I can tell the cause of https://github.com/facebook/jest/issues/6744#issuecomment-748042991 is missing source-maps.
React’s jest config has a custom preprocessor that transpiles the files without source maps. So
expect({state, store}).toMatchInlineSnapshot();
will be transpiled toexpect({ state: state, store: store }).toMatchInlineSnapshot();
which means that the generated snapshot will point to a different column (it’s using the stack trace) compared to the original source code.I could fix https://github.com/facebook/jest/issues/6744#issuecomment-748042991 with
But I don’t understand how jest creates accurate frames for other cases. For example
expect({stat, store}).toMatchInlineSnapshot();
will throw a ReferenceError that points to the correct column.