我在一个混合了 js 和 ts 文件的 vue 项目中使用 ts-jest。该项目使用该@
字符将导入相对映射到目录src/renderer/
。TS 为找不到模块发出警告。我可以忽略警告并通过执行以下操作运行测试:
globals: {
'ts-jest': {
/* Fails on mapped import syntax without this.*/
diagnostics: {
ignoreCodes: ['2307'],
},
},
},
但我希望导入按预期运行。
jest.config.js
module.exports = {
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'vue'],
transform: {
'^.+\\.js$': '<rootDir>/node_modules/babel-jest',
'.*\\.(vue)$': 'vue-jest',
'.+\\.(css|styl|less|sass|scss|png|jpg|ttf|woff|woff2)$': 'jest-transform-stub',
'^.+\\.tsx?$': 'ts-jest',
},
moduleNameMapper: {
'^@/(.*)$': '<rootDir>/src/renderer/$1',
'^vue$': 'vue/dist/vue.common.js',
},
roots: ['<rootDir>', '<rootDir>/src/renderer'],
modulePaths: ['<rootDir>'],
setupFiles: ['<rootDir>/vue-test-setup.ts'],
snapshotSerializers: ['jest-serializer-vue'],
testMatch: ['**/test/**/*.spec.(js|jsx|ts|tsx)|**/__test__/*.(js|jsx|ts|tsx)'],
transformIgnorePatterns: ['<rootDir>/node_modules/?!(vue-timers)'],
globals: {
'ts-jest': {
/* Fails on mapped import syntax without this.*/
diagnostics: {
warnOnly: true,
},
},
},
};
考验在于,test/unit/regexes.spec.ts
进口在于src/renderer/resources/regexes.ts
我得到的具体错误是:
FAIL test/unit/specs/regexes.spec.ts
● Test suite failed to run
TypeScript diagnostics (customize using `[jest-config].globals.ts-jest.diagnostics` option):
test/unit/specs/regexes.spec.ts:1:19 - error TS2307: Cannot find module '@/resources/regexes'.
1 import REGEX from '@/resources/regexes';
~~~~~~~~~~~~~~~~~~~~~