【问题标题】:jest.ts and got.ts giving "SyntaxError: Cannot use import statement outside a module"jest.ts 和 got.ts 给出 \"SyntaxError: Cannot use import statement outside a module\"
【发布时间】:2022-11-23 20:03:49
【问题描述】:

我是 Typescript、jest.ts 和 got.ts 的新手——我正在尝试创建一个 API 测试,但我得到了一个

SyntaxError: Cannot use import statement outside a module

我不明白为什么会这样。我尝试了几个修复程序,但都失败了。

包.json

{
  "type": "module",
  "dependencies": {
    "@types/got": "^9.6.12",
    "got": "^12.5.1",
    "ts-node": "^10.9.1"
  },
  "devDependencies": {
    "@testing-library/jest-dom": "^5.16.5",
    "@types/jest": "^29.1.2",
    "ts-jest": "^29.0.3",
    "typescript": "^4.8.4"
  },
  "scripts": {
    "test-gotjest": "npx jest ./tests/gotjest"
  }
}

jest.config.js

export default {
  preset: 'ts-jest',
  setupFilesAfterEnv: ["@testing-library/jest-dom"]
};

实际的测试文件本身 - gotjest.test.ts

import { got } from "got";
import {describe, expect, test} from '@jest/globals';

  describe('see if got works', () => {
    test('get a cat fact', () => {
    let foo = got.get('https://catfact.ninja/fact');
          expect(3).toBe(3);
    });
  });

当我使用“npm run test-gotjest”运行测试时,我得到以下信息:

    SyntaxError: Cannot use import statement outside a module

    > 1 | import { got } from "got";
        | ^
      2 | import {describe, expect, test} from '@jest/globals';
      3 |
      4 | describe('sum module', () => {

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1678:14)
      at Object.<anonymous> (tests/gotjest/gotjest.test.ts:1:1)

我错过了什么? 当我用 out got 运行时,导入语句为

import {describe, expect, test} from '@jest/globals';

工作正常。帮助!

【问题讨论】:

    标签: typescript ts-jest node.js-got


    【解决方案1】:

    有同样的问题,发现这个:

    https://github.com/sindresorhus/got/issues/1952

    长话短说:博士;降级到让 v11 与 jest 互操作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-21
      • 2022-11-26
      • 1970-01-01
      • 2022-11-21
      • 1970-01-01
      • 2022-09-26
      • 2020-08-30
      相关资源
      最近更新 更多