我有一个常量:
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从未被触发过。
不知道为什么,我的代码出了什么问题?
发布于 2020-12-29 19:01:28
之所以会发生这种情况,是因为测试执行本质上是同步的,并且它不等待断言发生,因此您必须传递一个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();
})
});
});https://stackoverflow.com/questions/45522202
复制相似问题