Jest - 在一个测试套件中多次测试模块
Jest - Testing Module Multiple Times in One Test Suite
我有一个 TypeScript 模块(应该是无关紧要的,因为我认为这也会影响 JS)并且我正在尝试测试我拥有的一个模块。该模块从外部文件导入大量数据,并根据a变量选择返回哪些数据。
我正在尝试 运行 一些测试,我更新了那个变量,重新 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
});
});
对 jest.resetModules()
的 beforeEach()
调用确保我们是 re-running 模块中的代码。
我有一个 TypeScript 模块(应该是无关紧要的,因为我认为这也会影响 JS)并且我正在尝试测试我拥有的一个模块。该模块从外部文件导入大量数据,并根据a变量选择返回哪些数据。
我正在尝试 运行 一些测试,我更新了那个变量,重新 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
});
});
对 jest.resetModules()
的 beforeEach()
调用确保我们是 re-running 模块中的代码。