简体   繁体   English

我使用mocha与typescript得到一个未定义的错误

[英]I am getting an undefined error using mocha with typescript

I am trying to run a test case using mocha and chai. 我正在尝试使用mocha和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. 似乎我从mocha导入的'should'部分没有被实例化。 Any help is appreciated. 任何帮助表示赞赏。

app.ts app.ts

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

server.ts 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 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 产量

 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) 

您是否尝试在routes.auth.test.ts明确导入should

import should = require('should');

Changing the start of routes.auth.test.ts as per the below partially fixed my problems. 根据以下内容更改routes.auth.test.ts的开头部分修复了我的问题。

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

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

import {describe} from 'mocha'

The await databaseInitializer(); 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. server.ts文件中也导致了问题所以我不得不删除它,并找到另一种方法来初始化数据库。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我收到错误消息,使用 mocha 找不到模块 './--require.js' - I am getting the error, Can not find the module './--require.js' using mocha 我使用毯子在mocha代码覆盖范围内获得0%覆盖0 SLOC - I am getting 0 % coverage 0 SLOC in mocha code coverage using blanket 我正在使用 chai 和 mocha 测试 nodejs 中的 API,并出现错误“TypeError: Cannot read properties of undefined (reading 'address')” - I am using chai and mocha for testing APIs in nodejsand got error "TypeError: Cannot read properties of undefined (reading 'address')" 为什么会出现未定义的错误? - why am I getting an undefined error? 使用快递我试图上传文件,但我收到这样的错误:TypeError:无法读取未定义的属性“文件” - using express I am trying to upload file but I am getting error like this: TypeError: Cannot read property 'file' of undefined 我正在尝试使用 postman 中的 jwt 令牌更新用户,但出现此错误(无法读取未定义的属性 ID) - I am trying to update a user using jwt token in postman and i am getting this error (cannot read property id of undefined) React-Admin:使用“getList”,我收到“错误:无法读取未定义的属性‘地图’” - React-Admin: Using "getList", I am getting "Error: cannot read property 'map' of undefined" 我正在尝试使用 mocha 运行我的测试脚本,但出现“参考错误:beforeEach 未定义”的错误 - I am trying to run my test script using mocha but there is an error of "Reference Error: beforeEach is not defined" 使用打字稿时变得不确定 - getting undefined when using typescript 我收到错误 Typerror 无法读取未定义的属性“执行” - I am getting an error Typerror Cannot read property 'execute' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM