Gathering detailed insights and metrics for @shopify/jest-koa-mocks
Gathering detailed insights and metrics for @shopify/jest-koa-mocks
[⚠️ Deprecated] A loosely related set of packages for JavaScript/TypeScript projects at Shopify
npm install @shopify/jest-koa-mocks
Typescript
Module System
Min. Node Version
Node Version
NPM Version
96.8
Supply Chain
97
Quality
83.6
Maintenance
100
Vulnerability
100
License
@shopify/react-i18n-universal-provider@3.1.8
Published on 10 Jan 2025
@shopify/react-i18n@7.14.0
Published on 10 Jan 2025
@shopify/name@1.3.0
Published on 10 Jan 2025
@shopify/dates@2.1.1
Published on 10 Jan 2025
@shopify/react-i18n-universal-provider@3.1.7
Published on 10 Jan 2025
@shopify/react-i18n@7.13.2
Published on 10 Jan 2025
TypeScript (96.39%)
Ruby (2.34%)
JavaScript (1.23%)
Shell (0.02%)
CSS (0.02%)
Total Downloads
12,878,913
Last Day
9,864
Last Week
38,629
Last Month
142,465
Last Year
2,910,035
1,700 Stars
4,974 Commits
225 Forks
392 Watching
249 Branches
7,201 Contributors
Minified
Minified + Gzipped
Latest Version
5.3.1
Package Id
@shopify/jest-koa-mocks@5.3.1
Unpacked Size
20.89 kB
Size
5.38 kB
File Count
30
NPM Version
10.7.0
Node Version
18.20.4
Publised On
13 Sept 2024
Cumulative downloads
Total Downloads
Last day
33.9%
9,864
Compared to previous day
Last week
12.8%
38,629
Compared to previous week
Last month
9.5%
142,465
Compared to previous month
Last year
-27.9%
2,910,035
Compared to previous year
2
2
@shopify/jest-koa-mocks
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. ✨😎
1yarn add @shopify/jest-koa-mocks
The module has two named exports, createMockContext
and createMockCookies
.
You should usually be able to get away with most unit tests just using createMockContext
.
1import {createMockContext, createMockCookies} from '@shopify/jest-koa-mocks';
This function allows you to create fully stubbable koa contexts for your tests.
1 export interface Options< 2 CustomProperties extends Object, 3 RequestBody = undefined 4 > { 5 url?: string; 6 method?: RequestMethod; 7 statusCode?: number; 8 session?: Dictionary<any>; 9 headers?: Dictionary<string>; 10 cookies?: Dictionary<string>; 11 state?: Dictionary<any>; 12 encrypted?: boolean; 13 host?: string; 14 requestBody?: RequestBody; 15 throw?: Function; 16 redirect?: Function; 17 customProperties?: CustomProperties; 18 } 19 20 createContext(options: Options)
In the simplest case you call createMockContext
, run your middleware passing the result in, and then assert against the context objects fields
1import SillyViewCounterMiddleware from '../silly-view-counter'; 2import {createMockContext} from '@shopify/jest-koa-mocks'; 3 4describe('silly-view-counter', () => { 5 it('iterates and displays new ctx.state.views', async () => { 6 const ctx = createMockContext({state: {views: 31}}); 7 8 await SillyViewCounterMiddleware(ctx); 9 10 expect(ctx.state.views).toBe(32); 11 expect(ctx.status).toBe(200); 12 expect(ctx.body).toBe({view: 32}); 13 }); 14});
ctx.throw
and ctx.redirect
are defaulted to jest.fn()
s, allowing you to easily test that a request has redirected or thrown in your middleware.
1import passwordValidator from '../password-validator'; 2import {createMockContext} from '@shopify/jest-koa-mocks'; 3 4describe('password-validator', () => { 5 it('throws if no password query parameter is present', async () => { 6 const ctx = createMockContext({url: '/validate'}); 7 8 await passwordValidator(ctx); 9 10 expect(ctx.throw).toBeCalledWith(400); 11 }); 12 13 it('redirects to /user if the password is correct', async () => { 14 const ctx = createMockContext({url: '/validate?password=correct'}); 15 16 await passwordValidator(ctx); 17 18 expect(ctx.redirect).toBeCalledWith('/user'); 19 }); 20});
ctx.cookies
is created using createMockCookies
.
1import oAuthStart from '../'; 2import {createMockContext} from '@shopify/jest-koa-mocks'; 3 4describe('oauthStart', () => { 5 it('sets nonce cookie', () => { 6 const oAuthStart = createOAuthStart(baseConfig); 7 const ctx = createMockContext({ 8 url: `https://myCoolApp.com/auth`, 9 }); 10 11 oAuthStart(ctx); 12 13 expect(ctx.cookies.set).toBeCalledWith('shopifyNonce', fakeNonce); 14 }); 15});
createMockContext
allows you to pass a requestBody
and session
key by default, so you should be able to test applications using the common body parsing or session libraries simply and quickly.
1import login from '../login'; 2import {createMockContext} from '@shopify/jest-koa-mocks'; 3 4describe('password-validator', () => { 5 it('sets session.user if body contains a valid password and username', async () => { 6 const ctx = createMockContext({ 7 url: '/login', 8 requestBody: { 9 username: 'valid', 10 password: 'valid', 11 }, 12 session: {}, 13 }); 14 15 await login(ctx); 16 17 expect(ctx.session.user).toMatchObject({ 18 username: 'valid', 19 accessToken: 'dummy-access-token', 20 }); 21 }); 22});
Creates a mock cookies instance.
1const cookies = createMockCookies({ 2 sessionID: 'something something', 3 store: 'shop1', 4 referrer: 'somewhere.io', 5});
The returned object will have the signature
1interface MockCookies { 2 set(key: string, value: string): void; 3 get(key: string): string; 4 responseStore: Map<string, string>; 5 requestStore: Map<string, string>; 6}
The set
and get
functions are designed to mimic how actual koa cookie instances work. This means set
will set a value to the responseStore
, while get
will retrieve values from the requestStore
.
1// will set to the response store 2cookies.set('key', 'value'); 3 4// will get from the request store 5cookies.get('key') !== 'value'; 6// => true
When testing against a mock cookies instance you can either assert against the set
/get
functions, or you can check if the appropriate value is in the expected store.
1cookies.set('foo', 'bar'); 2expect(cookies.set).toBeCalledWith('foo', 'bar');
1cookies.set('foo', 'bar'); 2expect(cookies.responseStore.get('foo')).toBe('bar');
No vulnerabilities found.
Reason
all changesets reviewed
Reason
30 commit(s) and 3 issue activity found in the last 90 days -- score normalized to 10
Reason
license file detected
Details
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
security policy file detected
Details
Reason
8 existing vulnerabilities detected
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-07-01
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More