Node.js 无法在.spec.ts文件NodeJs中的模块外部使用导入语句

Node.js 无法在.spec.ts文件NodeJs中的模块外部使用导入语句,node.js,typescript,mocha.js,chai,Node.js,Typescript,Mocha.js,Chai,对于单元测试,我使用mocha+chai。 我使用“test”运行命令:“mocha--require ts node/register'src/***spec.ts' 当我在*spec.ts文件中使用import时(例如从'chai'导入{expect};) 我收到错误语法错误:无法在模块外部使用导入语句 My tsconfig.json: { "compilerOptions": { "baseUrl": "src", "target": "esnext", "m

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

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

我收到错误
语法错误:无法在模块外部使用导入语句

My 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"]
}

您是否尝试过
const expect=require('chai')

这是一种包装模块的方法,其中使用的模块相当于从'chai'导入{expect}

“摩卡”仅为commonJS模块设计。您可以通过在node_modules/mocha中搜索自述文件来找到它

如果您想使用esnext,我建议您改为“开玩笑”。它和“摩卡”没什么不同

一个适用于我的配置示例:

tsconfig.json

{
  "compilerOptions": {
    "target": "es6",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "skipLibCheck": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noEmit": true,
    "esModuleInterop": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve"
  },
  "exclude": [
    "node_modules"
  ],
  "include": [
    "next-env.d.ts",
    "**/*.ts",
    "**/*.tsx"
  ]
}
{
  "name": "website",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "test": "jest",
    "test-watch": "npm run test -- --watch",
    "pretty": "prettier --write \"**/*.js\""
  },
  "dependencies": {
    "@material-ui/core": "^4.9.9",
    "@material-ui/icons": "^4.9.1",
    "next": "9.3.4",
    "react": "16.13.1",
    "react-dom": "16.13.1"
  },
  "devDependencies": {
    "@types/jest": "^25.2.1",
    "@types/node": "^13.11.0",
    "@types/react": "^16.9.32",
    "jest": "^25.2.7",
    "prettier": "^2.0.2",
    "ts-jest": "^25.3.1",
    "ts-node": "^8.8.1",
    "typescript": "^3.8.3"
  }
}
module.exports = {
  roots: ["<rootDir>"],
  transform: {
    "^.+\\.tsx?$": "ts-jest"
  },
  testRegex: "(/tests/.*|(\\.|/)(test|spec))\\.tsx?$",
  moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"]
};
package.json

{
  "compilerOptions": {
    "target": "es6",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "skipLibCheck": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noEmit": true,
    "esModuleInterop": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve"
  },
  "exclude": [
    "node_modules"
  ],
  "include": [
    "next-env.d.ts",
    "**/*.ts",
    "**/*.tsx"
  ]
}
{
  "name": "website",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "test": "jest",
    "test-watch": "npm run test -- --watch",
    "pretty": "prettier --write \"**/*.js\""
  },
  "dependencies": {
    "@material-ui/core": "^4.9.9",
    "@material-ui/icons": "^4.9.1",
    "next": "9.3.4",
    "react": "16.13.1",
    "react-dom": "16.13.1"
  },
  "devDependencies": {
    "@types/jest": "^25.2.1",
    "@types/node": "^13.11.0",
    "@types/react": "^16.9.32",
    "jest": "^25.2.7",
    "prettier": "^2.0.2",
    "ts-jest": "^25.3.1",
    "ts-node": "^8.8.1",
    "typescript": "^3.8.3"
  }
}
module.exports = {
  roots: ["<rootDir>"],
  transform: {
    "^.+\\.tsx?$": "ts-jest"
  },
  testRegex: "(/tests/.*|(\\.|/)(test|spec))\\.tsx?$",
  moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"]
};
jest.config.json

{
  "compilerOptions": {
    "target": "es6",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "skipLibCheck": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noEmit": true,
    "esModuleInterop": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve"
  },
  "exclude": [
    "node_modules"
  ],
  "include": [
    "next-env.d.ts",
    "**/*.ts",
    "**/*.tsx"
  ]
}
{
  "name": "website",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "test": "jest",
    "test-watch": "npm run test -- --watch",
    "pretty": "prettier --write \"**/*.js\""
  },
  "dependencies": {
    "@material-ui/core": "^4.9.9",
    "@material-ui/icons": "^4.9.1",
    "next": "9.3.4",
    "react": "16.13.1",
    "react-dom": "16.13.1"
  },
  "devDependencies": {
    "@types/jest": "^25.2.1",
    "@types/node": "^13.11.0",
    "@types/react": "^16.9.32",
    "jest": "^25.2.7",
    "prettier": "^2.0.2",
    "ts-jest": "^25.3.1",
    "ts-node": "^8.8.1",
    "typescript": "^3.8.3"
  }
}
module.exports = {
  roots: ["<rootDir>"],
  transform: {
    "^.+\\.tsx?$": "ts-jest"
  },
  testRegex: "(/tests/.*|(\\.|/)(test|spec))\\.tsx?$",
  moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"]
};
module.exports={
根:[''],
转换:{
“^.+\.tsx?$”:“ts笑话”
},
testRegex:(/tests/*|(\\.\124;/)(test | spec))\\\.tsx?$”,
moduleFileExtensions:[“ts”、“tsx”、“js”、“jsx”、“json”、“节点”]
};

这是可行的,但我想导入具有该样式的模块。也许我错过了什么?因为其他ts文件工作得很好,但不是.spec.ts文件。Jest很糟糕,速度很慢,所以不要使用它。只要坚持摩卡咖啡。