'Jest Mock Errors with ES Modules for i18next

I have problems configuring mocks for i18next. I'm running Jest with node's --experimental-vm-modules flag. (calling node_modules/jest/bin/jest.js).

this Error happens: Must use import to load ES Module: /App/Mocks/i18nextMock.js

my test configurations, suits & mocks are as follows:

jest.config.js

export default async () => {
    return {
        verbose: true,
        testEnvironment:'jsdom',
        transform: {
            '\\.jsx?$': ['babel-jest', { configFile: '<rootDir>/Command/Test/BabelConfig.json') }],
        },
        moduleNameMapper: {
            "^.+\\.(css|less|scss|sass)$": "babel-jest",
            "i18next": '<rootDir>/App/Mocks/i18nextMock.js'),
        },
        setupFiles: ['<rootDir>/Command/Test/EnzymeConfig.js')],
        globals: {
            NODE_ENV: "test"
        },
    };
}; 

BabelConfig.json

{
    "presets": [
        ["@babel/preset-env", {
            "targets": { "browsers": ["last 2 chrome versions"] },
            "useBuiltIns": "usage",
            "corejs": "3.6.5"
        }],
        "@babel/preset-react"
    ],
    "plugins": [
        ["@babel/plugin-proposal-decorators", { "legacy": true }],
        "@babel/plugin-proposal-optional-chaining",
        ["@babel/plugin-proposal-class-properties", { "loose": true }],
        "@babel/plugin-syntax-dynamic-import"
    ]
}

/App/Mocks/i18nextMock.js

import { jest } from "@jest/globals";

const i18next = jest.createMockFromModule("i18next");

// this is all I need from i18next for now
i18next.getFixedT = () => () => "";


export default i18next;

my test suite for store.js which uses i18next:

sotre.test.js

import store from "../store";

jest.mock("i18next");

describe("A suite for store", () => {
    it("should create new store", () => {
        const store = new store();
        expect(store).toBeInstanceOf(store);
    });
});

store.js

import i18next from 'i18next';

class store {
    //...
    constructor(){
      this.t = i18next.getFixedT(null, "myPageDictionary");
      this.name = this.t("sample message");
      //...
    }
    //...
}
export default store;

again as mentioned above, when running npm run test I get the following Error:

Must use import to load ES Module: /App/Mocks/i18nextMock.js

I've tried changing the way I export in i18nextMock.js

from export default i18next;

to module.exports = i18next;

but the Error changes to:

SyntaxError: The requested module 'i18next' does not provide an export named 'default'

I've also tried to create a manual-mock by avoiding jest.createMockFromModule which was to no avail as well.

the following change

i18nextMock.js

- const i18next = jest.createMockFromModule("i18next"); // removed
+ const i18next = {}; // added

i18next.getFixedT = () => () => "";

export default i18next;

caused the following Error:

ReferenceError: require is not defined

I'm using ESModules in all parts of my project & I've decided to use --experimental-vm-modules flag to run test commands. However, mocking third party libs like this one seems to be troublesome.

I would appreciate any help or workarounds for this to get rid of Errors & make jest work correctly by mocking external libs. Thanks in advance.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source