Jest 仅在指定两个 config.js 时运行单个项目

Jest only runs single project when specifying two config.js

我的 package.json.

的笑话部分指定了两个项目
"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)$'],
};


原来我用 Object.assign(..) 覆盖了我的常用笑话配置,因此停止了项目列表中第一个项目。

为了解决这个问题,我能够在使用赋值之前对其进行深度复制。

const commonJest = require('common/jest.config');

const commonJestCopy = Object.assign({}, commonJest)

module.exports = Object.assign(commonJestCopy, {
    //...overrides
}