Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error while Jest setup for Snapshots in " if (error?.stack) "

I'm going to create snapshot test, but got problems in the beginning.

I got such error while running test:

    /Users/illia/WebstormProjects/TESTS/node_modules/jest/node_modules/jest-cli/build/cli/index.js:161
    if (error?.stack) {
              ^
SyntaxError: Unexpected token '.'

In the test file I have no errors

import renderer from 'react-test-renderer';

import PaymentDisclaimer from './PaymentDisclaimer';

    it('renders correctly when all default props', () => {
        const tree = renderer.create(<PaymentDisclaimer fullPrice={9} />).toJSON();
        expect(tree).toMatchSnapshot();
    });

Packages:

"react-test-renderer": "^18.2.0",
"jest": "^29.0.3",
"ts-jest": "^29.0.1", (was installed as possible solution)
like image 878
illia_6655321 Avatar asked Nov 28 '25 19:11

illia_6655321


1 Answers

This happens when jest is running under the node version that do not read the new updates from JS. You need to run it in node 14 or higher.

Specifically, the ?. in if (error?.stack) is an optional chaining operator which is only supported in versions 14 or higher.

You can switch Node version using nvm:

$ nvm use 14 
Now using node v14.18.0 (npm v6.14.15)

check Node Version Manager (nvm)

like image 190
gmBarroso Avatar answered Dec 01 '25 07:12

gmBarroso