-
-
Save yamalight/370306d4027d70dcf29650be9db30f91 to your computer and use it in GitHub Desktop.
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 looks like I cannot help you here then.
but if you already have integration test - your middleware should be already covered by them
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'
})
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.
@polRk one simple option would be to construct new app for each middleware inside tests and test it that way
@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')
Yes, you are right! But how can i test context.state ?
Maybe like this ?
server.user(internationalizationPlugin)
server.use(ctx => {
ctx.body = ctx.state
})
@polRk return it as a response?
This is just one middleware in a large middleware chain