我正在尝试使用mocha和chai运行一个测试用例。我遵循了一些不同的示例来启动和运行设备,但是在运行测试时遇到了麻烦。我从摩卡咖啡导入的“应该”部分似乎没有实例化。任何帮助表示赞赏。
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)
答案 0 :(得分:0)
您是否尝试过像这样在should
中显式导入routes.auth.test.ts
:
import should = require('should');
答案 1 :(得分:0)
按照下面的方法更改 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();
也引起了问题,因此我也必须删除此问题并找到另一种初始化数据库的方法。