gpt4 book ai didi

javascript - 然后没有触发 Jasmine 的提取模拟

转载 作者:行者123 更新时间:2023-12-03 14:49:59 24 4
gpt4 key购买 nike

我有这个常数:

export const clientData = fetch(`${process.env.SERVER_HOST}clientData.json`)
.then(response => response.json());

哪个工作正常,现在我正在使用 Jasmine 和 fetch-mock 对此进行测试

这是我的测试:
import { clientData } from '../../../src/js/services/client-data.fetch';
import fetchMock from 'fetch-mock';

describe('test', () => {
const exampleResponse = {
clientData: 'test'
};

beforeAll(() => {
fetchMock.mock('*', exampleResponse);
});

it('ooo', () => {
console.log('here', clientData);
var a = clientData;
a.then(b=> console.log(b))
});
});
clientData的console.log返回 Promise (这很好),但是 then永远不会触发。

不明白为什么,我的代码有什么问题?

最佳答案

发生这种情况是因为测试执行本质上是同步的,并且它不会等待断言发生,因此您必须通过 done回调并从 then 内的测试中调用它打回来
像这样:

import { clientData } from '../../../src/js/services/client-data.fetch';
import fetchMock from 'fetch-mock';

describe('test', () => {
const exampleResponse = {
clientData: 'test'
};

beforeAll(() => {
fetchMock.mock('*', exampleResponse);
});

it('ooo', (done) => {
console.log('here', clientData);
var a = clientData;
a.then(b=> {
console.log(b);
done();
})
});
});

关于javascript - 然后没有触发 Jasmine 的提取模拟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45522202/

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