我正在尝试模拟 pg Promise 库。我希望能够模拟返回,无论承诺被拒绝还是解决。这是一个示例函数和测试:
const pgp = require('pg-promise')({});
const someFunc = callback => {
const db = pgp('connectionString');
db
.none('create database test;')
.then(() => {
callback(null, 'success');
})
.catch(err => {
callback(err);
});
};
module.exports = {
someFunc
};
Run Code Online (Sandbox Code Playgroud)
我想像这样测试它:
const { someFunc } = require('./temp');
let pgp = require('pg-promise')({
noLocking: true
});
// HOW TO MOCK?
describe('test', () => {
beforeEach(() => {
jest.resetModules();
jest.resetAllMocks();
});
it('should test', () => {
let db = pgp('connectionString');
// how to mock …Run Code Online (Sandbox Code Playgroud)