使用app.address无法解决Chai Test上的错误不是功能

时间:2019-02-12 11:03:23

标签: chai koa

我已经查看了几个答案,但不知道如何进行这项工作。我希望这与我将app.ts文件导入server.ts文件这一事实有关。我尝试将module.exports = bootstrap()添加到server.ts文件中,但这无济于事。

// app.ts

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

// server.ts

import * as bodyParser from 'koa-bodyparser';
import { databaseInitializer } from '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());

    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())

    app.listen(config.port);
};

bootstrap();

// routes.assets.test.ts

process.env.NODE_ENV = 'test';

import * as Chai from 'chai';
import {describe, should} from 'mocha'
import chaiHttp = require('chai-http');
Chai.use(chaiHttp);

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

console.log('app: ', app);


describe('GET /assets', () => {
    it('should return all assets', (done) => {
      Chai.request(app)
      .get('/assets')
      .end((err, res) => {
        // there should be no errors
        should.not.exist(err);
        // there should be a 200 status code
        res.status.should.equal(200);
        // the response should be JSON
        res.type.should.equal('application/json');
        // the JSON response body should have a
        // key-value pair of {"status": "success"}
        res.body.status.should.eql('success');
        // the JSON response body should have a
        // key-value pair of {"data": [3 movie objects]}
        res.body.data.length.should.eql(6);
        // the first object in the data array should
        // have the right keys
        res.body.data[0].should.include.keys(
          'tenant_id', 'asset_id', 'asset_type', 'asset_name'
        );
        done();
      });
    });
  });

感谢您的关注,

0 个答案:

没有答案