首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当指定两个config.js时,Jest仅运行单个项目

当指定两个config.js时,Jest仅运行单个项目
EN

Stack Overflow用户
提问于 2020-05-13 05:01:18
回答 1查看 170关注 0票数 0

我在package.json的jest部分中指定了两个项目。

代码语言:javascript
复制
"jest": {
    "projects": ["<rootDir>/jest.unit.config.js", "<rootDir>/tests/jest.component.config.js"]
}

每当我在命令行上运行jest时,它只会拾取并找到jest.component.config.js

我尝试从项目列表中删除jest.component.config.js并运行jest,在这种情况下,它确实成功地运行了单元测试配置。

找到并运行两者的诀窍是什么?

代码语言:javascript
复制
//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,
        }
    }
});
代码语言:javascript
复制
//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',
});
代码语言:javascript
复制
//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)$'],
};
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-05-13 21:29:32

事实证明,我用Object.assign(..)重写了我的公共jest配置,从而停止了项目列表中的第一个项目。

为了解决这个问题,我可以在使用赋值之前创建一个深度副本。

代码语言:javascript
复制
const commonJest = require('common/jest.config');

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

module.exports = Object.assign(commonJestCopy, {
    //...overrides
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61761836

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档