在jest中对函数进行更深层次的模拟。

huangapple go评论49阅读模式
英文:

Mocking functions at a very deeper level with jest

问题

我的文件结构如下。我需要模拟一个三级深的未导出函数。能有人帮我找出如何实现这个任务吗?我的目标不是将数据发送到数据库。


文件名 c.js

exports.c.function3 = {
dbCall.then((data)=>data);
}



文件名 b.js

require("./c");
exports.b.function2 = () => {
c.function3()
.then((data)=>data);
}



文件名 a.js

require("./b");
exports.function1 = () => {
b.function2()
.then((data)=>data);
}



文件名 a.test.js
require("./a");
describe("测试用例 a",() => {

test("function1应该返回来自数据库的数据",() => {
const dbCall = jest.spyOn(a,'function2.function3.dbCall');
dbCall.mockImplementation(() => [1,2,3]);
function1.then((data) => {
expect(data.length).toBe(3);
});
}
})


我尝试了多种方法来模拟这个函数,但都没有成功。非常感谢任何帮助。
英文:

my file structure looks like below. I needed to mock a function which is three level deeper and not exported. Can someone help me figure out how to achieve this task please. My goal is not to send data to database.

file name c.js

exports.c.function3 = {
dbCall.then((data)=>data);
}
file name b.js

require("./c");
exports.b.function2 = ()=>{
c.function3()
.then((data)=>data);
}
filename - a.js

require("./b");
exports.function1 = ()=>{
b.function2()
.then((data)=>data);
}
file name - a.test.js
require("./a");
describe("Test cases a",()=>{


test("function1 should return data from db",()=>{
const dbCall = jest.spyOn(a,'function2.function3.dbCall');
dbCall.mockImplementation(()=>[1,2,3]);
function1.then((data)=>{
expect(data.length).toBe(3);
});
}
})

I tried multiple ways to mock the function, but none worked. Any help would be highly appreciated. Thanks.

答案1

得分: 1

你可以使用jest.mock()在Jest中完成这个操作,不过这部分是代码,无需翻译。

关于"CJS"的部分,我无法提供精确翻译,因为它可能是某种特定的技术或缩写,而且不清楚上下文。

这段代码主要涉及对模块的模拟测试,确保在测试中使用模拟数据而不是实际数据库数据。最后的结果表明测试通过。

如果需要保留"C"模块的其余部分,你可以使用提供的另一种方式进行模拟测试。

英文:

You can do this in Jest with CJS by using jest.mock().

I do not understand your CJS imports and async flow 100%.
But this works, hope it helps:

c.js

const dbCall = () => {
    return new Promise((resolve, _reject) => resolve("Data from database"))
}

exports.function3 = () => {
    return dbCall().then((data) => data)
}
b.js

const c = require("./c");
exports.function2 = () => {
    return c.function3()
        .then((data) => data);
}

a.js

const b = require("./b");
exports.function1 = () => {
    return b.function2()
        .then((data) => data);
}

a.test.js

jest.mock('./c', () => {
    return {
        function3: jest.fn().mockReturnValue(
            new Promise((resolve, _reject) => resolve("Mocked data"))
        )
    };
});

const a = require("./a")

test("function1 should return data from db", () => {
    a.function1().then((data) => {
        expect(data).toBe("Mocked data");
    });
})

If you need the rest of c module intact, do like this:

jest.mock('./c', () => {
    const originalModule = jest.requireActual('./c')
    return {
        ...originalModule,
        function3: jest.fn().mockReturnValue(
            new Promise((resolve, _reject) => resolve("Mocked data"))
        )
    };
});

Result:


> 75891489@1.0.0 test
> jest

 PASS  ./a.test.js
  ✓ function1 should return data from db (2 ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        0.265 s, estimated 1 s
Ran all test suites.

node: v18.12.1
jest: 29.5.0

huangapple
  • 本文由 发表于 2023年3月31日 01:56:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75891489.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定