When using expect.toMatchInlineSnapshot using prettier 2.1.0, jest throws the following error:
FAIL src/App.test.tsx
● Test suite failed to run
TypeError: Cannot read property 'start' of undefined
at CallExpression (node_modules/jest-snapshot/build/inline_snapshots.js:217:57)
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 4.224s
Ran all test suites related to changed files.
Watch Usage: Press w to show more.
Steps to reproduce the behavior:
yarn create react-app --template typescript
// App.test.tsx
test('matches snapshot', () => {
const { container } = render(<App />);
expect(container).toMatchInlineSnapshot();
});
yarn add [email protected] -D
yarn test
yarn add [email protected] -D
yarn test
Test should write a new snapshot like at step 3.
https://www.github.com/iwazaru/jest-inline-snapshot-bug
System:
OS: macOS 10.15.6
CPU: (4) x64 Intel(R) Core(TM) i5-3210M CPU @ 2.50GHz
Binaries:
Node: 12.18.3 - ~/.nvm/versions/node/v12.18.3/bin/node
Yarn: 1.22.4 - /usr/local/bin/yarn
npm: 6.14.7 - ~/.nvm/versions/node/v12.18.3/bin/npm
I've bisected prettier, and the regression was introduced in https://github.com/prettier/prettier/pull/8678. Probably worth opening an issue in the prettier repo?
/cc @fisker @evilebottnawi @thorn0
Happy to change something here if our current implementation is buggy, of course
Specifically, it's the const {line, column} = callee.property.loc.start; line that fails
I don't know we use loc, sorry! I'll check how can you get that location info.
@SimenB I had a quick look, seems it's not only babel used (it's not safe anyway, not all parsers has location info), we don't use any location info inside Prettier, but we have range info parsers[inferredParser].locStart(node) and parsers[inferredParser].locEnd(node). Do you think it's better to calculate the location info here?
/cc @evilebottnawi @thorn0
I'm afraid what we've done the breaking change, because it was a part of public API (and jest uses it)
AST property is not the part of API. That's why I propose use locStart and locEnd, they are.
I'm happy to change here to use some other API (PR very much welcome!), but it might be best to reintroduce the loc field for now anyways as we won't be patching older versions of Jest
The only loc I removed from https://github.com/prettier/prettier/pull/8678 is for typescript parser, so for a quick fix, we may just need add this line back
Some other part in Prettier updates the range, so the location info still not "real", but it works that way before too.
Reintroducing that line sounds good, and we can possibly migrate Jest over to use locStart and locEnd helpers so it can be removed from prettier in the future?
I'll add it back, and we might won't remove it(not too much cost), but as I said before
not all parsers has location info
So, we still better refactor in Jest.
One thing to note is that CRA currently uses Jest 24. Upgrading to Jest 26 still fails though (and the linked fix in Prettier fixes it) due to the missing loc field. #10254 added a guard which makes the error be Jest: Couldn't locate all inline snapshots. rather than failing with a TypeError
2.1.1 is released and updating fixes the reproduction in the OP. Thanks!
Most helpful comment
2.1.1 is released and updating fixes the reproduction in the OP. Thanks!