bulletproof-nodejs icon indicating copy to clipboard operation
bulletproof-nodejs copied to clipboard

Issue with Jest and Supertest

Open lennodev opened this issue 4 years ago • 3 comments

I am trying to use Jest and Supertest to test my endpoint.

I added server variable and module.export = server in app.ts, so supertest can retrieve the server to perform testing. However, I found the server is return before the dependency injection is ready. which cause an error TypeError: Cannot read property 'address' of null.

Could you please provide a sample of how to adopt jest and supertest with your design?

lennodev avatar Mar 30 '20 14:03 lennodev

Hello,

I have the same issue as you. I solved it by importing and running loaders in beforeAll clause instead of importing express app from app.js. I don't know how good of a solution this is, and I would appreciate some feedback from an experienced tester. I am using javascript code for tests, and still looking into how to support typescript. Here is an example:

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

let expressApp = null;

beforeAll(async (done)=> {
    expressApp = express();
    await require('../dist/loaders').default({ expressApp })
    done();
});

describe('Endpoints availability', () => {

  it('should return 404', async () => {
    const res = await request(expressApp)
      .get('/api/nonexisting')
      .send()
    expect(res.statusCode).toEqual(404)
  })
})

leonpahole avatar Mar 30 '20 15:03 leonpahole

@leonpahole maybe something like this?

import { default as request } from 'supertest'
import express from 'express'
import loaders from '../loaders'

const app = express()

beforeAll(async (done) => {
  await loaders({ expressApp: app })
  done()
})

describe('Endpoints availability', () => {
  it('should return 404', async () => {
    const res = await request(app).get('/api/nonexisting').send()
    expect(res.status).toEqual(404)
  })
})

canlopes avatar Oct 24 '20 19:10 canlopes

I am not sure whether this is the right way, but I created one index.test.js and called the other tests with app instance from there. I don't want the beforeAll in all test.js files.

const request = require('supertest');
const express = require('express');
const { setupDatabase } = require('../fixtures/db');
const loaders = require('../../src/loaders');
const conversationTests = require("./user/conversationTests.js")
const userCrudTests = require("./user/userCrud.js")

const app = express();

beforeAll(async (done) => {
    await loaders({ expressApp: app });
    done();
});
beforeEach(setupDatabase);

test("Should connect app", async () => {
    await request(app)
      .get('/status')
      .send()
      .expect(200);
})

conversationTests(app)
userCrudTests(app)

shanshaji avatar Apr 02 '21 23:04 shanshaji