I am using nestjs.
Need run migrations before run all tests. And after all runned test nedd run clear test schema.
If i use javascript setup files in test/config/setup.js package.json all works.
But if i use typescript files dont work.
"globals": {
"ts-jest": {
"tsConfigFile": "tsconfig.json"
}
},
"globalSetup": "./config/setup.ts",
"globalTeardown": "./config/teardown.ts",
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": "./test",
"testRegex": ".spec.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"coverageDirectory": "../coverage",
"testEnvironment": "node"
}
test/config/setup.ts
import * as dotenv from 'dotenv'; // errors when run jest
module.exports = async () => {
dotenv.config({path: '.env.test'});
};
Errors
/home/jashka/job/projects/project-name/test/config/setup.ts:1
(function (exports, require, module, __filename, __dirname) { import * as dotenv from 'dotenv';
^
SyntaxError: Unexpected token *
at new Script (vm.js:79:7)
at createScript (vm.js:251:10)
at Object.runInThisContext (vm.js:303:10)
at Module._compile (internal/modules/cjs/loader.js:656:28)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)
at Module.load (internal/modules/cjs/loader.js:598:32)
at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
at Function.Module._load (internal/modules/cjs/loader.js:529:3)
at Module.require (internal/modules/cjs/loader.js:636:17)
at require (internal/modules/cjs/helpers.js:20:18)
This is my jest.config.js file, which works with .ts setup files. Add '^.+\\.tsx?$': 'ts-jest', to transform your .ts files and change tsConfigFile to tsConfig.
module.exports = {
setupFiles: ['<rootDir>/tests/unit/setup.ts'],
globalSetup: '<rootDir>/tests/unit/globalSetup.ts',
globalTeardown: '<rootDir>/tests/unit/globalTeardown.ts',
moduleFileExtensions: ['js', 'jsx', 'ts', 'tsx'],
transform: {
'^.+\\.jsx?$': 'babel-jest',
'^.+\\.tsx?$': 'ts-jest',
},
moduleNameMapper: {
'^@/(.*)$': '<rootDir>/src/$1',
},
testMatch: [
'<rootDir>/tests/unit/**/(*.)spec.(js|jsx|ts|tsx)',
],
globals: {
'ts-jest': {
babel: true,
tsConfig: "tsconfig.json",
}
}
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With