繁体   English   中英

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

[英]Testing Imported Function with Parameter using Jest Mock Function / Jest spyOn

我正在尝试使用 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(使用 required + 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"

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

我将参数存储在一个变量中,然后我使用该变量而不是直接使用字符串。

这是我如何编辑我的测试代码

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

暂无
暂无

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

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