Endpoint code:
app.post('/api/v1/user', async function (req, res) {
const user = await createUser(req);
res.send(user);
});
Integration test:
Below test is written to test the endpoint (/api/v1/user). createUser(req); function creates the user into DB. I want to skip creation of user into DB while testing.
Could you help me to mock createUser(req); in the below test.
import chai from 'chai';
import chaiHttp from 'chai-http';
chai.use(chaiHttp);
import { app } from '../../server';
describe('Create user tests', function () {
it('user should be created', function (done) {
chai.request(app)
.post('/api/v1/user')
.set({
'content-type': 'application/json',
})
.send({
name: 'xxxxx',
email: '[email protected]',
})
.end((err, res) => {
res.should.have.status(200);
done();
});
});
});
in case you can replace your runner from
mochatojest, the following solution can work for you:in my example above i used the following server:
and the following
package.json