Redux减速机的分支覆盖范围

时间:2017-02-09 12:32:19

标签: javascript ecmascript-6 redux code-coverage jestjs

说我有以下减速器:

import {FOO} from '../const/Foo'

const myReducer = (initialState = {foo: ''}, action) => {
  const state = {}
  if (action) {
    switch (action.type) {
      case FOO:
        state.foo = action.foo
    };
  }
  return Object.assign({}, initialState, state)
}

我使用jest测试:

import FOO from '../const/Foo'

test('returns correct state when action is not "Foo"', () => {
  expect(myReducer({foo: 'bar'}, {type: 'foo'})).toEqual({foo: 'bar'})
})

test("returns correct state when action is 'Foo'", () => {
  expect(myReducer({}, {type: FOO, foo: 'bar'})).toEqual({foo: 'bar'})
})

test('when there is no action / testing the default', () => {
  expect(myReducer()).toEqual({foo: ''})
})

这会产生4/5的分支覆盖率。经过一些思考/删除和/或重新添加行之后,我已经到达了initialState集合上的分支逻辑。这几乎是有道理的。不同的是:

1)为什么没有进行最后一次测试,空的myReducer()电话会覆盖这种情况。

减速机减速时:

const myReducer = (initialState = {foo: ''}, action) => {
    const state = {}
    return Object.assign({}, initialState, state)
}

测试(现在失败)的分支覆盖率为1/1。

这里发生了什么?

编辑:根据请求,我正在添加配置。我将以下jest.json传递给jest:

{

  "bail": true,
  "verbose": true,
  "moduleNameMapper": {
    "\\.(sass|jpg|png)$": "<rootDir>/src/main/js/config/emptyExport.js"
  },
  "testRegex": ".*(?<!snapshot)\\.(test|spec)\\.js$",
  "collectCoverage": true,
  "collectCoverageFrom": ["src/main/js/**/*.js"
    , "!**/node_modules/**"
    , "!**/*spec.js"
    , "!src/main/js/config/emptyExport.js"
    , "!**/coverage/**/*.js"
    , "!src/main/js/app.js"
    , "!src/main/js/store/configureStore.js"
    , "!src/main/js/reducers/index.js"],
  "coverageDirectory": "<rootDir>/src/main/js/coverage",
  "coverageThreshold": {
    "global": {
      "branches": 85,
      "function": 95,
      "lines": 95,
      "statements": 95
    }
  }
}

EDIT2: 以下测试也不会影响测试覆盖率:

test('when there is no action / testing the default', () => {
  expect(addressReducer(undefined, {foo: 'bar'})).toEqual({address: ''})
})

我仍然不明白为什么初始默认测试实现在分支覆盖范围内并不相同。

2 个答案:

答案 0 :(得分:2)

使用myReducer()进行的最后一次测试涵盖了initialState的{​​{1}}的默认值应用的情况。

答案 1 :(得分:1)

我认为缺少的是您没有对default的{​​{1}}分支进行测试。你需要添加

select