首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >fetch-mock with jasmine not then

fetch-mock with jasmine not then
EN

Stack Overflow用户
提问于 2017-08-05 20:59:25
回答 1查看 913关注 0票数 6

我有一个常量:

代码语言:javascript
复制
export const clientData = fetch(`${process.env.SERVER_HOST}clientData.json`)
    .then(response => response.json());

现在,我正在使用Jasmine和fetch-mock对其进行测试

这是我的测试:

代码语言:javascript
复制
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从未被触发过。

不知道为什么,我的代码出了什么问题?

EN

回答 1

Stack Overflow用户

发布于 2020-12-29 19:01:28

之所以会发生这种情况,是因为测试执行本质上是同步的,并且它不等待断言发生,因此您必须传递一个done回调,并在then回调中从测试中调用它

如下所示:

代码语言:javascript
复制
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();
        })
    });
});
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45522202

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档