Reputation: 463
The tests in my project were working fine when I first started using them, currently they have stopped working at all.
Whenever I use the test command the following error is thrown:
~/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
This error thrown makes it seem as if there is some issue in csv-writer
module, but I cant seem to find out what the issue actually is and how I can make the tests run if I dont want to remove this package.
My package.json file:
...
"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/"
],
}
I am using nodejs v14.0.6
witht typescript and mocha for testing.
What am I doing wrong ?
Upvotes: 1
Views: 409
Reputation: 519
I encountered the same problem.
Apparently, the csv-writer
package contains tests, like array.test.ts
specified in your stack trace.
This is your script used for running the mocha tests:
{
...
test:run: "mocha --require ts-node/register --watch-extensions ts './**/test/**/*.ts'"
...
}
The problem is that you recursively look for the following pattern: './**/test/**/*.ts'
As you can see, the csv-writer
tests match the specified pattern:
~/node_modules/csv-writer/src /test/ csv-stringifiers/array .test.ts
The solution is to exclude the node_modules
directory when running the mocha tests:
{
...
test:run: "mocha --require ts-node/register --watch-extensions ts './{,!(node_modules)/**}/test/**/*.ts'"
...
}
I hope this answer helps you.
Upvotes: 1