繁体   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

为什么我在使用 Babel 和 Jest 时不断收到错误消息“SyntaxError: Cannot use import statement outside a module”?

我有一个超级简单的应用程序,其中包含两个模块:

// moduleTwo.mjs:

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

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

// moduleOne.mjs:

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

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

 export { testFuncOneModOne }

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

这是我的 Jest 测试文件:

// 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:

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

// 包.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"
                    }

    }

// 错误信息(由我编辑):

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)

帮助将不胜感激,因为这让我发疯。

如果有人感兴趣,我最终自己解决了这个问题:

  1. 在我添加的 package.json 文件的顶层:
"type": "module"

这告诉节点项目中所有以 '.js' 结尾的文件都是 ES6 文件

  1. 我将模块的扩展名更改为“.js”(因此“moduleOne.mjs”变为“moduleOne.js”,“moduleTwo.mjs”变为“moduleTwo.js”

) 这与 package.json 文件的“jest”字段的值一致,该字段具有以下值:

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

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM