繁体   English   中英

Jest - 在一个测试套件中多次测试模块

[英]Jest - Testing Module Multiple Times in One Test Suite

我有一个TypeScript模块(应该是无关紧要的,因为我认为这也会影响JS),我正在尝试测试我拥有的模块。 模块从外部文件导入大量数据,并根据变量选择应返回哪些数据。

我正在尝试运行一些测试,我更新该变量,重新require模块并在一个文件中运行进一步的测试。 但我的问题是文件的require只运行一次。 我猜它正在被缓存。 是否有可能告诉Jest的require函数不缓存或清除测试之间的缓存?

这是我正在尝试实现的一些剥离代码:

module.ts

import { getLanguage } from "utils/functions";

import * as messagesEn from "resources/translations/en";
import * as messagesFr from "resources/translations/fr";

// Determine the user's default language.
const language: string = getLanguage();

// Set messages based on the language.
let messages: LocaleMessages = messagesEn.default;
if (languageWithoutRegionCode === "fr") {
    messages = messagesFr.default;
}

export { messages, language };

test.ts

import "jest";

// Mock the modules
const messagesEn = { "translation1": "English", "translation2": "Words" };
const messagesFr = { "translation1": "Francais", "translation2": "Mots" };
const getLangTest = jest.fn(() => "te-ST");
const getLangEn = jest.fn(() => "en-GB");
const getLangFr = jest.fn(() => "fr-FR");
jest.mock("resources/translations/en", () => ({"default": messagesEn}));
jest.mock("resources/translations/fr", () => ({"default": messagesFr}));
jest.mock("utils/functions", () => ({
        getLanguage: getLangTest
    })
);

describe("Localisation initialisation", () => {
    it("Sets language", () => {
        const localisation = require("./localisation");
        expect(getLangTest).toHaveBeenCalled();
        expect(localisation.language).toEqual("te-ST");
        expect(localisation.messages).toEqual(messagesEn);
    });

    it("Sets english messages", () => {
        // THIS GETS THE MODULE FROM THE CACHE
        const localisation = require("./localisation");
        expect(getLangEn).toHaveBeenCalled();
        expect(localisation.language).toEqual("en-GB");
        expect(localisation.messages).toEqual(messagesEn);
    });

    it("Sets french messages", () => {
        // THIS GETS THE MODULE FROM THE CACHE
        const localisation = require("./localisation");
        expect(getLangFr).toHaveBeenCalled();
        expect(localisation.language).toEqual("fr-FR");
        expect(localisation.messages).toEqual(messagesFr);
    });
});

我知道第二次和第三次测试无论如何都不会起作用,因为我需要更新"utils/functions"模拟器。 问题是module.ts中的代码只运行一次。

所以,非常感谢Discord上的Jest人。 可以使用jest.resetModules()函数从缓存中清除模块。

所以我的test.ts文件将如下所示:

describe("Localisation initialisation", () => {
    beforeEach(() => {
        jest.resetModules();
    });

    it("Sets language", () => {
        const localisation = require("./localisation");
        // Perform the tests
    });

    it("Sets english messages", () => {
        const localisation = require("./localisation");
        // Perform the tests
    });

    it("Sets french messages", () => {
        const localisation = require("./localisation");
        // Perform the tests
    });
});

beforeEach()调用jest.resetModules()确保我们重新运行模块中的代码。

暂无
暂无

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

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