gpt4 book ai didi

javascript - 如何为某个测试更改 Jest 模拟的实现

转载 作者:搜寻专家 更新时间:2023-10-30 21:57:33 24 4
gpt4 key购买 nike

我在我的 test/__mocks__ 文件夹中创建了一个文件,我在其中模拟了一个 npm 模块。这个文件的链接被添加到我的 Jest 设置中。一切都很好,这让我可以很好地测试它。但是现在对于某个测试,我需要更改此测试的返回值。我怎样才能做到这一点?

我尝试 unMock 加上 setMock 等。但没有任何效果。

// test/__mocks__/touchId.ts

jest.mock('react-native-touch-id', () => {
return {
isSupported: jest.fn(() => Promise.resolve(true)),
authenticate: jest.fn(() => Promise.resolve(true)),
};
});

还有我的测试

it('should not navigate to main if touch id return false', async () => {
jest.setMock('react-native-touch-id', {
authenticate: jest.fn(() => Promise.resolve(false)),
});
const pinCreation = new PinCreationStore();

const spy = jest.spyOn(NavigationServices, 'navigate');

spy.mockReset();

await pinCreation.verifyUser();

expect(spy).toHaveBeenCalledTimes(0);

spy.mockRestore();
});

这里我仍然得到 true,所以我的测试崩溃了。

最佳答案

您可以使用 jest.mock() 而无需创建 __mocks__ 文件夹。

例如:

react-native-touch-id.ts,为了简单起见,我模拟了这个模块。你可以用真正的 npm 模块替换它。

const touchId = {
isSupported() {
return false;
},
authenticate() {
return false;
}
};
export default touchId;

react-native-touch-id.spec.ts:

import touchId from './react-native-touch-id';

jest.mock('./react-native-touch-id', () => {
return {
isSupported: jest.fn(() => Promise.resolve(true)),
authenticate: jest.fn(() => Promise.resolve(true))
};
});

describe('react-native-touch-id', () => {
it('t1', () => {
expect(touchId.isSupported()).toBeTruthy();
expect(touchId.authenticate()).toBeTruthy();
});

it('t2', () => {
(touchId.isSupported as jest.MockedFunction<typeof touchId.isSupported>).mockReturnValueOnce(false);
(touchId.authenticate as jest.MockedFunction<typeof touchId.authenticate>).mockReturnValueOnce(false);
expect(touchId.isSupported()).toBeFalsy();
expect(touchId.authenticate()).toBeFalsy();
});

it('t3', () => {
(touchId.isSupported as jest.MockedFunction<typeof touchId.isSupported>).mockReturnValueOnce(true);
(touchId.authenticate as jest.MockedFunction<typeof touchId.authenticate>).mockReturnValueOnce(false);
expect(touchId.isSupported()).toBeTruthy();
expect(touchId.authenticate()).toBeFalsy();
});
});

如您所见,在您 mock 了 react-native-touch-id 模块之后,当您希望这两个方法具有不同的值时,您需要将其导入并再次 mock。这些模拟值将用于导入和使用 react-native-touch-id 模块的 isSupportedauthenticate 方法的其他模块。

单元测试结果:

 PASS  src/stackoverflow/52172531/react-native-touch-id.spec.ts
react-native-touch-id
✓ t1 (5ms)
✓ t2 (1ms)
✓ t3

Test Suites: 1 passed, 1 total
Tests: 3 passed, 3 total
Snapshots: 0 total
Time: 4.065s

关于javascript - 如何为某个测试更改 Jest 模拟的实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52172531/

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