簡體   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