@ 范围在打字稿项目中不起作用



我正试图通过应用@作用域机制来改进我的代码,这样我就可以使用@来表示./src/:

// tsconfig.json
{
"compilerOptions": {
"module": "commonjs",
"target": "es2017",
"declaration": true,
"outDir": "./dist",
"esModuleInterop": true,
"baseUrl": ".",
"paths": {
"@/*": ["./src/*"]
}
},
"include": ["./src/**/*"]
}

我的文件结构如下:

<root>
dist/
libraries/
node_modules/
src/
generated/
clients/
tests/
clients/
session.test.ts
constants/
util/
package.json
tsconfig.json

session.test.ts内部,我将做如下操作:

import { Constants } from '@/tests/constants';
import { Util } from '@/util';

但是我一直得到这个错误:

无法从'src/tests/clients/session.test.ts'中找到模块'@/tests/constants'

我在这里做错了什么?

编辑

有了这个设置,VS Code就不会抱怨了。只有在使用

运行测试时才会出现错误
jest ./src/tests/**/*.test.ts --coverage --runInBand

感谢@JBallin的评论。这使我解决了我的问题。

我的jest.config.js现在是:

/** @type {import('ts-jest').JestConfigWithTsJest} */
const { pathsToModuleNameMapper } = require('ts-jest');
const { compilerOptions } = require('./tsconfig');
module.exports = {
coverageDirectory: 'coverage',
coverageProvider: 'babel',
coverageReporters: ['json', 'text', 'lcov', 'clover', 'cobertura'],
preset: 'ts-jest',
testEnvironment: 'node',
testSequencer: './custom-sequencer.js',
transform: { '^.+\.ts?$': 'ts-jest' },
moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths, {
prefix: '<rootDir>/'
})
};

事实上,VS Code可以告诉范围工作,但运行我的测试是错误发生的地方,应该告诉我问题是如何配置jest。

最新更新