I am using Jest to test and cover my express js code. I have a file default.js which is exporting a simple function:
async function foo(req, res) {
let out = {}
// performs some computations
res.send({
file: 'Default',
data: out.data
})
}
module.export = {foo}
This is then being called in app.js:
const def = require('./default')
app.get('/default/:a/:b' async (req, res) => {
def.foo(req, res)
})
This works fine. However, when I try to create a test on the Handler function in default.js Jest throws this error:
TypeError: def.foo is not a function
This is my test file:
const def = require('./default')
describe('Default Test', function() {
test('responds to /default/:a/:b', () => {
const req = {
params: {
a: 'country',
b: 'city'
}
}
const res = {
text: '',
send: function(input) {
this.text = input
}
}
def.foo(req, res)
expect(res.json).toEqual({
file: 'Default',
})
})
})
Never mind that the test should fail with the expect function. But the test is failing at default.foo it is not finding this function. Why is this?