簡體   English   中英

在玩笑測試中模擬節點模塊

[英]Mocking a node module in a jest test

我有一個 Jest 測試,我正在為一個進行 API 調用的函數編寫。 如果 API 調用返回 403,則應調用來自節點模塊的函數。 我正在嘗試使用模擬 jest 函數對此進行測試,但是我無法使用我正在制作的模塊的模擬版本進行測試。

文件.spec.js

import file from './file'

const mockedNodeModule = jest.genMockFromModule('nodeModule')
mockedNodeModule.namedExport = { logout: jest.fn() }

it('call returns a 403', async () => {
      await file.apiCall('brand', 'entityType', 'name')
      expect(mockedNodeModule.namedExport.logout).toHaveBeenCalled()
})

文件.js

import { namedExport } from './nodeModule';
import api from './api';

const apiCall = () => {
  return api.makeCall().then(
    () => {},
    (error) => {
      if (error.status === 403) {
        namedExport.logout();
      }
    },
  );
};

export default { apiCall };

當我檢查是否調用了 mockedNodeModule.namedExport.logout 時,測試總是失敗。 當我在它被調用的行上放置一個斷點時,似乎在測試運行時沒有使用模擬版本(即它仍在使用我的 node_modules 中的模塊)。 我也嘗試過使用 jest.mock() ,但結果是一樣的。 我設置測試的方式有問題嗎? 在這種情況下,可以開玩笑不模擬節點模塊嗎?

jest.mock(moduleName, factory, options)應該可以工作。

例如

file.js

import { namedExport } from './nodeModule';
import api from './api';

const apiCall = () => {
  return api.makeCall().then(
    () => {},
    (error) => {
      if (error.status === 403) {
        namedExport.logout();
      }
    },
  );
};

export default { apiCall };

api.js :

function makeCall() {}

export default { makeCall };

nodeModule.js :

export const namedExport = {
  logout() {
    console.log('real implementation');
  },
};

file.test.js :

import file from './file';
import api from './api';
import { namedExport } from './nodeModule';

jest.mock('./nodeModule', () => {
  const mNamedExport = {
    logout: jest.fn(),
  };
  return { namedExport: mNamedExport };
});

jest.mock('./api', () => {
  return { makeCall: jest.fn() };
});

describe('59831697', () => {
  afterEach(() => {
    jest.clearAllMocks();
  });
  it('should handle error if http status equal 403', async () => {
    const mError = { status: 403 };
    api.makeCall.mockRejectedValueOnce(mError);
    await file.apiCall();
    expect(namedExport.logout).toHaveBeenCalledTimes(1);
  });
});

帶有覆蓋率報告的單元測試結果:

 PASS  src/stackoverflow/59831697/file.test.js (13.506s)
  59831697
    ✓ should handle error if http status equal 403 (7ms)

----------|----------|----------|----------|----------|-------------------|
File      |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files |      100 |       50 |    66.67 |      100 |                   |
 file.js  |      100 |       50 |    66.67 |      100 |                 8 |
----------|----------|----------|----------|----------|-------------------|
Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        15.448s

源代碼: https : //github.com/mrdulin/jest-codelab/tree/master/src/stackoverflow/59831697

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM