使用模拟笑话间谍实现在笑话上异步运行多个测试失败(但单独运行成功)

问题描述 投票:0回答:0

因此,我在挂载 TSX 文件的同一场景中进行了多次测试,但由于我有多个异步代码和多个模拟,因此值似乎可能重叠?

const getLoadedWrapperAsync = (async () => {
    const wrapper = getWrapper();
    await flushPromises();
    wrapper.update(); // forces DOM to render for test
    return wrapper;
  });

const getWrapper = (): ReactWrapper<any, Readonly<{}>, React.Component<{}, {}, any>> => {
    const wrapper = mount(<SomeComponent/>);
    return wrapper;
  };

afterEach(() => { jest.restoreAllMocks(); });

it('Test A', async done => {
    const logSpy = jest.spyOn(console, 'log');
    jest.spyOn(ClassName.prototype, 'methodName').mockImplementation(() => Promise.resolve(null));
    const wrapper = await getLoadedWrapperAsync();
    expect(blabla wrapper and logspy);
    done();
})

it('Test B', async done => {
    const logSpy = jest.spyOn(console, 'log');
    jest.spyOn(ClassName.prototype, 'methodName').mockImplementation(() => Promise.resolve(new Url(someUrl)));
    const wrapper = await getLoadedWrapperAsync();
    expect(blabla wrapper and logspy);
    done();
})

它单独运行成功,但当我一起运行时失败。另一个模拟返回无效的承诺值,另一个模拟返回有效的承诺值。

asynchronous testing jestjs mocking
© www.soinside.com 2019 - 2024. All rights reserved.