相关疑难解决方法(0)

Why does mutating a module update the reference if calling that module from another module, but not if calling from itself?

This question pertains to testing javascript and mocking functions.

Say I have a module that looks like this:

export function alpha(n) {
    return `${n}${beta(n)}${n}`;
}

export function beta(n) {
    return new Array(n).fill(0).map(() => ".").join("");
}
Run Code Online (Sandbox Code Playgroud)

Then I can't test it the following way:

import * as indexModule from "./index";

//Not what we want to do, because we want to mock the functionality of beta
describe("alpha, large test", () => {
    it("alpha(1) returns '1.1'", () => {
        expect(indexModule.alpha(1)).toEqual("1.1"); //PASS
    });

    it("alpha(3) …
Run Code Online (Sandbox Code Playgroud)

javascript jestjs es6-modules

5
推荐指数
1
解决办法
1392
查看次数

标签 统计

es6-modules ×1

javascript ×1

jestjs ×1