Jest模拟测试与输入

huangapple go评论66阅读模式
英文:

Jest mock testing with input

问题

// 为这个函数编写 jest 测试应该如何做?

const input = require('prompt-sync')();

export function choices(): void {
    const choice = input("Choose a letter");
    if (choice === "a") {
        console.log("Airplane");
    } if (choice === "b") {
        console.log("Balloon");
    } else {
        console.log("Neither");
    }
}

我知道我应该使用模拟 (mocking)。但是当输入存储在函数内而不是作为参数传递时,我不知道如何做。我不想改变这一点。

英文:

How can I write a jest test for this function?

const input = require('prompt-sync')();

export function choices(): void {
    const choice = input("Choose a letter");
    if (choice === "a") {
        console.log("Airplane");
    } if (choice === "b") {
        console.log("Balloon");
    } else {
        console.log("Neither");
    }

}

I know that I should use mocking. But I do not know how to do it when the input is being stored inside of the function, and not as a parameter. I do not want to change this.

答案1

得分: 0

你可以使用 jest.mock 来模拟你的 prompt-sync 模块。而且因为该模块返回一个返回你的 input 的函数,所以 Jest.mock 应该是这样的:

const mockInput = jest.fn();
jest.mock("prompt-sync", () => () => mockInput);

这应该在你的测试文件中的导入之前包含。

完整示例:

const mockInput = jest.fn();
jest.mock("prompt-sync", () => () => mockInput);

import { choices } from "../path/to/file";

describe("test", () => {
  let consoleSpy: jest.SpyInstance;

  beforeEach(() => {
    consoleSpy = jest.spyOn(console, "log");
  });

  afterEach(() => {
    jest.resetAllMocks();
  });

  it("should call the input", () => {
    choices();

    expect(mockInput).toHaveBeenCalledWith("Choose a letter");
  });

  it("should call A", () => {
    mockInput.mockReturnValue("a");

    choices();

    expect(consoleSpy).toHaveBeenCalledWith("Airplane");
  });
});

然而,我建议重构你的代码,以便更容易进行测试。

英文:

You can use jest.mock to mock your prompt-sync module. And since that module is returning a function that returns your input, Jest.mock would have something like:

const mockInput = jest.fn();
jest.mock("prompt-sync", () => () => mockInput);

Which should be included before the import in your test file.

Full example:

const mockInput = jest.fn();
jest.mock("prompt-sync", () => () => mockInput);

import { choices } from "../path/to/file";

describe("test", () => {
  let consoleSpy: jest.SpyInstance;

  beforeEach(() => {
    consoleSpy = jest.spyOn(console, "log");
  });

  afterEach(() => {
    jest.resetAllMocks();
  });

  it("should call the input", () => {
    choices();

    expect(mockInput).toHaveBeenCalledWith("Choose a letter");
  });

  it("should call A", () => {
    mockInput.mockReturnValue("a");

    choices();

    expect(consoleSpy).toHaveBeenCalledWith("Airplane");
  });
});

However I advice refactoring your code to make it easily testable

huangapple
  • 本文由 发表于 2023年3月4日 06:02:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75632241.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定