I struggled with this for a Node.js, Express, TypeScript, Jest, ts-jext project. The issue, for me, was getting the jest.config.json configured correctly.
I'm posting my files in case this helps someone.
PACKAGE.JSON
{
"name": "express-react",
"version": "0.0.0",
"private": true,
"main": "dist/index.js",
"type": "module",
"engines": {
"node": ">=14.0.0"
},
"scripts": {
"start:dev": "npm run build && npm run dev",
"build": "npm run format && npm run lint && npm run tsc",
"start:prod": "npm run build && node --experimental-specifier-resolution=node dist/index.js",
"start": "npm run start:prod",
"dev": "cross-env-shell DEBUG=express:* node --experimental-specifier-resolution=node --loader ts-node/esm ./src/index.ts",
"format": "prettier --write \"**/*.ts\"",
"lint": "eslint . --ext 'ts,json'",
"tsc": "tsc",
"test": "cross-env-shell TS_JEST_LOG=ts-jest.jog NODE_OPTIONS=--experimental-vm-modules jest --detectOpenHandles --verbose --testTimeout 20000 --coverage=true --reporters=default --json --debug --outputFile=./test/log.json",
"test:list": "jest --detectOpenHandles --coverage=true --listTests --debug"
},
"dependencies": {
"cookie-parser": "~1.4.4",
"cors": "^2.8.5",
"debug": "~2.6.9",
"dotenv": "^16.0.0",
"express": "~4.16.1",
"express-fileupload": "^1.3.1",
"http-errors": "^1.6.3",
"into-stream": "^7.0.0",
"jest": "^28.1.0",
"morgan": "~1.9.1",
"multer": "^1.4.4",
"save-dev": "0.0.1-security",
"sharp": "^0.30.4",
"supertest": "^6.2.3",
"ts-jest": "^28.0.2",
"ts-node": "^10.7.0",
"uuid": "^8.3.2"
},
"devDependencies": {
"@types/jest": "^27.5.1",
"@types/supertest": "^2.0.12",
"@azure/storage-blob": "^12.9.0",
"@types/cookie-parser": "^1.4.3",
"@types/cors": "^2.8.12",
"@types/debug": "^4.1.7",
"@types/express": "^4.16.1",
"@types/express-fileupload": "^1.2.2",
"@types/http-errors": "^1.8.2",
"@types/into-stream": "^3.1.1",
"@types/morgan": "^1.9.3",
"@types/multer": "^1.4.7",
"@types/node": "^17.0.31",
"@types/sharp": "^0.30.2",
"@types/uuid": "^8.3.4",
"@typescript-eslint/eslint-plugin": "^5.22.0",
"@typescript-eslint/parser": "^5.22.0",
"cross-env": "^7.0.3",
"eslint": "^8.15.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-jsx-a11y": "^6.5.1",
"eslint-plugin-node": "^11.1.0",
"nodemon": "^2.0.16",
"prettier": "^2.6.2",
"tslib": "^2.4.0",
"typescript": "^4.6.4"
}
}
TSCONFIG.JSON
{
"name": "express-react",
"version": "0.0.0",
"private": true,
"main": "dist/index.js",
"type": "module",
"engines": {
"node": ">=14.0.0"
},
"scripts": {
"start:dev": "npm run build && npm run dev",
"build": "npm run format && npm run lint && npm run tsc",
"start:prod": "npm run build && node --experimental-specifier-resolution=node dist/index.js",
"start": "npm run start:prod",
"dev": "cross-env-shell DEBUG=express:* node --experimental-specifier-resolution=node --loader ts-node/esm ./src/index.ts",
"format": "prettier --write \"**/*.ts\"",
"lint": "eslint . --ext 'ts,json'",
"tsc": "tsc",
"test": "cross-env-shell TS_JEST_LOG=ts-jest.jog NODE_OPTIONS=--experimental-vm-modules jest --detectOpenHandles --verbose --testTimeout 20000 --coverage=true --reporters=default --json --debug --outputFile=./test/log.json",
"test:list": "jest --detectOpenHandles --coverage=true --listTests --debug"
},
"dependencies": {
"cookie-parser": "~1.4.4",
"cors": "^2.8.5",
"debug": "~2.6.9",
"dotenv": "^16.0.0",
"express": "~4.16.1",
"express-fileupload": "^1.3.1",
"http-errors": "^1.6.3",
"into-stream": "^7.0.0",
"jest": "^28.1.0",
"morgan": "~1.9.1",
"multer": "^1.4.4",
"save-dev": "0.0.1-security",
"sharp": "^0.30.4",
"supertest": "^6.2.3",
"ts-jest": "^28.0.2",
"ts-node": "^10.7.0",
"uuid": "^8.3.2"
},
"devDependencies": {
"@types/jest": "^27.5.1",
"@types/supertest": "^2.0.12",
"@azure/storage-blob": "^12.9.0",
"@types/cookie-parser": "^1.4.3",
"@types/cors": "^2.8.12",
"@types/debug": "^4.1.7",
"@types/express": "^4.16.1",
"@types/express-fileupload": "^1.2.2",
"@types/http-errors": "^1.8.2",
"@types/into-stream": "^3.1.1",
"@types/morgan": "^1.9.3",
"@types/multer": "^1.4.7",
"@types/node": "^17.0.31",
"@types/sharp": "^0.30.2",
"@types/uuid": "^8.3.4",
"@typescript-eslint/eslint-plugin": "^5.22.0",
"@typescript-eslint/parser": "^5.22.0",
"cross-env": "^7.0.3",
"eslint": "^8.15.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-jsx-a11y": "^6.5.1",
"eslint-plugin-node": "^11.1.0",
"nodemon": "^2.0.16",
"prettier": "^2.6.2",
"tslib": "^2.4.0",
"typescript": "^4.6.4"
}
}
JEST.CONFIG.JS
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
module.exports = {
clearMocks: true,
moduleFileExtensions: ["js", "ts", "json", "node"],
roots: ["./"],
testMatch: ["./**/*.test.ts"],
preset: "ts-jest",
testEnvironment: "node",
transform: {
"^.+\\.ts?$": "ts-jest",
},
transformIgnorePatterns: [
"./node_modules/",
"./dist/",
"./files/",
"./public/",
],
extensionsToTreatAsEsm: ['.ts'],
globals: {
"ts-jest": {
tsconfig: './tsconfig.json',
useESM: true
}
}
};