'Node / Express Aliases with Jest
I am currently configuring file path aliases to simplify relative path imports within the project. I have everything working perfectly when running locally. Here is some context:
tsconfig.json
{
"compilerOptions": {
"target": "es6", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
"lib": [
"ES2020.Promise"
], /* Specify library files to be included in the compilation. */
"allowJs": true, /* Allow javascript files to be compiled. */
"outDir": "./build", /* Redirect output structure to the directory. */
"rootDir": "./src", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
"strict": true, /* Enable all strict type-checking options. */
"noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
"moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
"baseUrl": "./src", /* Base directory to resolve non-absolute module names. */
"paths": {
"@models/*": ["models/*"],
"@services/*": ["server/services/*"],
"@spikeball-api/types/*": ["server/types/*"],
},
"types": ["jest", "node"], /* Type declaration files to be included in compilation. */
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
},
"include": [
"src/**/*",
"decs.d.ts",
],
"exclude": [
"node_modules"
],
"ts-node": {
"files": true,
"transpileOnly": true
}
}
My package.json has the following:
"_moduleAliases": {
"@models": "build/models",
"@services": "build/server/services",
"@spikeball-api/types": "build/server/types",
"@spikeball-api/graphql": "build/server/app/graphql"
},
My jest.config.ts is:
import type { Config } from '@jest/types'
import jestModuleNameMapper from "jest-module-name-mapper"
// For possible configuration options see: https://jestjs.io/docs/configuration
// Sync object
const config: Config.InitialOptions = {
preset: "ts-jest",
testEnvironment: "node",
verbose: true,
forceExit: true,
// clearMocks: true,
roots: [
"<rootDir>/src"
],
testMatch: [
// "**/__tests__/**/*.+(ts|tsx|js)",
// "**/?(*.)+(spec|test).+(ts|tsx|js)",
// When wanting to only test one file for simplicity purposes or test suite debugging
// "**/**/__tests__/app/graphql/posts/mutation/likePost.test.ts",
"**/**/__tests__/models/Badge.test.ts",
],
transform: {
"^.+\\.(ts|tsx)$": "ts-jest"
},
coverageDirectory: "<rootDir>/coverage",
collectCoverage: true,
collectCoverageFrom: [
"**/*.{js,jsx}",
"**/*.{ts,tsx}",
"!**/node_modules/**",
"!**/vendor/**",
"!**/__tests__/**",
],
/* This should be used when implementing CI tests for PR and merge checks */
// coverageThreshold: {
// global: {
// branches: 80,
// functions: 80,
// lines: 80,
// statements: -10
// }
// },
globalSetup: "<rootDir>/src/__tests__/setup/setup.api.ts",
setupFiles: [
"<rootDir>/src/__tests__/setup/setupFile.ts"
],
testPathIgnorePatterns: [
"<rootDir>/src/__tests__/setup"
],
moduleFileExtensions: ["js", "jsx", "ts", "tsx", "graphql", "json", "node"],
// For mapping module aliases
moduleNameMapper: {
"@models/*(.*)": "<rootDir>/src/models/$1",
"@services/*(.*)": "<rootDir>/src/server/services/$1",
"@spikeball-api/types/*(.*)": "<rootDir/src/server/types/$1",
}
}
export default config
The error I keep getting:
Error: Jest: Got error running globalSetup - /Users/shanekeney/Documents/Spikeball/spikeball-api/src/__tests__/setup/setup.api.ts, reason: Cannot find module '@spikeball-api/types/graphql'
Require stack: ...
So it's clearly something with the aliases. And only when running Jest as everything works fine locally in development when I run the server with the following: nodemon --exec ts-node src/server/app.ts,
`.
I've tried several different solutions from all types of combinations with the jest config regex patterns. Any help would be greatly appreciated.
Solution 1:[1]
Resolved this by doing the following:
Added import 'tsconfig-paths/register'; to the top of my globalSetup file. This was silly mistake of mine as I didn't think about jest global set up running outside my test environment so the linking import I had set in the entry of the application didn't apply for setup file.
I'm also pretty sure there is an error in my format for moduleNameMapper file aliases in jest.config.ts so I ended up using a library to handle the mapping for me:
import jestModuleNameMapper from "jest-module-name-mapper"
...
// And then just use the function as follows in your jest.config.ts
moduleNameMapper: jestModuleNameMapper()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | SKeney |
