Sunday, 14 October 2018

Writing tests for Express CRUD operations using Supertest / Mocha / Should [help]

Hello Node!​I am very new to testing and am having some difficulties writing tests for my Express server. Here is a small sample from my test code. I am using Supertest, Mocha, and Should to perform these tests against my MongoDB database. My goal in the first test is to successfully create a new User, and then immediately delete it but I keep receiving an error. Some help would be really appreciated!​// users.test.jsrequire('should'); const request = require('supertest'); const routes = require('../../../server/index'); const User = require('../../../server/models/user'); describe('The User service', () => { // Register a User describe('Register a User', () => { it('should return HTTP 200 OK - { token } - Create a User', (done) => { var newUser = { email: "test@gmail.com", password: "lolomg123", subscribe: false }; request(routes) .post('/users/signup') .send( newUser ) .expect( 200 ) .end( function(err, res) { if(err) return done(err); res.body.should.have.property('token'); User.findOneAndDelete( { email: 'test@gmail.com' }, function (err) { if(err) return done(err); console.log('test@gmail.com deleted.'); return; }); return done(); }); }); it('should return HTTP 400 Bad Request - ValidationError (Joi) - Invalid body', (done) => { request(routes) .post('/users/signup') .expect(400) .end( (err, res) => { if(err) return done(err); res.body.error.name.should.equal('ValidationError'); return done(); }); }); it('should return HTTP 403 Forbidden - Email is already in use (Passport-local)', (done) => { // Point to a user that definitely already exists let body = { email: "emailthatexists@gmail.com", password: "asdfasdf", subscribe: false }; request(routes) .post('/users/signup') .send( body ) .expect(403) .end( (err, res) => { if(err) return done(err); res.body.error.should.equal('Email is already in use.'); return done(); }); }); }); }); I keep getting this errorhttps://i.redd.it/35c0g31ch6s11.pngCheers.

Submitted October 14, 2018 at 05:32PM by Hydrotechnics

No comments:

Post a Comment