亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用Jest模擬ES6模塊導入?

如何使用Jest模擬ES6模塊導入?

慕的地6264312 2019-12-09 10:16:46
我開始認為這是不可能的,但是無論如何我都想問。我想測試我的一個ES6模塊以特定方式調用另一個ES6模塊。有了茉莉花,這非常容易-應用程式碼:// myModule.jsimport dependency from './dependency';export default (x) => {  dependency.doSomething(x * 2);}和測試代碼://myModule-test.jsimport myModule from '../myModule';import dependency from '../dependency';describe('myModule', () => {  it('calls the dependency with double the input', () => {    spyOn(dependency, 'doSomething');    myModule(2);    expect(dependency.doSomething).toHaveBeenCalledWith(4);  });});笑話相當于什么?我覺得這很簡單,但是我一直想弄清楚我的頭發。我最接近的方法是將imports 替換為requires,然后將它們移入測試/函數中。都不是我想做的事情。// myModule.jsexport default (x) => {  const dependency = require('./dependency'); // yuck  dependency.doSomething(x * 2);}//myModule-test.jsdescribe('myModule', () => {  it('calls the dependency with double the input', () => {    jest.mock('../dependency');    myModule(2);    const dependency = require('../dependency'); // also yuck    expect(dependency.doSomething).toBeCalledWith(4);  });});為了獲得加分,我希望在其中的功能dependency.js為默認導出時使整個工作正常進行。但是,我知道監視默認導出在Jasmine中不起作用(或者至少我永遠無法使它起作用),因此我也不希望在Jest中也有可能。
查看完整描述

3 回答

?
慕婉清6462132

TA貢獻1804條經驗 獲得超2個贊

我已經能夠通過使用涉及到的黑客解決此問題import *。它甚至適用于命名和默認導出!


對于命名出口:


// dependency.js

export const doSomething = (y) => console.log(y)


// myModule.js

import { doSomething } from './dependency';


export default (x) => {

  doSomething(x * 2);

}


// myModule-test.js

import myModule from '../myModule';

import * as dependency from '../dependency';


describe('myModule', () => {

  it('calls the dependency with double the input', () => {

    dependency.doSomething = jest.fn(); // Mutate the named export


    myModule(2);


    expect(dependency.doSomething).toBeCalledWith(4);

  });

});

或默認導出:


// dependency.js

export default (y) => console.log(y)


// myModule.js

import dependency from './dependency'; // Note lack of curlies


export default (x) => {

  dependency(x * 2);

}


// myModule-test.js

import myModule from '../myModule';

import * as dependency from '../dependency';


describe('myModule', () => {

  it('calls the dependency with double the input', () => {

    dependency.default = jest.fn(); // Mutate the default export


    myModule(2);


    expect(dependency.default).toBeCalledWith(4); // Assert against the default

  });

});

正如Mihai Damian在下面正確指出的那樣,這是對的模塊對象進行了變異dependency,因此它將“泄漏”到其他測試中。因此,如果使用這種方法,則應存儲原始值,然后在每次測試后再次將其重新設置。要使用Jest輕松實現此目的,請使用spyOn()方法代替,jest.fn()因為它支持輕松恢復其原始值,因此避免了前面提到的“泄漏”。


查看完整回答
反對 回復 2019-12-09
?
紅顏莎娜

TA貢獻1842條經驗 獲得超13個贊

您必須模擬模塊并自己設置間諜:


import myModule from '../myModule';

import dependency from '../dependency';

jest.mock('../dependency', () => ({

  doSomething: jest.fn()

}))


describe('myModule', () => {

  it('calls the dependency with double the input', () => {

    myModule(2);

    expect(dependency.doSomething).toBeCalledWith(4);

  });

});


查看完整回答
反對 回復 2019-12-09
  • 3 回答
  • 0 關注
  • 1303 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號