我在package.json的jest部分中指定了两个项目。
"jest": {
"projects": ["<rootDir>/jest.unit.config.js", "<rootDir>/tests/jest.component.config.js"]
}每当我在命令行上运行jest时,它只会拾取并找到jest.component.config.js。
我尝试从项目列表中删除jest.component.config.js并运行jest,在这种情况下,它确实成功地运行了单元测试配置。
找到并运行两者的诀窍是什么?
//jest.unit.config.js
const jestConfig = require('/common/jest.config');
module.exports = Object.assign(jestConfig, {
displayName: {
color: 'cyan',
name: 'unit-tests'
},
coverageThreshold: {
global: {
branches: 20,
functions: 20,
lines: 20,
statements: 20,
}
}
});//jest.component.config.js
const jestConfig = require('common/jest.config');
module.exports = Object.assign(jestConfig, {
rootDir: '.',
displayName: {
color: 'yellow',
name: 'component-tests',
},
testMatch: ['./**/*test.ts'],
testEnvironment: './helpers/test-environment.js',
});//common jest.config.js
module.exports = {
collectCoverage: true,
collectCoverageFrom: [
"src/**/*.{js,jsx,ts,tsx,mjs}",
],
coverageDirectory: "<rootDir>/coverage",
coverageProvider: 'babel',
coverageReporters: ['text', 'html'],
coverageThreshold: {
global: {
branches: 50,
functions: 50,
lines: 50,
statements: 50,
},
},
moduleFileExtensions: ['js', 'jsx', 'ts', 'tsx', 'mjs', 'json'],
modulePathIgnorePatterns: [],
prettierPath: "prettier",
testEnvironment: 'node',
testMatch: ['**/__tests__/**/*.(js|ts|jsx|tsx|mjs)'],
testPathIgnorePatterns: ['/node_modules/', '/fixtures/', '/__tests__/helpers/', '__mocks__', 'dist', '.yalc'],
transform: {
'^.+\\.(ts|tsx)$': 'ts-jest',
'^.+\\\\.(js|jsx|mjs)$': 'babel-jest',
},
transformIgnorePatterns: ['[/\\\\]node_modules[/\\\\].+\\.(js|ts|jsx|tsx|mjs)$'],
};发布于 2020-05-13 21:29:32
事实证明,我用Object.assign(..)重写了我的公共jest配置,从而停止了项目列表中的第一个项目。
为了解决这个问题,我可以在使用赋值之前创建一个深度副本。
const commonJest = require('common/jest.config');
const commonJestCopy = Object.assign({}, commonJest)
module.exports = Object.assign(commonJestCopy, {
//...overrides
}https://stackoverflow.com/questions/61761836
复制相似问题