Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/428.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Jest-模拟非默认导入函数_Javascript_Reactjs_Unit Testing_Jestjs - Fatal编程技术网

Javascript Jest-模拟非默认导入函数

Javascript Jest-模拟非默认导入函数,javascript,reactjs,unit-testing,jestjs,Javascript,Reactjs,Unit Testing,Jestjs,我需要用Jest模拟React应用程序(使用create React app创建)中导入的函数。我尝试了在文档和其他线程中显示的操作(例如,here:),但没有成功。目前,我的项目如下所示: 调用getOne.js import {getOne} from "../../Services/articleService"; export const a = async function () { return getOne("article1");

我需要用Jest模拟React应用程序(使用create React app创建)中导入的函数。我尝试了在文档和其他线程中显示的操作(例如,here:),但没有成功。目前,我的项目如下所示:

调用getOne.js

import {getOne} from "../../Services/articleService";

export const a = async function () {
    return getOne("article1");
};
export const getOne = async function (id) {
    return;
};
import {ArticleTypes} from "../../Data/articleTypes";
import {getOne} from "../../Services/articleService";
import {a} from "./calls getOne";

jest.mock("../../Services/articleService", () => {
    return {
        getOne: jest.fn().mockImplementation(async (id) => {
            const articles = {
                article1: {
                    title: "Some valid title",
                    description: "Some valid description",
                    type: /*ArticleTypes.General*/"a"
                }
            };

            return Promise.resolve(articles[id]);
        })
    };
});

const mockedGetOne = async (id) => {
    const articles = {
        article1: {
            title: "Some valid title",
            description: "Some valid description",
            type: ArticleTypes.General
        }
    };

    return Promise.resolve(articles[id]);
};

beforeAll(() => {
    getOne.mockImplementation(mockedGetOne);
});

test("calls getOne", async () => {
    const res = await a();

    expect(getOne).toHaveBeenCalled();
    expect(res).not.toBeUndefined();
});
articleService.js

import {getOne} from "../../Services/articleService";

export const a = async function () {
    return getOne("article1");
};
export const getOne = async function (id) {
    return;
};
import {ArticleTypes} from "../../Data/articleTypes";
import {getOne} from "../../Services/articleService";
import {a} from "./calls getOne";

jest.mock("../../Services/articleService", () => {
    return {
        getOne: jest.fn().mockImplementation(async (id) => {
            const articles = {
                article1: {
                    title: "Some valid title",
                    description: "Some valid description",
                    type: /*ArticleTypes.General*/"a"
                }
            };

            return Promise.resolve(articles[id]);
        })
    };
});

const mockedGetOne = async (id) => {
    const articles = {
        article1: {
            title: "Some valid title",
            description: "Some valid description",
            type: ArticleTypes.General
        }
    };

    return Promise.resolve(articles[id]);
};

beforeAll(() => {
    getOne.mockImplementation(mockedGetOne);
});

test("calls getOne", async () => {
    const res = await a();

    expect(getOne).toHaveBeenCalled();
    expect(res).not.toBeUndefined();
});
testss.test.js

import {getOne} from "../../Services/articleService";

export const a = async function () {
    return getOne("article1");
};
export const getOne = async function (id) {
    return;
};
import {ArticleTypes} from "../../Data/articleTypes";
import {getOne} from "../../Services/articleService";
import {a} from "./calls getOne";

jest.mock("../../Services/articleService", () => {
    return {
        getOne: jest.fn().mockImplementation(async (id) => {
            const articles = {
                article1: {
                    title: "Some valid title",
                    description: "Some valid description",
                    type: /*ArticleTypes.General*/"a"
                }
            };

            return Promise.resolve(articles[id]);
        })
    };
});

const mockedGetOne = async (id) => {
    const articles = {
        article1: {
            title: "Some valid title",
            description: "Some valid description",
            type: ArticleTypes.General
        }
    };

    return Promise.resolve(articles[id]);
};

beforeAll(() => {
    getOne.mockImplementation(mockedGetOne);
});

test("calls getOne", async () => {
    const res = await a();

    expect(getOne).toHaveBeenCalled();
    expect(res).not.toBeUndefined();
});
我目前已经在jest.mock中使用的工厂函数中注释掉了mock中的ArticleTypes。我需要使用它们,但无法在factory函数中使用导入的文件。我只是想测试嘲弄在那里是否有效,但它在任何地方都不起作用


为什么我不能嘲笑进口?我错过什么了吗?我是否需要一些我不知道的其他配置?

您在运行测试时是否遇到任何错误,或者它是否无法按预期工作?如果有错误,请共享错误消息。另外,我无法理解对
getOne
方法的2个mock实现的需求。谢谢,但我已经解决了。出于某种原因,mock在beforeAll和jest.mock中不起作用,但它们在beforeach中起作用。但我不知道为什么。快速指出您在这里使用的测试策略。理想情况下,您的模块应该有单独的测试文件,并测试各个功能。因此,可以为
articleService.js
添加一个单独的测试文件来测试
getOne
方法,并为
调用getOne.js
添加一个单独的测试文件,该文件将断言使用正确的参数调用
getOne
。不说你在做什么是错误的,但如果你试图测试从源文件中调用的方法,在更复杂的文件中这可能会有点困难。实际上,我不是在测试要调用的正确参数,而是在我的真实代码中,它在useEffect钩子中调用getOne,这就是我正在测试的——组件是否正常工作——因此我不得不模拟getOne以避免它从实际服务器获取请求。