Testing async/await middleware?
See original GitHub issueWe’re looking to migrate over to Koa 2. So far i’ve converted from generators to async await but I’m having a hard time wrapping my head around how to update my unit tests to test context bubbling up. I’ve scoured your code and other repo’s seeing if anyone has good middleware examples but come up short.
I used to test my middleware as such:
const gen = someMiddleware.call(koaContext);
// Here I force bubble up behavior allowing my middleware to add
// or manipulate data on the context.
gen.next().next();
koaContext.state.someValue.should.equal(...)
How do I accomplish the same bubble up behavior using async await
?
Issue Analytics
- State:
- Created 6 years ago
- Comments:7 (2 by maintainers)
Top Results From Across the Web
Jest testing of async middleware for authentication
A partial answer us simply to add an async/await on the middleware call: it('should add user to req on authorised requests', ...
Read more >Async testing Koa with Jest - Medium
Simplify API and middleware tests with lesser-known features. This is a transcript of a presentation given at October Node.js Berlin Meetup.
Read more >Writing Async/Await Middleware in Express
... experiment more with async / await functions and Express middleware; the best way I learn is by making test projects and having...
Read more >middleware-async - npm
A handy tool to work with async/promise express middleware. ... { test('should go through all middlewares', async () => { const req =...
Read more >Using async/await in Express - Zell Liew
Once you have the async keyword, you can await something immediately in your code. app.post('/testing', async (req ...
Read more >Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
In the end all a middleware does is mutate the
Context
. With that in mind testing is as easy as this:This issue inspired me to give a talk about testing Koa middleware and later I put it into a form of an article: https://medium.com/@robinpokorny/async-testing-koa-with-jest-1b6e84521b71
Maybe someone, who google this problem, will find it interesting.