gpt4 book ai didi

javascript - Redux reducer 的分支覆盖率

转载 作者:数据小太阳 更新时间:2023-10-29 03:55:29 26 4
gpt4 key购买 nike

假设我有以下 reducer :

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() 调用没有涵盖这种情况。

当 reducer 被缩减为:

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
}
}
}

编辑2:以下测试也不影响测试覆盖率:

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

我仍然不明白为什么最初的默认测试实现从分支覆盖的 Angular 来看不等价。

最佳答案

myReducer() 的最后一个测试涵盖了 initialState 的默认值的情况,在您的情况下是 {foo: ''} 得到应用。

关于javascript - Redux reducer 的分支覆盖率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42136828/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com