I am trying to run a test case using mocha and chai. I've followed a few different examples to get things up and running but I have hit a wall in running the tests. It seems that the 'should' part which I have imported from mocha is not getting instantiated. Any help is appreciated.

app.ts

import * as Koa from 'koa';
var app = new Koa();
module.exports = app;

server.ts

import * as bodyParser from 'koa-bodyparser';
import * as session from 'koa-session';
import * as passport from 'koa-passport';

import { databaseInitializer } from '../src/initializers/database';
import { logger } from '../logging';
import { config } from '../config';

//import the various routers that we have setup
import {qaRouter} from 'routes/qa-routes'
import {restRouter} from 'routes/rest-routes'
import {graphqlRouter} from 'routes/graphql-routes';


//import the koa app
var app = require('./app');

const bootstrap = async () => {
    await databaseInitializer();

    // Enable bodyParser which is needed to work with information passed to the server from the client requests 
    app.use(bodyParser());

    // sessions
    app.keys = ['super-secret-key'];
    app.use(session(app));

    // authentication
    require('./auth');
    app.use(passport.initialize());
    app.use(passport.session());

    app.use(async (ctx, next) => {
        // Log the request to the console
        console.log('Url:', ctx.url);
        // Pass the request to the next middleware function
        await next();
    });

    //tell the app to use teh routers that we imported
    app.use(logger);
    app.use(graphqlRouter.routes(), graphqlRouter.allowedMethods())
    app.use(qaRouter.routes(), qaRouter.allowedMethods())
    app.use(restRouter.routes(), restRouter.allowedMethods())

};

bootstrap();

// needed for testing porpoises only
module.exports = app.listen(config.port), err=>{
    if(err) console.error(err);
    console.log(`Server listening on port ${app.get('port')}...`);
  };

routes.auth.test.ts

process.env.NODE_ENV = 'test';

import chai = require('chai');
import {describe, should} from 'mocha'
import chaiHttp = require('chai-http');

chai.use(chaiHttp);

const server = require('../src/server');

  describe('GET /auth/register', () => {
      it('should render the register view', (done) => {
        chai.request(server)
        .get('/auth/register')
        .end((err, res) => {
          should.not.exist(err);
          //res.redirect.length.should.eql(0);
          res.status.should.eql(200);
          res.type.should.eql('text/html');
          res.text.should.contain('<h1>Register</h1>');
          res.text.should.contain(
            '<p><button type="submit">Register</button></p>');
          done();
        });
      });
    });

Output

[email protected]:/mnt/c/Users/micha/github/n4nite-api$ npm test

> [email protected] test /mnt/c/Users/micha/github/n4nite-api
> cross-env NODE_PATH=./src mocha -r ts-node/register ./test/*.test.ts



  GET /auth/register
    1) should render the register view


  0 passing (23ms)
  1 failing

  1) GET /auth/register
       should render the register view:
     Uncaught TypeError: Cannot read property 'not' of undefined
      at /mnt/c/Users/micha/github/n4nite-api/test/routes.auth.test.ts:36:18
      at Test.Request.callback (node_modules/superagent/lib/node/index.js:716:12)
      at IncomingMessage.parser (node_modules/superagent/lib/node/index.js:916:18)
      at endReadableNT (_stream_readable.js:1064:12)
      at _combinedTickCallback (internal/process/next_tick.js:138:11)
      at process._tickCallback (internal/process/next_tick.js:180:9)

2 Answers

0
tomcek112 On

Have you tried explicitly importing should in your routes.auth.test.ts like:

import should = require('should');
0
n4nite On

Changing the start of routes.auth.test.ts as per the below partially fixed my problems.

import chai = require('chai');
const should = chai.should();

import chaiHttp = require('chai-http');
chai.use(chaiHttp);

import {describe} from 'mocha'

The await databaseInitializer(); in the server.ts file also caused problems so I had to remove this as well and find another way to initialize the database.