简体   繁体   English

为什么 Jest 一直给我错误“SyntaxError: Cannot use import statement outside a module'”? 我正在使用 Babel 转译 ES6 以供 Jest 测试

[英]Why does Jest keep giving me error "SyntaxError: Cannot use import statement outside a module' "? I am using Babel to transpile ES6 for Jest to test

Why do I keep getting error message "SyntaxError: Cannot use import statement outside a module" when I use Babel and Jest?为什么我在使用 Babel 和 Jest 时不断收到错误消息“SyntaxError: Cannot use import statement outside a module”?

I have a super simple app that contains two modules:我有一个超级简单的应用程序,其中包含两个模块:

// moduleTwo.mjs: // moduleTwo.mjs:

let testFuncOneModTwo = () => {
    return ('String generated by fn in moduleTwo.mjs')
                              }
    
    export {testFuncOneModTwo } 

///////////// /////////////

// moduleOne.mjs: // moduleOne.mjs:

import {testFuncOneModTwo} from './moduleTwo.mjs'

let testFuncOneModOne = () => {
    return (testFuncOneModTwo())
                              }

 export { testFuncOneModOne }

////////////// //////////////

Here's my Jest test file:这是我的 Jest 测试文件:

// myTest.test.js: // myTest.test.js:

import * as myImports from './moduleOne.mjs';

test('tests fn in module that calls fn in another module.', () => {
          expect(myImports.testFuncOneModOne()).toEqual( 'String generated by fn in
                                        moduleTwo.mjs' );
    });

///////////// /////////////

// babel.config.json: // babel.config.json:

{
  "presets": ["@babel/preset-env"]
}

// package.json: // 包.json:

{
  "name": "JestIsVeryTesting",
  "version": "1.0.0",
  "description": "",
  "main": "moduleOne.mjs",


  "scripts": {
    "test": "jest --coverage "
             },
  "jest": {
    "transform": {
      "^.+\\.js?$": "babel-jest"
                 }
          },

  "dependencies": {
    "@babel/runtime": "^7.18.6"
                  },

  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@babel/plugin-transform-runtime": "^7.18.6",
    "@babel/preset-env": "^7.18.6",
    "babel": "^6.23.0",
    "babel-jest": "^28.1.2",
    "jest": "^28.1.2"
                    }

    }

// The error message (edited by me): // 错误信息(由我编辑):

Test suite failed to run

    Jest encountered an unexpected token

    Jest failed to parse a file.   ...

...
...

 Details:

    /xxx/xxx/xxx/xxx/moduleOne.mjs:91
    import { testFuncOneModTwo } from './moduleTwo.mjs';
    ^^^^^^


 SyntaxError: Cannot use import statement outside a module

      1 | 
    > 2 | import * as myImports from './moduleOne.mjs';
        | ^
      3 |
      4 | test('tests fn in module that calls fn in another module.', () => {
      5 |           expect(myImports.testFuncOneModOne()).toEqual( 'This string returned by a function in moduleTwo.mjs' );

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1796:14)
      at Object.<anonymous> (myTest.test.js:2:1)

Help would be much appreciated as this is driving me mad.帮助将不胜感激,因为这让我发疯。

In case anyone is interested I solved the problem myself in the end:如果有人感兴趣,我最终自己解决了这个问题:

  1. To the top level of the package.json file I added:在我添加的 package.json 文件的顶层:
"type": "module"

This tells node that all files in the project that end with '.js' are ES6 files这告诉节点项目中所有以 '.js' 结尾的文件都是 ES6 文件

  1. I changed the extensions of my modules to '.js' (so that 'moduleOne.mjs' became 'moduleOne.js' and 'moduleTwo.mjs' became 'moduleTwo.js'我将模块的扩展名更改为“.js”(因此“moduleOne.mjs”变为“moduleOne.js”,“moduleTwo.mjs”变为“moduleTwo.js”

) This is in keeping with the value of the package.json file's "jest" field, which has value: ) 这与 package.json 文件的“jest”字段的值一致,该字段具有以下值:

"transform": {
      "^.+\\.js?$": "babel-jest"
                 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 开玩笑的单元测试 - SyntaxError:不能在模块外使用 import 语句 - Jest unit test - SyntaxError: Cannot use import statement outside a module 使用 VSCode 调试器进行 Jest 测试会引发错误“SyntaxError: Cannot use import statement outside a module” - Using VSCode Debugger for Jest Tests throws error “SyntaxError: Cannot use import statement outside a module” 为什么在使用 cloudinary 模块时出现“语法错误:无法在模块外使用导入语句”错误? - Why am I getting the 'SyntaxError: Cannot use import statement outside a module' error while using the cloudinary module? 如何修复 SyntaxError:无法在使用 Jest 的模块外部使用 import 语句? - How do I fix SyntaxError: Cannot use import statement outside a module using Jest? Jest - SyntaxError: 无法在模块外使用 import 语句 - Jest - SyntaxError: Cannot use import statement outside a module Jest 或 Mocha 与 Vue:SyntaxError:无法在模块外使用 import 语句 - Jest or Mocha with Vue: SyntaxError: Cannot use import statement outside a module Jest 无法导入 vue-test-utils 并出现“SyntaxError: Cannot use import statement outside a module”异常 - Jest fails to import vue-test-utils with "SyntaxError: Cannot use import statement outside a module" exception 使用 Vue Js 运行 Jest 测试时出现“语法错误:无法在模块外使用导入语句” - "SyntaxError: Cannot use import statement outside a module" when running a Jest test with Vue Js Jest.js 和 Create-react-app:运行测试时出现“语法错误:无法在模块外使用导入语句” - Jest.js and Create-react-app: I get "SyntaxError: Cannot use import statement outside a module" when running test React CKeditor Jest 错误:SyntaxError:无法在模块外使用导入语句 - React CKeditor Jest error: SyntaxError: Cannot use import statement outside a module
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM