santiq / bulletproof-nodejs

Implementation of a bulletproof node.js API 🛡️
https://softwareontheroad.com/ideal-nodejs-project-structure/
MIT License
5.47k stars 1.15k forks source link

How would you test an express route with this app.ts structure ? #52

Open m4nu56 opened 4 years ago

m4nu56 commented 4 years ago

I've tried cutting the startServer() method with an expressApp() one that I can export:

export async function expressApp() {
  const app = express();
  await require('./loaders').default({ expressApp: app });
  return app;
}

async function startServer() {
  const app = await expressApp();
 ...
}

And in my test I try using it like I would normally do:

import request from 'supertest';
import { expressApp } from '../src/app';

describe('Analyze Movements', () => {
  test('succeeds list of analyze movements', async () => {
    const response = await request(expressApp)
      .get(`/analyze`)
      .expect(200);
    let body = response.body;
    expect(body.length).toBeGreaterThan(1);
  });
}

But I'm getting a SyntaxError: SyntaxError: Unexpected identifier queries.js:1 ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import moment from 'moment';

I suspect this is a problem related with the fact that my expressApp() method is async but I can't figure it out..

jacquesikot commented 3 years ago

use like this carbon