相关文章推荐
鼻子大的领结  ·  记录一个boost1.72 ...·  2 月前    · 
英姿勃勃的课本  ·  Microsoft Entra ...·  10 月前    · 
爱听歌的松树  ·  ASP.Net Core API ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I encountered this error when I tried running a unit test for my React component. I use VSCode so I notice that when I wrote the expect().toBe(..), it doesn't auto suggest the term so I think there's something wrong with my files. I tested on another project and the autosuggestion worked fine. I only use a basic true assertion to test if the test file works or not

    Jest encountered an unexpected token
    Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.
    Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.
    By default "node_modules" folder is ignored by transformers.
    Here's what you can do:
     • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
     • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
     • If you need a custom transformation specify a "transform" option in your config.
     • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.
    You'll find more details and examples of these config options in the docs:
    https://jestjs.io/docs/configuration
    For information about custom transformations, see:
    https://jestjs.io/docs/code-transformation
    Details:
    /Users/daniel/Documents/GitHub/flinder-react/src/components/FirstStep.test.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import React from 'react';
                                                                                      ^^^^^^
    SyntaxError: Cannot use import statement outside a module
      at Runtime.createScriptFromCode (node_modules/jest/node_modules/jest-runtime/build/index.js:1479:14)
Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        0.311 s
Ran all test suites.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! flinder-react@0.1.0 test: `jest`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the flinder-react@0.1.0 test script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/daniel/.npm/_logs/2021-09-03T23_14_44_337Z-debug.log
daniel@daniels-Air flinder-react % npm run test
> flinder-react@0.1.0 test /Users/daniel/Documents/GitHub/flinder-react
 FAIL  src/components/FirstStep.test.js
  ● Test suite failed to run
    Jest encountered an unexpected token
    Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.
    Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.
    By default "node_modules" folder is ignored by transformers.
    Here's what you can do:
     • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
     • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
     • If you need a custom transformation specify a "transform" option in your config.
     • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.
    You'll find more details and examples of these config options in the docs:
    https://jestjs.io/docs/configuration
    For information about custom transformations, see:
    https://jestjs.io/docs/code-transformation
    Details:
    SyntaxError: /Users/daniel/Documents/GitHub/flinder-react/src/components/FirstStep.test.js: Support for the experimental syntax 'jsx' isn't currently enabled (10:33):
       8 | describe('FirstStep', () => {
       9 |     it('button is disabled', () => {
    > 10 |         const wrapper = shallow(<FirstStep />);
         |                                 ^
      11 |         const button  = wrapper.find("Button");
      13 |         expect(button.props().disabled).toBe(true);
    Add @babel/preset-react (https://git.io/JfeDR) to the 'presets' section of your Babel config to enable transformation.
    If you want to leave it as-is, add @babel/plugin-syntax-jsx (https://git.io/vb4yA) to the 'plugins' section to enable parsing.
      at Parser._raise (node_modules/@babel/parser/src/parser/error.js:134:45)
      at Parser.raiseWithData (node_modules/@babel/parser/src/parser/error.js:129:17)
      at Parser.expectOnePlugin (node_modules/@babel/parser/src/parser/util.js:198:18)
      at Parser.parseExprAtom (node_modules/@babel/parser/src/parser/expression.js:1275:18)
      at Parser.parseExprSubscripts (node_modules/@babel/parser/src/parser/expression.js:652:23)
      at Parser.parseUpdate (node_modules/@babel/parser/src/parser/expression.js:632:21)
      at Parser.parseMaybeUnary (node_modules/@babel/parser/src/parser/expression.js:604:23)
      at Parser.parseExprOps (node_modules/@babel/parser/src/parser/expression.js:374:23)
      at Parser.parseMaybeConditional (node_modules/@babel/parser/src/parser/expression.js:340:23)
      at Parser.parseMaybeAssign (node_modules/@babel/parser/src/parser/expression.js:279:21)
Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        0.395 s
Ran all test suites.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! flinder-react@0.1.0 test: `jest`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the flinder-react@0.1.0 test script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/daniel/.npm/_logs/2021-09-03T23_17_16_517Z-debug.log
                Seems like the issue that you might have faced is resolved. But for the reference, adding the link to the changes which fixed the similar issue on my end. github.com/facebook/create-react-app/issues/…
– aevin
                Mar 1 at 11:26

You are trying to use modules syntax but Jest works with commonjs syntax by default.

SyntaxError: Cannot use import statement outside a module

Jest supports es modules but it's currently experimental now and not so much stable. You can use for now babel-jest in order to transpile your code. In your jest config add a transform definition and use babel preset. Read more about it: https://www.npmjs.com/package/babel-jest And in the "getting started" guide of jest: https://jestjs.io/docs/getting-started

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.