Utilities to easily stub Koa context and cookies. The utilities are designed to help you write unit tests for your Koa middleware without needing to set up any kind of actual server in your test environment. When test writing is easy and fun you'll want to write more tests. ✨😎
$ yarn add @shopify/jest-koa-mocks
The module has two named exports,
You should usually be able to get away with most unit tests just using
This function allows you to create fully stubbable koa contexts for your tests.
In the simplest case you call
createMockContext, run your middleware passing the result in, and then assert against the context objects fields
Testing throws and redirects
ctx.redirect are defaulted to
jest.fn()s, allowing you to easily test that a request has redirected or thrown in your middleware.
ctx.cookies is created using
Testing apps using common koa libraries
createContext allows you to pass a
session key by default, so you should be able to test applications using the common body parsing or session libraries simply and quickly.
Creates a mock cookies instance.
const cookies =;
The returned object will have the signature
get functions are designed to mimic how actual koa cookie instances work. This means
set will set a value to the
get will retrieve values from the
// will set to the response storecookies;// will get from the request storecookies !== 'value';// => true
When testing against a mock cookies instance you can either assert against the
get functions, or you can check if the appropriate value is in the expected store.