Jest - testing module several times in one test suite

I have a TypeScript module (should be irrelevant, since I think it affects JS too), and I'm trying to test the module I have. The module imports a large amount of data from external files and selects which data should be returned based on the variable.

I am trying to run some tests, where I update this variable, re- requiremodule and run additional tests in a single file. But my problem is that the requirefile only works once. I assume it is cached. Is it possible to tell Jest functions requirenot to cache or clear the cache between tests?

Here is some discarded code of what I'm trying to achieve:

module.ts

import { getLanguage } from "utils/functions";

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

// Determine the user 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 .

+4
1

, Discord. 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() , .

+9

Source: https://habr.com/ru/post/1666538/


All Articles