繁体   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