我有下一个结构:
-src
-qa
在qa文件夹中有一个配置文件
import { InitialOptions } from '@jest/types/build/Config';
const config: InitialOptions = {
verbose: true,
preset: 'ts-jest',
testEnvironment: 'node',
moduleFileExtensions: ['js', 'json', 'ts'],
rootDir: '../',
globalSetup: './qa/global-setup.ts',
globalTeardown: './qa/global-teardown.ts',
testRunner: 'jest-jasmine2',
testRegex: '.*-spec.ts$',
transform: {
'^.+\\.ts$': 'ts-jest',
},
setupFilesAfterEnv: ['jest-allure/dist/setup'],
testTimeout: 40 * 1000,
moduleNameMapper: {
// '^src/(.)*$': '<rootDir>/src/$1',
'^src/*': '../*',
'^src/(.*)$': '../$1',
},
// reporters: ['./reporter'],
};
export default config;
而且我在 src 文件夹中有 ts.config.json
{
"compilerOptions": {
"module": "commonjs",
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es2017",
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./",
"incremental": true,
"skipLibCheck": true,
"typeRoots": [ "./types", "./node_modules/@types"]
},
"exclude": ["node_modules", "dist"]
}
import { Service } from 'src/service';
到
import { Service } from '../service';
效果很好。但开发商反对改变道路。 怎么解决?
这与nestjs无关
这是由于 jest 中使用了绝对路径,默认情况下不支持。请参阅:在 Typescript Jest 测试中导入绝对路径