【发布时间】:2020-11-12 11:43:28
【问题描述】:
我正在使用 react-testing-library
我在 reducer 中使用 react-cookies 来加载用户。我想模拟 react-cookies,所以我可以在测试时将假用户加载到我的减速器。但是,我只想在第一个测试中模拟 react-cookies,因为在第二个测试中,我希望 cookie 返回 undefined,这与 react-cookies 的原始实现相同。所以我在我的测试中的 mocks 文件夹中有 react-cookies。所以我在嘲笑整个模块 react-cookies。
但我希望第二次测试忽略我在嘲笑 react-cookies。但是我尝试了jest.RequireActual, jest.unmock, jest.deepUnmock,但这些都不起作用,我的测试总是使用 react-cookies 的模拟实现。
测试文件:
describe('reducer', () => {
afterEach(() => {jest.unmock('react-cookies') });
test('should return initialState when user is defined ', () => {
expect(authentication(undefined, {})).toEqual(
{
user: {
name: "Smith",
},
})
})
test('should return initialState when user is undefined', () => {
expect(authentication(undefined, {})).toEqual(
{
user: undefined,
}
)
})
})
这是 mocks 文件夹中的 react-cookies 文件:
export default {
load: jest.fn()
.mockReturnValue(
{
name: "Smith"
})
}
【问题讨论】:
标签: reactjs react-redux react-testing-library