Saturday 24 November 2018

Cannot test a simple route with Jest

I have been trying to test this route for 4 hours now, and I can't seem to make it work.So i have a route like this:api.post('/register', (req, res) => { let hasErrors = false if(!req.body.name || !req.body.email || !req.body.password){ hasErrors = true } if(hasErrors){ res.status(errCode.invalid_input).json({ message: 'Invalid input' }) }else{ const NewUser = new User({ _id : mongoose.Types.ObjectId(), name: req.body.name, email: req.body.email, password: req.body.password }); NewUser.save().then(saved_user => { res.status(201).json({ message: 'User registered', user: saved_user }); }).catch(err => { res.status(500).json({ message: err }); }) } }) ...which I'm testing using Jest and Supertest:it('/register should return 201 if valid input', (done) => { //mock valid user input const NewUser = { 'name': 'John Wick', 'email': 'john@wick.com', 'password': 'secret' } request(app) .post('/register') .type('form') .send(NewUser) .expect(201) .end((err) => { if (err) return done(err) done() }) }) And the test is pending, and It gives me Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout.I have tested this /register endpoint manually with Postman, and indeed, when I call the /register endpoint with Form URL Encoded data (name, email, password), it works and adds a new entry in my database. If I send the data as Multipart Form, it doesn't work and gives a 422 Invalid Input. Maybe that's the cause of my problem?Cheers!

Submitted November 24, 2018 at 07:06PM by pythonistaaaaaaa

No comments:

Post a Comment