Skip to content

Instantly share code, notes, and snippets.

@yamalight
Created October 3, 2016 18:05
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save yamalight/370306d4027d70dcf29650be9db30f91 to your computer and use it in GitHub Desktop.
Save yamalight/370306d4027d70dcf29650be9db30f91 to your computer and use it in GitHub Desktop.
Koa and supertest example
const test = require('tape');
const koa = require('koa');
const supertest = require('supertest');
const app = koa();
app.use(function *(){
this.body = 'Hello World';
});
let server;
let request;
test('Start server', t => {
server = app.listen();
request = supertest(server);
t.end();
})
test('Koa test', t => {
request
.get('/')
.expect(200)
.end((err, res) => {
if (err) throw err;
t.equals(res.text, 'Hello World');
t.end();
});
});
test('Shutdown server', t => {
server.close();
t.end();
})
@polRk
Copy link

polRk commented Nov 13, 2019

@polRk one simple option would be to construct new app for each middleware inside tests and test it that way

Yes, you are right! But how can i test context.state ?
expect(context.state.key).toBe('value')

@polRk
Copy link

polRk commented Nov 13, 2019

Yes, you are right! But how can i test context.state ?

Maybe like this ?

server.user(internationalizationPlugin)
server.use(ctx => {
  ctx.body = ctx.state
})

@yamalight
Copy link
Author

@polRk return it as a response?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment