question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Testing async/await middleware?

See original GitHub issue

We’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:closed
  • Created 6 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

11reactions
marvinhagemeistercommented, Jul 1, 2017

In the end all a middleware does is mutate the Context. With that in mind testing is as easy as this:

// Custom middleware
const foo = (ctx, next) => {
  ctx.state = {
    bar: "Hello World!",
  };

  await next();
}

// Test case
it("should set bar", async () => {
  const mock = {
    state: undefined,
  };

  // Simply pass a noop function as `next` argument
  const noop = () => { };
  await foo(mock, noop);
  assert.equal(mock.state.bar, "Hello World!");
});
7reactions
robinpokornycommented, Oct 18, 2017

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found