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

Can u give an example of testing koa context.state ?

@yamalight
Copy link
Author

@polRk it's best to test the implementation, not specific details - just do request(s) and test that server returns expected things.

@polRk
Copy link

polRk commented Nov 13, 2019

@polRk it's best to test the implementation, not specific details - just do request(s) and test that server returns expected things.

The server always returns OK.
This is about context.state and a handler that makes requests to another server.
See https://stackoverflow.com/questions/58832594/how-can-i-test-koa-middleware-with-typescript

@yamalight
Copy link
Author

@polRk your middleware still does something during one of the requests, right? if it's an i18n plugin - it'll return localized strings. so that's what you should be testing - switch locales and make sure requests actually get localized data back

@polRk
Copy link

polRk commented Nov 13, 2019

This plugin is for translating strings that will be sent via axios to another server. My server always returns OK

@polRk
Copy link

polRk commented Nov 13, 2019

And I am testing language detection. And for this I need to check my middleware work

@yamalight
Copy link
Author

@polRk then intercept that request and make sure it is correct

@polRk
Copy link

polRk commented Nov 13, 2019

It's not a request, but a language detection

@polRk
Copy link

polRk commented Nov 13, 2019

Another example

async function payloadPlugin(
  context: Koa.ParameterizedContext<AppState, AppContext>,
  next: Koa.Next
) {
  if (context.state.update?.data) {
    context.state.payload = JSON.parse(context.state.update.data)
  }

  await next()
}

export { payloadPlugin }

@polRk
Copy link

polRk commented Nov 13, 2019

I need to test the transmitted data between middleware at each step

@yamalight
Copy link
Author

@polRk you said:

plugin is for translating strings that will be sent via axios to another server

I pointed out that you should test that outgoing request.

For second example - once again, I'd test the place where it's being used, not the middleware itself.
Integration tests are generally way more valuable than unit tests.

@polRk
Copy link

polRk commented Nov 13, 2019

I have about 700 middleware. I need to check them all individually. And i have Integration tests. I need unit tests.

@polRk
Copy link

polRk commented Nov 13, 2019

I pointed out that you should test that outgoing request.

This is just one middleware in a large middleware chain

@yamalight
Copy link
Author

@polRk looks like I cannot help you here then.
but if you already have integration test - your middleware should be already covered by them

@polRk
Copy link

polRk commented Nov 13, 2019

Simple middleware chain.

const middlewareList = new Map<
  string,
  Koa.Middleware<AppState, AppContext>
>()
  .set('100_LoadUpdate', updatePlugin)
  .set('200_LoadState', statePlugin)
  .set('300_LoadPayload', payloadPlugin)
  .set('400_LoadInternationalization', internationalizationPlugin)

server.use(mount('/app', createApp(middlewareList)))
server.use(ctx => {
  ctx.body = 'OK'
})

@polRk
Copy link

polRk commented Nov 13, 2019

but if you already have integration test - your middleware should be already covered by them

The fact is that they are not all used. They connect as needed. This is a large number of plugins that should work independently.

@yamalight
Copy link
Author

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

@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