gpt4 book ai didi

typescript - Ts-Jest Typescript诊断:错误TS2307:找不到模块

转载 作者:行者123 更新时间:2023-12-04 00:12:23 26 4
gpt4 key购买 nike

我在包含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';
~~~~~~~~~~~~~~~~~~~~~

最佳答案

虽然不是直接解决方案,但我通过将测试的位置更改为位于要测试的文件旁边来解决了该错误:

- test/unit/regexes.spec.ts -> src/renderer/resources/regexes.spec.ts
- src/renderer/resources/regexes.ts

关于typescript - Ts-Jest Typescript诊断:错误TS2307:找不到模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57262334/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com