Using node-mocks-http package to test a next.js api route

Viewed 853

I'm working on an api route in a next.js app. The route posts to a mongodb atlas cluster via mongoose. I've tested the route with postman and it works properly, but I haven't been able to get a jest test to work. I'm using node-mocks-http to create a request and a response, passing them to the handler, then expecting the data to be on the response but I'm getting an empty string. I've tried using the ._getJSONdata method on response as well, but that gives an error: SyntaxError: Unexpected end of JSON input at JSON.parse ()

Here's the test:

import handler from '../../pages/api/apply';
import { createMocks } from 'node-mocks-http';
import regeneratorRuntime from 'regenerator-runtime';

describe('apply route', () => {
  it('posts an application to mongo and returns 200', async () => {
    const { req, res } = createMocks({
      method: 'POST',
      body: JSON.stringify(
        {
          form: {
            first: 'test',
            mi: 'String',
            last: 'String',
            pronoun: 'String',
            dob: new Date('2020-12-14T08:00:00.000Z'),
            email: 'String',
            phone: 'String',
            address: 'String',
            city: 'String',
            state: 'String',
            zip: 'String',
            country: 'String',
            source: 'String',
            veteran: true,
            hispanic: true,
            american_indian: true,
            asian: true,
            african_american: true,
            pacific: true,
            white: true,
            lgbtq: true,
            education: 'String',
            study: 'String',
            job: 'String',
            salary: 'String',
            cohort: 'String',
            experience: 'String',
            tech: 'String',
            interest: 'String',
            group: 'String',
            goals: 'String',
            financing: true,
            gi_bill: true,
            diversity_scholarship: true,
            wwcode_scholarship: true,
            pdxwit_scholarship: true,
            worksource: true
          },
          ip: 'String'
        }
      )
    });
    await handler(req, res);
    expect(res._getStatusCode()).toEqual(200);
    expect(res._getJSONData()).toEqual({
      form: {
        first: 'test',
        mi: 'String',
        last: 'String',
        pronoun: 'String',
        dob: new Date('2020-12-14T08:00:00.000Z'),
        email: 'String',
        phone: 'String',
        address: 'String',
        city: 'String',
        state: 'String',
        zip: 'String',
        country: 'String',
        source: 'String',
        veteran: true,
        hispanic: true,
        american_indian: true,
        asian: true,
        african_american: true,
        pacific: true,
        white: true,
        lgbtq: true,
        education: 'String',
        study: 'String',
        job: 'String',
        salary: 'String',
        cohort: 'String',
        experience: 'String',
        tech: 'String',
        interest: 'String',
        group: 'String',
        goals: 'String',
        financing: true,
        gi_bill: true,
        diversity_scholarship: true,
        wwcode_scholarship: true,
        pdxwit_scholarship: true,
        worksource: true
      },
      ip: 'String'
    });
  })
})

And here's the handler:

import { schema } from '../../lib/models/Application';
import { connect } from '../../utils/connect';
import mongoose from 'mongoose';
connect();

let Application;
try {
  Application = mongoose.model('Application');
}
catch {
  Application = mongoose.model('Application', schema);
}

export default (req, res) => {
  const body = JSON.parse(req.body);
  try {
    Application.create({...body})
      .then(application => {
        res.send(application)
      })
    res.statusCode = 200
  }

  catch(error) {
    console.error(error.message);
    res.statusCode = 500
  }
}

Thanks in advance! Also this is my first question, let me know if more info is needed!

0 Answers
Related