代码之家  ›  专栏  ›  技术社区  ›  roydukkey

为什么在更改testMatch regex时Jest coverage报告会中断?

  •  0
  • roydukkey  · 技术社区  · 7 年前

    testMatch cli选项。我试过设置 collectCoverageFrom './src/*.js'

    ./src/sum.js

    function sum(a, b) {
        return a + b;
    }
    module.exports = sum;
    

    ./测试/sum.js

    const sum = require('../src/sum');
    
    test('adds 1 + 2 to equal 3', () => {
        expect(sum(1, 2)).toBe(3);
    });
    

    包.json

    "devDependencies": {
        "@babel/core": "^7.1.2",
        "@babel/plugin-proposal-decorators": "^7.1.2",
        "@babel/preset-env": "^7.1.0",
        "@babel/register": "^7.0.0",
        "babel-core": "7.0.0-bridge.0",
        "babel-jest": "^23.6.0",
        "jest": "^23.6.0"
    },
    
    "scripts": {
        "test": "jest ./test/*.js --testMatch '**/*.js' --coverage --coverageDirectory './test-coverage'"
    }
    

    npm run test

     PASS  test/sum.js
    ----------|----------|----------|----------|----------|-------------------|
    File      |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
    ----------|----------|----------|----------|----------|-------------------|
    All files |  Unknown |  Unknown |  Unknown |  Unknown |                   |
    ----------|----------|----------|----------|----------|-------------------|
    
    Test Suites: 1 passed, 1 total
    Tests:       1 passed, 1 total
    Snapshots:   0 total
    Time:        1.125s
    Ran all test suites matching /.\/test\/sum.js/i.
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Alien11689    7 年前

    设置 testRegex

    jest --testRegex='./test/.*\.js$' --coverage --coverageDirectory './test-coverage'
    

    现在输出如下所示:

    PASS  test/sum.js
    ✓ adds 1 + 2 to equal 3 (3ms)
    
    ----------|----------|----------|----------|----------|-------------------|
    File      |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
    ----------|----------|----------|----------|----------|-------------------|
    All files |      100 |      100 |      100 |      100 |                   |
     sum.js   |      100 |      100 |      100 |      100 |                   |
    ----------|----------|----------|----------|----------|-------------------|
    Test Suites: 1 passed, 1 total
    Tests:       1 passed, 1 total
    Snapshots:   0 total
    Time:        0.769s, estimated 1s
    Ran all test suites.