Koa: Mocking a Koa Context

Created on 9 Jun 2017  路  5Comments  路  Source: koajs/koa

I didn't find any pre-existing issues, so I figured I'd open this for discussion. I'm looking for an easier way to mock a Context object in order to unit test routes/middleware in isolation without having to spin up and teardown an http server to do so.

Consider this trivial example:

authenticate.ts

import { Context } from 'koa';

export default function() {
  return async function(ctx: Context) {
    ctx.assert(ctx.headers.completelySecure === 'OPEN THE DOOR', 401, 'Unauthenticated.  What\'s the secret word?');
  };
}

authenticate.spec.ts

import authenticate from './authenticate';

describe('Authenticate', function() {
  it('totally works', async function(done) {
    const authenticator = authenticate();

    // Sadness, this does not implement `context` and there is no easy way
    // to do so
    const mockContext = {
      headers: {
        completelySecure: 'OPEN THE DOOR'
      }
    };

    try {
      await authenticator(mockContext);
      done();
    } catch(err) {
      done.fail();
    }
  });
});

What I'm proposing is to support creation of mock contexts either in the core library, or another closely held project since it's highly likely that something like this would need to depend on internals of the library.

documentation

Most helpful comment

Future Google visitors: Shopify made a thing

All 5 comments

I'm probably just stupid, but there's https://github.com/koajs/koa/blob/master/test/helpers/context.js which to my ears sounds like what you need? Or is it too complete to be considered a mockObject? :)
Testing theory isn't my strong side yet - so please correct me.

const context = require('../helpers/context');
...
it('test', async () => {
  const mockContext = context();
  const authenticator = async ctx => {
    ctx.assert(ctx.headers.completelySecure === 'OPEN THE DOOR', 401, 'Unauthenticated.  What\'s the secret word?');
  };
  mockContext.headers.completelySecure = 'OPEN THE DOOR';
  await authenticator(mockContext);
});

That sounds like it'd be fantastic if it could be made public; its not documented right now, so I assumed it was a private API

@dcherman it's not public because there's not really a point of mocking when it's really simple to just test an actual koa app. we also don't want people extending it too much and adding "globals" because we see that as an anti-pattern

Future Google visitors: Shopify made a thing

For Future visitors who don't use Jest but just want a simple Koa context to mock, I made a gist based on the solution above by @fl0w above. If I get time I'll try to make an test-runner agnostic npm package that has the same functionality as the package from Shopify.

https://gist.github.com/emmanuelnk/f1254eed8f947a81e8d715476d9cc92c

Was this page helpful?
0 / 5 - 0 ratings

Related issues

xinshouke picture xinshouke  路  4Comments

usernameisalreadytaken2014 picture usernameisalreadytaken2014  路  4Comments

ilkkao picture ilkkao  路  4Comments

ke1Del picture ke1Del  路  3Comments

coodoo picture coodoo  路  4Comments