Я пытаюсь запустить тестовый набор, используя мокко и чай. Я использовал несколько разных примеров, чтобы все заработало, но я попал в стену при запуске тестов. Похоже, что часть «следует», которую я импортировал из мокко, не создается. Любая помощь приветствуется.
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)