我有这个常数:
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/