如何模拟一个带参数的模块
how to mock a module that takes a parameter
我正在尝试为使用 pg-promise 的代码编写单元测试,它看起来像这样:
const pgp = require('pg-promise')();
const cn = {
host: process.env.DB_HOST,
port: 5432,
database: process.env.DB_NAME,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD
};
function insertStuff(stuff) {
let db = pgp(cn);
return db.one('INSERT INTO test(stuff) VALUES () RETURNING id, stuff', [stuff])
.then(data => {
return data
})
}
module.exports.insertStuff = insertStuff
测试代码如下所示:
const mockFakeDb = {
one: jest.fn()
}
jest.mock("pg-promise", () => {
return mockFakeDb
})
const insertStuff = require("../src/db-utils").insertStuff
test("params for inserting stuff are correct", done => {
mockFakeDb.one.mockImplementationOnce(() => {
return Promise.resolve({id: 123456789, stuff: "stuff"})
insertStuff("stuff").then((data) => {
const insertCall = fakeDb.one.mock.calls[0]
expect(insertCall).toHaveBeenCalledTimes(1)
done()
})
})
因此在尝试模拟 pg-promise require 时出现错误:
TypeError: require(...) 不是函数。
我可以看到 pg-promise 有一个函数接受参数(第二个括号)但不确定现在如何模拟它?
对于其他不太确定如何执行此操作的人:
const fakeDB = {
one: jest.fn()
}
function fakePgpFunc() {
return fakeDB
}
fakePgpFunc.end = jest.fn()
jest.doMock("pg-promise", () => {
return jest.fn(() => fakePgpFunc)
})
const insertStuff = require("../src/db-utils").insertStuff
beforeEach(() => {
jest.clearAllMocks()
})
test("params for inserting stuff are correct", done => {
fakeDB.one.mockImplementationOnce(() => {
return Promise.resolve({"stuff": "Stuff", "id": 123456789})
})
insertStuff("Stuff").then((data) => {
expect(fakeDB.one).toHaveBeenCalledTimes(1)
const insertStuffCall = fakeDB.one.mock.calls[0]
expect(insertStuffCall[0]).toEqual("INSERT INTO test(stuff) VALUES () RETURNING id, stuff")
expect(queryInsertCall[1]).toEqual(["Stuff"])
expect(data).toEqual({id: 123456789, stuff: "Stuff"})
expect(fakePgpFunc.end).toHaveBeenCalledTimes(1)
done()
})
})
我正在尝试为使用 pg-promise 的代码编写单元测试,它看起来像这样:
const pgp = require('pg-promise')();
const cn = {
host: process.env.DB_HOST,
port: 5432,
database: process.env.DB_NAME,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD
};
function insertStuff(stuff) {
let db = pgp(cn);
return db.one('INSERT INTO test(stuff) VALUES () RETURNING id, stuff', [stuff])
.then(data => {
return data
})
}
module.exports.insertStuff = insertStuff
测试代码如下所示:
const mockFakeDb = {
one: jest.fn()
}
jest.mock("pg-promise", () => {
return mockFakeDb
})
const insertStuff = require("../src/db-utils").insertStuff
test("params for inserting stuff are correct", done => {
mockFakeDb.one.mockImplementationOnce(() => {
return Promise.resolve({id: 123456789, stuff: "stuff"})
insertStuff("stuff").then((data) => {
const insertCall = fakeDb.one.mock.calls[0]
expect(insertCall).toHaveBeenCalledTimes(1)
done()
})
})
因此在尝试模拟 pg-promise require 时出现错误:
TypeError: require(...) 不是函数。
我可以看到 pg-promise 有一个函数接受参数(第二个括号)但不确定现在如何模拟它?
对于其他不太确定如何执行此操作的人:
const fakeDB = {
one: jest.fn()
}
function fakePgpFunc() {
return fakeDB
}
fakePgpFunc.end = jest.fn()
jest.doMock("pg-promise", () => {
return jest.fn(() => fakePgpFunc)
})
const insertStuff = require("../src/db-utils").insertStuff
beforeEach(() => {
jest.clearAllMocks()
})
test("params for inserting stuff are correct", done => {
fakeDB.one.mockImplementationOnce(() => {
return Promise.resolve({"stuff": "Stuff", "id": 123456789})
})
insertStuff("Stuff").then((data) => {
expect(fakeDB.one).toHaveBeenCalledTimes(1)
const insertStuffCall = fakeDB.one.mock.calls[0]
expect(insertStuffCall[0]).toEqual("INSERT INTO test(stuff) VALUES () RETURNING id, stuff")
expect(queryInsertCall[1]).toEqual(["Stuff"])
expect(data).toEqual({id: 123456789, stuff: "Stuff"})
expect(fakePgpFunc.end).toHaveBeenCalledTimes(1)
done()
})
})