小编Joh*_*ith的帖子

如何用 jest 模拟 pg-promise 库

我正在尝试模拟 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)

unit-testing promise pg-promise

5
推荐指数
1
解决办法
3300
查看次数

标签 统计

pg-promise ×1

promise ×1

unit-testing ×1