Jest에서 ModuleNameMapper를 통해 경로별칭을 이용하여 불러오는 과정에서 발생한 문제이다.
최종수정일 : 2024.02.28
/src/features/featuredir/feature.spec.ts
import { mine } from 'mine';
describe('captureBatchThumbnails', () => {
it('Initialize CrownCase', () => {
console.log(mine(1, 2));
});
});
/src/mine/index.ts
export function mine(a: number, b: number): number {
return a + b;
}
/jest.config.js
export default {
preset: 'ts-jest',
testMatch: ['**/__tests__/**/*.+(ts|tsx|js)', '**/?(*.)+(spec|test).+(ts|tsx|js)'],
transform: {
'^.+\\.(ts|tsx)$': ['ts-jest', { tsconfig: 'tsconfig.esm.json' }],
},
testPathIgnorePatterns: ['/node_modules/', '/dist/'],
roots: ['<rootDir>'],
moduleFileExtensions: ['js', 'mjs', 'cjs', 'jsx', 'ts', 'tsx', 'json', 'node'],
moduleNameMapper: {
'^@constants/(.*)$': '<rootDir>/src/constants/$1',
'^@types/(.*)$': '<rootDir>/src/types/$1',
'^classes/(.*)$': '<rootDir>/src/classes/$1',
'^features/(.*)$': '<rootDir>/src/features/$1',
'^lib/(.*)$': '<rootDir>/src/lib/$1',
'^managers/(.*)$': '<rootDir>/src/managers/$1',
'^modules/(.*)$': '<rootDir>/src/modules/$1',
'^utils/(.*)$': '<rootDir>/src/utils/$1',
'^mine/(.*)$': '<rootDir>/src/mine/$1',
},
};
ModuleNameMapper에서 하위항목이 있어야하도록 정의되었기 때문이다. 다음과 같이 변경하였다.
/jest.config.js
export default {
preset: 'ts-jest',
testMatch: ['**/__tests__/**/*.+(ts|tsx|js)', '**/?(*.)+(spec|test).+(ts|tsx|js)'],
transform: {
'^.+\\.(ts|tsx)$': ['ts-jest', { tsconfig: 'tsconfig.esm.json' }],
},
testPathIgnorePatterns: ['/node_modules/', '/dist/'],
roots: ['<rootDir>'],
moduleFileExtensions: ['js', 'mjs', 'cjs', 'jsx', 'ts', 'tsx', 'json', 'node'],
moduleNameMapper: {
'^@constants(.*)$': '<rootDir>/src/constants$1',
'^@types(.*)$': '<rootDir>/src/types$1',
'^classes(.*)$': '<rootDir>/src/classes$1',
'^features(.*)$': '<rootDir>/src/features$1',
'^lib(.*)$': '<rootDir>/src/lib$1',
'^managers(.*)$': '<rootDir>/src/managers$1',
'^modules(.*)$': '<rootDir>/src/modules$1',
'^utils(.*)$': '<rootDir>/src/utils$1',
'^mine(.*)$': '<rootDir>/src/mine$1',
},
};