I want to unit test the exported method in the code below. I want to mock the values in the private method to control the reject/resolves of the returned Promise. client
is node-postgres object that is already connected to the database.
I know I can use proxyquire to stub out required libraries but how can I mock the chained methods .on('error', ...)
, .pipe(stream)
and .on('end', ...)
such that I can control the returned values.
Note the exported method shown is a simplification of the real one and exporting importDomain is not feasible.
const copyFrom = require('pg-copy-streams').from
const request = require('request')
const Promise = require('bluebird')
// private
function importDomain (client, domain) {
return new Promise((resolve, reject) => {
let stream = client.query(copyFrom(`COPY ${domain.table} FROM STDIN;`))
let req = request(`${domain.url}`)
req.on('error', reject)
req.pipe(stream)
.on('error', reject)
.on('end', resolve)
})
}
// public
module.exports = (client) => {
let domain = someFunctionReturningDomain()
importDomain(client, domain)
}