Reputation: 19284
I have this simple method that i'm trying to test.
function isS3Healthy(s3, logger = console) {
return new Promise((resolve, reject) => {
s3.listObjects({}, (err, data) => {
if (err) {
logger.error(err)
return resolve(['s3', false])
}
return resolve(['s3', true])
})
})
}
My test keeps timing out and it has to do with my mocked object isn't right. What does this mock object need to look like in order to test my method properly?
describe.only('#isS3Healthy', () => {
let s3
before(() => {
s3 = {
listObjects: ({}, (err, data) => {
return Promise.resolve(['s3', true])
})
}
})
it('should return that it is healthy', () => {
return isS3Healthy(s3)
.then(result => {
const [name, status] = result
expect(name).to.equal('s3')
expect(status).to.be.true
})
.catch(err => {
console.log('err = ', err)
})
})
})
Upvotes: 0
Views: 212
Reputation: 101652
Based on the code you've provided, it looks like listObjects
is a function that accepts an object and a callback, and at some point calls that callback, so probably the simplest way to mock it would be like this:
listObjects: (_, c) => c()
The function you're testing only seems to care whether listObjects
is passing an error to the callback or not, so this should seemingly be sufficient.
Side note: you have return resolve
in two places in your isS3Healthy
function, but using return
here almost certainly serves no purpose. To resolve a promise, just call resolve(...)
without returning it.
Upvotes: 1