I'm in the process of migrating some node packages which has become quite a headache.
Currently this is what my package.json looks like:
{
//These are the updated dependencies and Jest configurations
"dependencies": {
"@google-cloud/logging-winston": "^4.0.2",
"@google-cloud/secret-manager": "^3.2.3",
"@google-cloud/storage": "^5.7.0",
"@nestjs/common": "^8.2.3",
"@nestjs/config": "^1.1.5",
"@nestjs/core": "^8.2.3",
"@nestjs/jwt": "^8.0.0",
"@nestjs/mongoose": "^9.0.1",
"@nestjs/passport": "^8.0.1",
"@nestjs/platform-express": "^8.2.3",
"bcrypt": "^5.0.1",
"class-transformer": "^0.5.1",
"class-validator": "^0.13.2",
"dayjs": "^1.9.7",
"helmet": "^4.2.0",
"mongoose": "^6.0.14",
"nanoid": "^3.1.20",
"nest-winston": "^1.4.0",
"p-all": "^3.0.0",
"passport": "^0.4.1",
"passport-jwt": "^4.0.0",
"passport-local": "^1.0.0",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.4.0",
"winston": "^3.3.3"
},
"devDependencies": {
"@nestjs/cli": "^8.1.5",
"@nestjs/schematics": "^8.0.5",
"@nestjs/testing": "^8.2.3",
"@types/bcrypt": "^5.0.0",
"@types/express": "^4.17.13",
"@types/jest": "^27.0.3",
"@types/multer": "^1.4.7",
"@types/node": "^16.11.11",
"@types/passport-jwt": "^3.0.3",
"@types/passport-local": "^1.0.33",
"@types/supertest": "^2.0.10",
"@typescript-eslint/eslint-plugin": "^4.6.1",
"@typescript-eslint/parser": "^4.6.1",
"eslint": "^7.12.1",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^3.4.0",
"jest": "^27.4.3",
"jest-mock": "^27.4.2",
"prettier": "^2.2.1",
"supertest": "^6.1.3",
"ts-jest": "^27.1.0",
"ts-loader": "^9.2.6",
"ts-node": "^10.4.0",
"tsconfig-paths": "^3.9.0",
"typescript": "^4.5.2"
},
"jest": {
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": "src",
"testRegex": ".*\\.spec\\.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"moduleDirectories": [
"node_modules",
"src"
],
"collectCoverageFrom": [
"**/*.(t|j)s"
],
"coveragePathIgnorePatterns": [
".module.ts",
"main.ts",
"env-var-names.ts",
".d.ts",
".types.ts",
".e2e-spec.ts"
],
"coverageDirectory": "../coverage",
"testEnvironment": "node",
"resetMocks": true,
"resetModules": true
}
}
When I try to run Jest, I get the following error relating to /node_modules
● Test suite failed to run
Jest encountered an unexpected token
Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.
Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.
By default "node_modules" folder is ignored by transformers.
Here's what you can do:
• If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
• If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
• To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
• If you need a custom transformation specify a "transform" option in your config.
• If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.
You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/configuration
For information about custom transformations, see:
https://jestjs.io/docs/code-transformation
Details:
/home/{username}/{project_directory}/node_modules/mongodb/src/bson.ts:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import type {
^^^^^^
SyntaxError: Cannot use import statement outside a module
at Runtime.createScriptFromCode (../node_modules/jest-runtime/build/index.js:1728:14)
at Object.<anonymous> (../node_modules/mongodb/src/bson.ts:8:12)
So within our team, we've been able to fix it by changing a part of the Jest configuration from:
"moduleDirectories": [
"node_modules",
"src"
],
To so:
"moduleDirectories": [
"node_modules",
],
We also had to change our import paths back to relative. This gets rid of the error and is working fine, however we are a bit stumped as to why this works? Does anyone have any insight they could share?
Thank you!