使用Jest Mock Function / Jest spyOn测试带有参数的导入函数

时间:2020-02-18 04:05:45

标签: javascript node.js unit-testing testing jestjs

我正在尝试使用Jest为Node.js项目的逻辑编写单元测试。 但是,大多数文档仅提供了导入模块或类的情况,但就我而言,我的模块仅包含函数。

到目前为止,我知道在Jest中测试功能的方法主要有以下三种:

1)jest.fn()

2)jest.spyOn

3)jest.mock('path')

我尝试了全部三个方法,但均无济于事。

我想测试函数在调用时是否返回正确的值(字符串)。 我尝试了许多不同的方式

这是我的代码:(我将在后面的部分中显示我的代码的简短摘要)

getDefApCode.ts

export function getDefApCode(code: string) {
  switch (code) {
    case 'BKK':
      return 'NRT'
    case 'CTX':
      return 'ICN'
    case 'SIN':
      return 'TPE'
    default:
      return code
  }
}

export function getDefaultDepartureCode(code: string) {
  return code ? getDefaultLocationCode(code) : 'LHR'
}

export function getDefaultDestinationCode(code: string) {
  return code ? getDefaultLocationCode(code) : 'ZRH'
}

getDefAPCode.spec.ts >>模式1(使用必填+ jest.fn)

import { Connection, getConnection, getConnectionOptions } from "typeorm";
import { bootstrap, dbConnection } from "../../../src/app";
import { TourSearchParamsFactory } from "../../helpers/typeOrmFactory";
import * as getDefAPCode from "../../../src/controllers/logic/getDefAPCode";

describe("Logic Test", () => {
  beforeAll(async () => {
    await dbConnection(15, 3000);
  });

  afterAll(async () => {
    const conn = getConnection();
    await conn.close();
  });

  it("should get a default location code", async () => {
  	const getLocation = require('../../../src/controllers/logic/getDefAPCode');
  	const code = jest.fn(code => 'BKK');
  	const getCode = getLocation(code);
  	expect(getCode).toHaveBeenCalled();

   });
 });

错误消息:

TypeError: getLocation is not a function

getDefAPCode.spec.ts >>模式2(使用spyON)

import { Connection, getConnection, getConnectionOptions } from "typeorm";
import { bootstrap, dbConnection } from "../../../src/app";
import { TourSearchParamsFactory } from "../../helpers/typeOrmFactory";
import * as getDefaultLocationCode from "../../../src/controllers/logic/getDefaultLocationCode";


describe("Logic Test", () => {
  beforeAll(async () => {
    await dbConnection(15, 3000);
  });

  afterAll(async () => {
    const conn = getConnection();
    await conn.close();
  });

  const { getDefaultLocationCode, getDefaultDepartureCode, getDefaultDestinationCode } = require('../../../src/controllers/logic/getDefaultLocationCode');
  
  it("should get a default location code", async () => {
  	const spy = jest.spyOn(getDefaultLocationCode, 'getDefaultLocationCode');
  	getDefaultLocationCode.getDefaultLocationCode('AKJ');
  	expect(spy).toHaveBeenCalled();
  });
 });

当我尝试使用其他模式时,会出现一些错误消息(我没有跟踪所有测试代码模式,一旦修复了docker,就会添加测试代码模式)

错误消息:

Cannot spy the getDefaultLocationCode property because it is not a function; undefined given instead

31 | const spy = jest.spyOn(getDefaultLocationCode,'getDefaultLocationCode');

过去的错误消息

error TS2349: This expression is not callable.
      Type 'typeof import("/app/src/controllers/logic/getDefAPCode")' has no call signatures.

另一个

expect(received).toHaveBeenCalled()

Matcher error: received value must be a mock or spy function

Received has type:  string
Received has value: "NRT"

1 个答案:

答案 0 :(得分:0)

我发现在这种情况下不必使用模拟功能。

我将参数存储在变量中,然后使用变量代替直接使用字符串。

这是我修改测试代码的方式

  it("should get a default location code", () => {
    const code = 'BKK';
    expect(code).toHaveBeenCalled();
   });