Currently, I am utilizing Jest in conjunction with ts-jest to conduct tests on my TypeScript code. Everything is functioning well except for the issue that arises when importing external libraries. Strangely enough, while import dotenv from 'dotenv';
does not work as intended, using
import * as dotenv from 'dotenv';
resolves the problem. Although adjusting my code to adhere to this format is a temporary solution, I am inclined to find a permanent resolution to this predicament.
Displayed below is my configuration file, jest.config.js
:
module.exports = {
transform: { '^.+\\.ts?$': 'ts-jest' },
testEnvironment: 'node',
testRegex: '/tests/.*\\.(test|spec)?\\.(ts|tsx)$',
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
moduleNameMapper: {
'^@apps/(.*)$': '<rootDir>/src/apps/$1',
'^@core/(.*)$': '<rootDir>/src/core/$1',
},
};
In addition, here is an excerpt from my tsconfig.json
:
{
"compilerOptions": {
"exactOptionalPropertyTypes": true,
"noFallthroughCasesInSwitch": true,
"noImplicitOverride": true,
"noImplicitReturns": true,
"noUnusedLocals": true,
"strict": true,
"module": "Node16",
"paths": {
"@apps/*": ["./src/apps/*"],
"@core/*": ["./src/core/*"]
},
"resolveJsonModule": true,
"downlevelIteration": true,
"outDir": "dist",
"removeComments": true,
"sourceMap": true,
"allowSyntheticDefaultImports": true,
"forceConsistentCasingInFileNames": true,
"lib": ["es2021"],
"target": "ES2021"
},
"include": ["src/**/*", "tests/**/*", "index.d.ts"]
}