gpt4 book ai didi

javascript - 用 Jest 在 javascript 中模拟类原型(prototype)

转载 作者:行者123 更新时间:2023-11-30 14:04:08 26 4
gpt4 key购买 nike

我目前在一个项目中,我需要mock某个constructor class prototype 来运行测试所以它不直接处理我的数据库。我已经尝试研究并发现我需要将 moduleimport 到我的 mock 文件中,然后将其export 到我的test 文件并使用 jest.setMock() 替换我的 test.js 文件中的模块。以下是我尝试过的代码。

我有一个 Admin constructor class 在其 prototype 中有一个删除函数,负责从 db

admin.js

function Admin(username, email, password, access) {
Users.call(this, username, email, password, access);
}

Admin.constructor = Admin;

Admin.prototype.deleteSingleUser = function(id, accountType) {
this.id = id;
this.accountType = accountType;

let response = "";

if(this.accountType === "admin") {
for(let i in dbData.admin) {
if(dbData.admin[i].id === this.id) {
dbData.admin.splice(i, 1);
fs.writeFileSync('db.json', JSON.stringify(dbData, null, 2));
console.log(dbData.admin);
response = "Account successfully deleted";
}
else {
response = "There is no user registered with this ID";
}
}
}
else if(this.accountType === "user") {
for(let i in dbData.users) {
if(dbData.users[i].id === this.id) {
dbData.users.splice(i, 1);
fs.writeFileSync('db.json', JSON.stringify(dbData, null, 2));
console.log(dbData.users);
response = "Account successfully deleted";
}
else {
response = "There is no user registered with this ID";
}
}
}
else {
response = "Kindly use a valid account type";
}
console.log(response);
return response;
};

然后在我的 __mocks__ 目录中,我有一个 admin.js 文件,其中包含一个 mock 数据库和我的 mock删除代码

__mocks__/admin.js

//Imported the admin.js

let Admin = require("../admin");

//Created a mock database
let mockDb = {
"users": [
{
"id": 1,
"username": "aaa",
"email": "aaa@gmail.com",
"password": "aaa",
"access": "user"
}
],
"admin": [
{
"id": 1,
"username": "bbb",
"email": "bbb@gmail.com",
"password": "bbb",
"access": "admin"
}
]
}

//My mock delete function

Admin.prototype.deleteSingleUser = function(id, accountType) {
this.id = id;
this.accountType = accountType;

let response = "";

if (this.accountType === "admin") {
if(mockDb.admin[0].id === this.id) {
response = "Account successfully deleted";
}
else {
response = "There is no user registered with this ID";
}
}
else if (this.accountType === "user") {
if(mockDb.users[0].id === this.id) {
response = "Account successfully deleted";
}
else {
response = "There is no user registered with this ID";
}
}
console.log("Its using the mock");
return response;
};
module.exports = Admin;

然后在我的 __test__ 文件夹中我有一个包含我的测试用例的 test.js 文件

__test__/test.js

const Admin = require("../admin");
jest.setMock("Admin", require("../__mocks__/admin"));

test("Should check if admin can delete with a wrong id", () => {
let response = Admin.prototype.deleteSingleUser(0, "user");
expect(response).toBe("There is no user registered with this ID");
});
test("Should check if admin can delete a user", () => {
let response = Admin.prototype.deleteSingleUser(1, "user");
expect(response).toBe("Account successfully deleted");
});

两个测试用例都没有通过,但都抛出一个error saying

Test suite failed to run

Cannot find module 'Admin' from 'test.js'

2 | const Users = require("../main");

3 | const Admin = require("../admin");

4 | jest.setMock("Admin", require("../mocks/admin"));

| ^

5 | const order = require("../order");

6 | let mocks = require("../mocks/admin");

7 | let dbData = JSON.parse(fs.readFileSync('db.json'));

at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:229:17)

at Object.setMock (test/test.js:4:6)

我觉得我做的事情不对,但我似乎无法指出来。请问这是什么问题,我该如何解决?谢谢。

最佳答案

你根本不需要使用jest.setMock

您只需要激活 manual mock您通过这样做为您的用户模块创建:

jest.mock('../admin');  // <= activate the manual mock

详情

jest.setMock 的文档声明它只应在“极少数情况下”使用,当“即使手动模拟也不适合您的目的”时,并补充说“建议改用 jest.mock()

在这种情况下,您没有做任何需要 jest.setMock 的事情,因此最佳做法是使用 jest.mock 激活您的手动模拟。

关于javascript - 用 Jest 在 javascript 中模拟类原型(prototype),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55776228/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com