我无法模拟 Jest NodeJS 模块中的函数



我有一个用NodeJS创建的模块,在里面我调用一个名为existsObject的函数来检查谷歌存储中是否存在文件。

我正在用Jest 29.1.2测试我的应用程序,为了让它不真正执行这个功能并从存储中获取它,我试图创建一个Mock。

问题是,我尝试了无数种方式来模拟(包括spyOn(我在模块内调用的这个函数,但它无论如何都不遵守这一规则,总是直接从存储中查找。我不知道我做错了什么,但我会在下面发布我的代码:

storage.js

import config from '../config/config.js';
import { storage, bucket } from '../connections/storage.js';
async function existsObject (directory) {
const exists = await bucket.object(directory).exists();
return exists;
}
async function getObject (directory) {
const getObject = await bucket.object(directory).get();
return getObject;
}
async function  insertObject (localPath, remotePath) {
const insert = await bucket.object(remotePath).insertFile(localPath);
return insert;
}
async function insertObjectContent (content, remotePath) {
const insert = await storage.insert(content, config.bucket+'/'+remotePath);
return insert;
}
async function deleteObject (directory) {
const deleteObject = await bucket.object(directory).delete();
return deleteObject;
}
export { existsObject, getObject, insertObject, insertObjectContent, deleteObject };

existsPrivatekey.js(这是我想要测试的文件(

import config from '../config/config.js';
import fs from 'node:fs';
import { existsObject } from '../functions/storage.js';
export default async function (domain, where = 'local') {
if(where == 'local'){
if(fs.existsSync(config.certificates_dir + '/' + domain + '/' + config.certificates.private)){
return true;
}else{
return false;
}
}else{
if(await existsObject(domain + '/' + config.certificates.private)){
return true;
}else{
return false;
}
}
}

existsPrivatekey.spec.js(测试文件(

import { jest } from '@jest/globals';
import existsPrivatekey from '../../helpers/existsPrivatekey.js';
describe('[existsPrivatekey] Check if exists privatekey', () => {
it('Exists privatekey in Storage', async () => {
/* Mock tentativa 1 */
const existsObject = jest.fn();
existsObject.mockReturnValue(true);
jest.mock('../../functions/storage.js', () => {
return existsObject;
});


const existsPrivatekeyResult = await existsPrivatekey('meudominio.com.br', 'bucket');
expect(existsPrivatekeyResult).toBe(true);
});
it('Not Exists privatekey in Storage', async () => {
const existsPrivatekeyResult = await existsPrivatekey('meudominio.com.br', 'bucket');
expect(existsPrivatekeyResult).toBe(false);
});
});

我试过其他几种可能性,但都没有成功。我需要在这方面的帮助来继续我的测试,而不需要它,这取决于函数的实际结果。

mock应该返回带有方法(模块(的对象,而您只是返回一个mock函数。

试试这个(并在导入后在测试之外定义它(:

jest.mock('../../functions/storage.js', () => {
const originalModule = jest.requireActual('../../functions/storage.js');
return {
...originalModule, // if you need other original methods from this module 
existsObject: jest.fn().mockResolvedValue(true), // because the existsObject is an async method use here `mockResolvedValue`
}
});

如果你想更改模拟existsObject的值,那么你可以这样定义模拟:

const mockExistsObject = jest.fn().mockResolvedValue('test');
jest.mock('../../functions/storage.js', () => {
const originalModule = jest.requireActual('../../functions/storage.js');
return {
...originalModule, // if you need other original methods from this module 
existsObject: () => mockExistsObject(),
}
});

所以整个模拟和更新的测试看起来是这样的:

const mockExistsObject = jest.fn().mockResolvedValue(false);
jest.mock("../../functions/storage.js", () => {
const originalModule = jest.requireActual("../../functions/storage.js");
return {
...originalModule,
existsObject: () => mockExistsObject(),
};
});
describe("[existsPrivatekey] Check if exists privatekey", () => {
it("Exists privatekey in Storage", async () => {
mockExistsObject.mockResolvedValue(true);
const existsPrivatekeyResult = await existsPrivatekey(
"meudominio.com.br",
"bucket"
);
expect(existsPrivatekeyResult).toBe(true);
});
it("Not Exists privatekey in Storage", async () => {
mockExistsObject.mockResolvedValue(false);
const existsPrivatekeyResult = await existsPrivatekey(
"meudominio.com.br",
"bucket"
);
expect(existsPrivatekeyResult).toBe(false);
});
});

最新更新