无法在.spec.ts文件NodeJs中的模块外部使用import语句

问题描述 投票:0回答:1

对于单元测试,我使用mocha + chai。我用"test": "mocha --require ts-node/register 'src/**/*spec.ts'"运行命令。

[当我在* spec.ts文件中使用导入时(例如import { expect } from 'chai';

我收到错误SyntaxError: Cannot use import statement outside a module

我的tsconfig.json:

{
  "compilerOptions": {
    "baseUrl": "src",
    "target": "esnext",
    "module": "esnext",
    "moduleResolution": "node",
    "lib": ["dom", "es2018"],
    "allowSyntheticDefaultImports": true,
    "noUnusedLocals": true,
    "removeComments": true,
    "resolveJsonModule": true,
    "strict": true,
    "typeRoots": ["node_modules/@types"]
  },
  "exclude": ["node_modules"],
  "include": ["src/**/*.ts"]
}
node.js typescript mocha chai
1个回答
0
投票

您尝试过吗? const expect= require('chai')

© www.soinside.com 2019 - 2024. All rights reserved.