Search code examples
mongodbroutesfunctional-testinghapi.jssupertest

Testing POST with supertest still creates new document


I have written a service called bookService and am currently having the issue that whenever I test a POST request in Hapi (v17) using Supertest and Expect, the test is creating a new record in the database, so it's not actually wrapping the application that I've imported.

I'm currently importing the bookService into server.js, a snippet of my route in server.js looks as follows:

server.js

const Hapi = require('hapi');
const mongoose = require('mongoose');
const bookService = require('./book-service');

server.route({
  method: 'POST',
  path: '/books',
  handler: bookService.createBook
});

const init = async () => {
  await server.start();
  mongoose.connect(MongoDBUrl, { useNewUrlParser: true }).then(() => {
    console.log(`Connected to Mongo server at ${MongoDBUrl}`)
  }, err => {
    console.log(err)
  });
};

init();

module.exports = { server };

The following snippet shows the function that bookService makes for the POST request:

bookService.js

const { Book } = require('./models/book');

const createBook = (req, h) => {
  const book = new Book({
    author: req.payload.author,
    title: req.payload.title,
    year: req.payload.year
  });

  return Book.create(book).then((book) => {
    return h.response({
      message: "Book created successfully",
      book
    }).code(200);
  }).catch((err) => {
    return {err};
  });
};

module.exports = { createBook };

I'm using server.listener as the argument for supertest's 'request' library and am still having this issue. My test is passing however it actually adds a new book document to Mongodb whenever I run the test. Are there any gotcha's that I'm not aware of with Supertest and Hapi?

server.test.js

const expect = require('expect');
const request = require('supertest');

const { server } = require('../server/server');

describe('POST /books', () => {
  it('should create a new book', (done) => {
  let book = {
    author: "Steinbeck",
    title: "Grapes of Wrath",
    year: "1939"
  };

  request(server.listener)
    .post('/books')
    .send(book)
    .expect(200)
    .expect((res) => {
      expect(res.body.book.author).toBe(book.author);
      expect(res.body.book.title).toBe(book.title);
      expect(res.body.book.year).toBe(book.year);
    })
    .end(done);
  });
});

Thank you in advance for any help.


Solution

  • lit,

    A new book created in your case is expected in integration testing. That's how it works and it always creates a new book in your mongodb.

    If you are expecting to not change database, perhaps you can try unit test which should mock the database functionalities so it can prevent a new book created.