简体   繁体   English

javascript 在组件中使用 jest 模拟导入

[英]javascript mock import in component with jest

any idea how to mock an “import” for testing?知道如何模拟“导入”进行测试吗? I use jest now.我现在用笑话。

ie:即:

//browser.js
export const browser = {
    id: undefined
};
export const getBrowser = function() {
    return browser;
};


//fetch-data.js
//code that uses the browser and I wanna test
import {getBrowser} from './../components/browser';

export const fetchData = function() {
     const browser = getBrowser();
     return Object.assign({dontcare:1}, browser);
};



//My test... Im using jest
import {fetchData} from './../fetch-data.js';
    expect(fetchData()).toBe({......});
};

now in the test file I want to mock the response of the browser component…现在在测试文件中我想模拟browser组件的响应......

Any ideas?有什么想法吗?

Thanks!谢谢!

Got to resolve it in the end with the help of one of the posts but in a different way with Sinnon.js最后必须在其中一篇文章的帮助下解决它,但使用 Sinnon.js 的方式不同

1) I import the browser component in my test: 1)我在测试中导入浏览器组件:

import * as browserModule from './../components/browser';

2) now in my tests I'll mock the methods I want: 2)现在在我的测试中,我将模拟我想要的方法:

 sinon.stub(browserModule, 'getBrowser').returns({id: 1});

3) all good from here, I call my test object and it gets the proper mocked responses :) 3)从这里开始一切都很好,我调用了我的测试对象,它得到了正确的模拟响应:)

you need to find a way to inject the the getBrowser function into the fetchData您需要找到一种方法将getBrowser函数注入到 fetchData 中

export const fetchData = function(injectedFunction) {
     const browser = injectedFunction !== undefined?  injectedFunction() || getBrowser();
     return Object.assign({dontcare:1}, browser);
};

this way you can now //your test... Im using jest这样你现在可以//你的测试......我在开玩笑

import {fetchData} from './../fetch-data.js';
import {getBrowser} from './../components/browser';
    let mockBrowser = mock(getBrowser);// <choose your mock library>
    // mock the behavior then inject it to the fetchData 
    expect(fetchData(mockBrowser)).toBe({......});
};

that's why it is good to apply dependency injection in your code to be able to test it with ease这就是为什么在您的代码中应用依赖注入以便能够轻松测试它的原因

for me you can use http://sinonjs.org/ for test spies, stubs and mocks对我来说,你可以使用http://sinonjs.org/来测试间谍、存根和模拟

Your function in fetch-data.js is dependent upon exports from browser.js .您在fetch-data.js函数依赖browser.js导出

You would find that browser and getBrowser both are undefined depending on your jest configuration .您会发现browsergetBrowser未定义,具体取决于您的jest 配置

Since browser.js is ready for export, you can use jest.requireActual(moduleName) to import and use that module in fetch-data.js .由于browser.js准备好导出,您可以使用jest.requireActual(moduleName)fetch-data.js导入和使用该模块。 I would do this in the following way:我会通过以下方式做到这一点:

jest.mock("./browser", () => {
    const br = jest.requireActual("browser");
    return { 
       ...br, 
       browser: {id:'changed value'} 
    };
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM