SyntaxError: Cannot use import statement outside a module error is thrown while using mocha to run tests

SyntaxError: Cannot use import statement outside a module error is thrown while using mocha to run tests

我的项目中的测试在我刚开始使用时运行良好,目前它们完全停止工作了。

每当我使用测试命令时,都会抛出以下错误:

~/node_modules/csv-writer/src/test/csv-stringifiers/array.test.ts:1
import {resolveDelimiterChar} from '../helper/delimiter';
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at wrapSafe (internal/modules/cjs/loader.js:979:16)
    at Module._compile (internal/modules/cjs/loader.js:1027:27)
    at Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Object.require.extensions.<computed> [as .ts] (~/node_modules/ts-node/src/index.ts:1045:43)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Module.require (internal/modules/cjs/loader.js:952:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at Object.exports.requireOrImport (~/backend/node_modules/mocha/lib/esm-utils.js:20:12)
    at Object.exports.loadFilesAsync (~/node_modules/mocha/lib/esm-utils.js:33:34)
    at Mocha.loadFilesAsync (~/node_modules/mocha/lib/mocha.js:431:19)
    at singleRun (~/node_modules/mocha/lib/cli/run-helpers.js:125:15)
    at exports.runMocha (~/node_modules/mocha/lib/cli/run-helpers.js:190:10)
    at Object.exports.handler (~/node_modules/mo

抛出的这个错误让人觉得 csv-writer 模块似乎有问题,但我似乎无法找出问题的真正所在以及如何进行测试 运行 如果我不想删除这个包。

我的 package.json 文件:

...
  "scripts": {
    "test": "cross-env GENSCRIP_PORT=3001 && cross-env GENSCRIP_SECRET=xxxx && cross-env GENSCRIP_DB_URL=mongodb://xxxx/genscrip_dev && cross-env GENSCRIP_BE_ENV=development && cross-env GENSCRIP_FE_URL=http://xxxx/ && npm run test:run",
    "test:run": "mocha --require ts-node/register --watch-extensions ts './**/test/**/*.ts'",
    "coverage": "tsc && nyc --reporter=lcov npm run test",
    "start:dev": "nodemon --config nodemon.json ./src/index.ts",
    "start": "npm run build && npm run start:node",
    "start:node": "node ./dist/index.js",
    "dev:debug": "nodemon --config nodemon.json --inspect-brk ./src/index.ts",
    "build": "rimraf ./dist && tsc"
  },
...
"dependencies": {
    "@elastic/elasticsearch": "^7.11.0",
    "@types/cors": "^2.8.9",
    "@types/nodemailer": "^6.4.0",
    "@types/nodemailer-direct-transport": "^1.0.31",
    "@types/nodemailer-smtp-transport": "^2.7.4",
    "@types/uuid": "^8.3.0",
    "bcryptjs": "^2.4.3",
    "body-parser": "^1.19.0",
    "cors": "^2.8.5",
    "csv-parser": "^3.0.0",
    "csv-writer": "^1.6.0",
    "express": "^4.17.1",
    "fuzzball": "^1.3.1",
    "fuzzy": "^0.1.3",
    "helmet": "^4.4.1",
    "inversify": "^5.0.5",
    "jsonwebtoken": "^8.5.1",
    "moment": "^2.29.1",
    "mongoose": "^5.11.10",
    "morgan": "^1.10.0",
    "nodemailer": "^6.4.18",
    "uuid": "^8.3.2"
  },
  "devDependencies": {
    "@istanbuljs/nyc-config-typescript": "^1.0.1",
    "@tsconfig/node12": "^1.0.7",
    "@types/bcryptjs": "^2.4.2",
    "@types/chai": "^4.2.14",
    "@types/express": "^4.17.9",
    "@types/jsonwebtoken": "^8.5.0",
    "@types/mocha": "^8.2.0",
    "@types/mongoose": "^5.10.3",
    "@types/morgan": "^1.9.2",
    "@types/node": "^14.14.37",
    "@types/sinon": "^9.0.10",
    "chai": "^4.2.0",
    "cross-env": "^7.0.3",
    "mocha": "^8.2.1",
    "nodemon": "^2.0.4",
    "nyc": "^15.1.0",
    "reflect-metadata": "^0.1.13",
    "rimraf": "^3.0.2",
    "sinon": "^9.2.4",
    "source-map-support": "^0.5.19",
    "ts-node": "^9.1.1",
    "tslint": "^6.1.3",
    "typescript": "^4.1.3"
  },
  "nyc": {
    "extends": "@istanbuljs/nyc-config-typescript",
    "include": "src",
    "exclude": "src/**/test/*",
    "all": true,
    "check-coverage": true
  }

tsconfig.json

{
    "compilerOptions": {
        "module": "CommonJS",
        "esModuleInterop": true,
        "target": "es6",
        "noImplicitAny": true,
        "moduleResolution": "node",
        "sourceMap": true,
        "outDir": "dist",
        "baseUrl": ".",
        "noEmit": false,
        "paths": {
            "@domain/*":["./src/domain"],
            "@data_entry/*":["./src/data_entry"],
            "*": [
                "node_modules/*"
            ]
        },
        "experimentalDecorators": true,
        "emitDecoratorMetadata": true,
        "lib": ["es6","dom"],
        "types": ["reflect-metadata","mocha","node"]
    },
    "include": [
        "src/**/*", 
        "index.ts"    ],
    "exclude": [
            "src/tests/",
            "node_modules/",
            "dist/"
          ],

}

我正在使用带有 typescript 和 mocha 的 nodejs v14.0.6 进行测试。

我做错了什么?

我遇到了同样的问题

显然,csv-writer 包包含测试,例如在堆栈跟踪中指定的 array.test.ts

这是您用于 运行 mocha 测试的脚本:

{
...    

test:run: "mocha --require ts-node/register --watch-extensions ts './**/test/**/*.ts'"

...
}

问题是您递归地寻找以下模式: './**/test/**/*.ts'

如您所见,csv-writer 测试匹配指定的模式:

~/node_modules/csv-writer/src /test/ csv-stringifiers/array .test.ts

解决方法是运行mocha测试时排除node_modules目录:

{
...    

test:run: "mocha --require ts-node/register --watch-extensions ts './{,!(node_modules)/**}/test/**/*.ts'"

...
}

希望这个回答对您有所帮助。