【问题标题】:I am getting an undefined error using mocha with typescript我在使用 mocha 和 typescript 时遇到未定义的错误
【发布时间】:2019-09-29 14:58:19
【问题描述】:

我正在尝试使用 mocha 和 chai 运行测试用例。我已经按照几个不同的示例来启动和运行,但我在运行测试时遇到了障碍。我从 mocha 导入的“应该”部分似乎没有被实例化。任何帮助表示赞赏。

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

输出

n4nite@DESKTOP-MMRKPMO:/mnt/c/Users/micha/github/n4nite-api$ npm test

> n4nite-api@0.0.1 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)

【问题讨论】:

    标签: node.js typescript mocha.js chai


    【解决方案1】:

    您是否尝试过在您的routes.auth.test.ts 中显式导入should,例如:

    import should = require('should');
    

    【讨论】:

      【解决方案2】:

      根据以下内容更改 routes.auth.test.ts 的开头部分解决了我的问题。

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

      server.ts 文件中的await databaseInitializer(); 也引起了问题,因此我也不得不删除它并找到另一种初始化数据库的方法。

      【讨论】:

        猜你喜欢
        • 2018-06-18
        • 1970-01-01
        • 2019-08-10
        • 2019-03-14
        • 2023-04-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-26
        相关资源
        最近更新 更多