@skuba-lib/vitest-koa-mocks 0.0.0 → 1.0.0-node-24-20251210005321
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +22 -0
- package/README.md +97 -0
- package/lib/createMockContext/createMockContext.d.ts +29 -0
- package/lib/createMockContext/createMockContext.js +47 -0
- package/lib/createMockContext/createMockContext.js.map +1 -0
- package/lib/createMockCookies/createMockCookies.d.ts +7 -0
- package/lib/createMockCookies/createMockCookies.js +14 -0
- package/lib/createMockCookies/createMockCookies.js.map +1 -0
- package/lib/index.d.ts +2 -0
- package/lib/index.js +3 -0
- package/lib/index.js.map +1 -0
- package/package.json +48 -8
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
### MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2018-present Shopify
|
|
4
|
+
Copyright (c) 2020 SEEK
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
in the Software without restriction, including without limitation the rights
|
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
furnished to do so, subject to the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
# @skuba-lib/vitest-koa-mocks
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@skuba-lib/vitest-koa-mocks)
|
|
4
|
+
[](https://www.npmjs.com/package/@skuba-lib/vitest-koa-mocks)
|
|
5
|
+
|
|
6
|
+
Vitest-compatible Koa mocks for testing Koa middleware and applications.
|
|
7
|
+
|
|
8
|
+
Inspired by [Shopify's jest-koa-mocks](https://github.com/Shopify/quilt/tree/main/packages/jest-koa-mocks), this package provides utilities that do not rely on Jest globals and are suitable for ESM-based Vitest setups.
|
|
9
|
+
|
|
10
|
+
## API reference
|
|
11
|
+
|
|
12
|
+
### `createMockContext`
|
|
13
|
+
|
|
14
|
+
Create a fully-typed mock [`Koa` `Context`](https://koajs.com) for testing middleware and request handlers.
|
|
15
|
+
|
|
16
|
+
```typescript
|
|
17
|
+
describe('createServiceAuthClient', () => {
|
|
18
|
+
const serviceAuthClient = createServiceAuthClient({
|
|
19
|
+
audience: 'upstream-service',
|
|
20
|
+
baseUrl,
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it('attaches expected headers', async () => {
|
|
24
|
+
nock(baseUrl)
|
|
25
|
+
.get('/mocked')
|
|
26
|
+
// ensures authorization is passed through
|
|
27
|
+
.matchHeader('authorization', MOCK_SERVICE_AUTH_HEADER)
|
|
28
|
+
// ensures data tags are passed through
|
|
29
|
+
.matchHeader('seek-tag-record-expiry', '0000-01-01T00:00:00.000Z')
|
|
30
|
+
.matchHeader('seek-tag-test-record', 'true')
|
|
31
|
+
// ensures tracing headers are passed through
|
|
32
|
+
.matchHeader('x-request-id', 'abc')
|
|
33
|
+
.matchHeader('x-session-id', 'cba')
|
|
34
|
+
.reply(200);
|
|
35
|
+
|
|
36
|
+
const ctx = createMockContext({
|
|
37
|
+
headers: {
|
|
38
|
+
'seek-tag-record-expiry': '0000-01-01T00:00:00.000Z',
|
|
39
|
+
'seek-tag-test-record': 'true',
|
|
40
|
+
'x-request-id': 'abc',
|
|
41
|
+
'x-seek-jwt-issuer': 'requesting-service',
|
|
42
|
+
'x-session-id': 'cba',
|
|
43
|
+
},
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
contextStorage.enterWith(ctx);
|
|
47
|
+
|
|
48
|
+
await Middleware.seekTagMiddleware(
|
|
49
|
+
{ ...ctx, headers: { ...ctx.headers } },
|
|
50
|
+
() =>
|
|
51
|
+
serviceAuthClient.request({
|
|
52
|
+
url: '/mocked',
|
|
53
|
+
}),
|
|
54
|
+
);
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
`createMockContext` accepts an options object that lets you control:
|
|
60
|
+
|
|
61
|
+
- HTTP details such as **`method`**, **`url`**, **`statusCode`**, **`headers`**, **`host`**, and whether the request is **`encrypted`**
|
|
62
|
+
- Koa-specific behaviour such as **`state`**, **`session`**, **`cookies`**, and **`throw`** / **`redirect`** handlers
|
|
63
|
+
- arbitrary **`customProperties`** that are merged onto the returned context
|
|
64
|
+
|
|
65
|
+
The returned context type is `MockContext`, which extends Koa's `Context` with:
|
|
66
|
+
|
|
67
|
+
- a `cookies` implementation compatible with `MockCookies`
|
|
68
|
+
- a `request` that can include `body`, `rawBody`, and `session` metadata
|
|
69
|
+
|
|
70
|
+
### `createMockCookies`
|
|
71
|
+
|
|
72
|
+
Create a standalone mock cookies implementation for Koa-style code.
|
|
73
|
+
|
|
74
|
+
```typescript
|
|
75
|
+
import { createMockCookies } from '@skuba-lib/vitest-koa-mocks';
|
|
76
|
+
import { expect, it } from 'vitest';
|
|
77
|
+
|
|
78
|
+
it('tracks request and response cookies', () => {
|
|
79
|
+
const cookies = createMockCookies({ session: 'abc' });
|
|
80
|
+
|
|
81
|
+
expect(cookies.get('session')).toBe('abc');
|
|
82
|
+
|
|
83
|
+
cookies.set('session', 'def');
|
|
84
|
+
|
|
85
|
+
// requestStore reflects initial inbound cookies
|
|
86
|
+
expect(cookies.requestStore.get('session')).toBe('abc');
|
|
87
|
+
|
|
88
|
+
// responseStore reflects cookies set during the test
|
|
89
|
+
expect(cookies.responseStore.get('session')).toBe('def');
|
|
90
|
+
});
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
`createMockCookies` returns a `MockCookies` instance that:
|
|
94
|
+
|
|
95
|
+
- exposes `get` and `set` methods implemented with Vitest spies (`vi.fn`)
|
|
96
|
+
- maintains separate `requestStore` and `responseStore` `Map` instances for assertions
|
|
97
|
+
- supports a `secure` flag to simulate HTTPS behaviour
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { type Context } from 'koa';
|
|
2
|
+
import { type RequestMethod } from 'node-mocks-http';
|
|
3
|
+
import { vi } from 'vitest';
|
|
4
|
+
import { type MockCookies } from '../createMockCookies/createMockCookies.js';
|
|
5
|
+
export interface MockContext extends Context {
|
|
6
|
+
cookies: MockCookies;
|
|
7
|
+
request: Context['request'] & {
|
|
8
|
+
body?: unknown;
|
|
9
|
+
rawBody?: string;
|
|
10
|
+
session?: Record<string, unknown>;
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
export interface Options<CustomProperties extends object, RequestBody = undefined> {
|
|
14
|
+
url?: string;
|
|
15
|
+
method?: RequestMethod;
|
|
16
|
+
statusCode?: number;
|
|
17
|
+
session?: Record<string, unknown>;
|
|
18
|
+
headers?: Record<string, string>;
|
|
19
|
+
cookies?: Record<string, string>;
|
|
20
|
+
state?: Record<string, unknown>;
|
|
21
|
+
encrypted?: boolean;
|
|
22
|
+
host?: string;
|
|
23
|
+
requestBody?: RequestBody;
|
|
24
|
+
rawBody?: string;
|
|
25
|
+
throw?: ((status: number, message?: string) => never) | ReturnType<typeof vi.fn>;
|
|
26
|
+
redirect?: ((url: string) => void) | ReturnType<typeof vi.fn>;
|
|
27
|
+
customProperties?: CustomProperties;
|
|
28
|
+
}
|
|
29
|
+
export declare const createMockContext: <CustomProperties extends object, RequestBody = undefined>(options?: Options<CustomProperties, RequestBody>) => MockContext;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import stream from 'stream';
|
|
2
|
+
import Koa from 'koa';
|
|
3
|
+
import httpMocks from 'node-mocks-http';
|
|
4
|
+
import { vi } from 'vitest';
|
|
5
|
+
import { createMockCookies, } from '../createMockCookies/createMockCookies.js';
|
|
6
|
+
export const createMockContext = (options = {}) => {
|
|
7
|
+
const app = new Koa();
|
|
8
|
+
const { cookies, method, statusCode, session, requestBody, rawBody = '', url = '/', host = 'test.com', encrypted = false, throw: throwFn = vi.fn(), redirect = vi.fn(), headers = {}, state = {}, customProperties = {}, } = options;
|
|
9
|
+
const extensions = {
|
|
10
|
+
...customProperties,
|
|
11
|
+
throw: throwFn,
|
|
12
|
+
session,
|
|
13
|
+
redirect,
|
|
14
|
+
state,
|
|
15
|
+
};
|
|
16
|
+
const protocol = encrypted ? 'https' : 'http';
|
|
17
|
+
const requestHeaders = {
|
|
18
|
+
Host: host,
|
|
19
|
+
...headers,
|
|
20
|
+
};
|
|
21
|
+
const hasOriginHeader = 'Origin' in requestHeaders || 'origin' in requestHeaders;
|
|
22
|
+
if (!hasOriginHeader) {
|
|
23
|
+
requestHeaders.Origin = `${protocol}://${host}`;
|
|
24
|
+
}
|
|
25
|
+
const req = httpMocks.createRequest({
|
|
26
|
+
url,
|
|
27
|
+
method,
|
|
28
|
+
statusCode,
|
|
29
|
+
session,
|
|
30
|
+
headers: requestHeaders,
|
|
31
|
+
});
|
|
32
|
+
req.socket = new stream.Duplex();
|
|
33
|
+
Object.defineProperty(req.socket, 'encrypted', {
|
|
34
|
+
writable: false,
|
|
35
|
+
value: encrypted,
|
|
36
|
+
});
|
|
37
|
+
const res = httpMocks.createResponse();
|
|
38
|
+
res.statusCode = 404;
|
|
39
|
+
res.set = undefined;
|
|
40
|
+
const context = app.createContext(req, res);
|
|
41
|
+
Object.assign(context, extensions);
|
|
42
|
+
context.cookies = createMockCookies(cookies);
|
|
43
|
+
context.request.body = requestBody;
|
|
44
|
+
context.request.rawBody = rawBody;
|
|
45
|
+
return context;
|
|
46
|
+
};
|
|
47
|
+
//# sourceMappingURL=createMockContext.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createMockContext.js","sourceRoot":"","sources":["../../src/createMockContext/createMockContext.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,QAAQ,CAAC;AAE5B,OAAO,GAAqB,MAAM,KAAK,CAAC;AACxC,OAAO,SAAiC,MAAM,iBAAiB,CAAC;AAChE,OAAO,EAAE,EAAE,EAAE,MAAM,QAAQ,CAAC;AAE5B,OAAO,EAEL,iBAAiB,GAClB,MAAM,2CAA2C,CAAC;AAiCnD,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAI/B,UAAkD,EAAE,EACpD,EAAE;IACF,MAAM,GAAG,GAAG,IAAI,GAAG,EAAE,CAAC;IAEtB,MAAM,EACJ,OAAO,EACP,MAAM,EACN,UAAU,EACV,OAAO,EACP,WAAW,EACX,OAAO,GAAG,EAAE,EACZ,GAAG,GAAG,GAAG,EACT,IAAI,GAAG,UAAU,EACjB,SAAS,GAAG,KAAK,EACjB,KAAK,EAAE,OAAO,GAAG,EAAE,CAAC,EAAE,EAAE,EACxB,QAAQ,GAAG,EAAE,CAAC,EAAE,EAAE,EAClB,OAAO,GAAG,EAAE,EACZ,KAAK,GAAG,EAAE,EACV,gBAAgB,GAAG,EAAE,GACtB,GAAG,OAAO,CAAC;IAEZ,MAAM,UAAU,GAAG;QACjB,GAAG,gBAAgB;QACnB,KAAK,EAAE,OAAO;QACd,OAAO;QACP,QAAQ;QACR,KAAK;KACN,CAAC;IAIF,MAAM,QAAQ,GAAG,SAAS,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,C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|
+
requestStore: Map<string, string>;
|
|
5
|
+
responseStore: Map<string, string>;
|
|
6
|
+
}
|
|
7
|
+
export declare const createMockCookies: (cookies?: Record<string, string>, secure?: boolean) => MockCookies;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { vi } from 'vitest';
|
|
2
|
+
export const createMockCookies = (cookies = {}, secure = true) => {
|
|
3
|
+
const cookieEntries = Object.keys(cookies).map((key) => [key, cookies[key]]);
|
|
4
|
+
const requestStore = new Map(cookieEntries);
|
|
5
|
+
const responseStore = new Map(cookieEntries);
|
|
6
|
+
return {
|
|
7
|
+
set: vi.fn((key, value) => responseStore.set(key, value)),
|
|
8
|
+
get: vi.fn((key) => requestStore.get(key)),
|
|
9
|
+
requestStore,
|
|
10
|
+
responseStore,
|
|
11
|
+
secure,
|
|
12
|
+
};
|
|
13
|
+
};
|
|
14
|
+
//# sourceMappingURL=createMockCookies.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createMockCookies.js","sourceRoot":"","sources":["../../src/createMockCookies/createMockCookies.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,EAAE,EAAE,MAAM,QAAQ,CAAC;AAS5B,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAC/B,UAAkC,EAAE,EACpC,MAAM,GAAG,IAAI,EACA,EAAE;IACf,MAAM,aAAa,GAAG,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,GAAG,CAC5C,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC,GAAG,EAAE,OAAO,CAAC,GAAG,CAAC,CAAqB,CACjD,CAAC;IAEF,MAAM,YAAY,GAAG,IAAI,GAAG,CAAiB,aAAa,CAAC,CAAC;IAC5D,MAAM,aAAa,GAAG,IAAI,GAAG,CAAiB,aAAa,CAAC,CAAC;IAE7D,OAAO;QACL,GAAG,EAAE,EAAE,CAAC,EAAE,CAAC,CAAC,GAAW,EAAE,KAAa,EAAE,EAAE,CAAC,aAAa,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;QACzE,GAAG,EAAE,EAAE,CAAC,EAAE,CAAC,CAAC,GAAW,EAAE,EAAE,CAAC,YAAY,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QAClD,YAAY;QACZ,aAAa;QACb,MAAM;KACmB,CAAC;AAC9B,CAAC,CAAC"}
|
package/lib/index.d.ts
ADDED
package/lib/index.js
ADDED
package/lib/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,0CAA0C,CAAC;AACzD,cAAc,0CAA0C,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,11 +1,51 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@skuba-lib/vitest-koa-mocks",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
"
|
|
7
|
-
|
|
3
|
+
"version": "1.0.0-node-24-20251210005321",
|
|
4
|
+
"private": false,
|
|
5
|
+
"description": "Vitest mocks for Koa",
|
|
6
|
+
"homepage": "https://github.com/seek-oss/skuba/tree/main/packages/vitest-koa-mocks#readme",
|
|
7
|
+
"bugs": {
|
|
8
|
+
"url": "https://github.com/seek-oss/skuba/issues"
|
|
9
|
+
},
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/seek-oss/skuba.git",
|
|
13
|
+
"directory": "packages/vitest-koa-mocks"
|
|
14
|
+
},
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"type": "module",
|
|
17
|
+
"main": "./lib/index.js",
|
|
18
|
+
"module": "./lib/index.js",
|
|
19
|
+
"types": "./lib/index.d.ts",
|
|
20
|
+
"files": [
|
|
21
|
+
"lib/**/*.d.ts",
|
|
22
|
+
"lib/**/*.js",
|
|
23
|
+
"lib/**/*.js.map"
|
|
24
|
+
],
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"node-mocks-http": "^1.17.2"
|
|
27
|
+
},
|
|
28
|
+
"peerDependencies": {
|
|
29
|
+
"@types/koa": "^3.0.1",
|
|
30
|
+
"koa": "^3.1.1",
|
|
31
|
+
"vitest": "^4.0.14"
|
|
8
32
|
},
|
|
9
|
-
"
|
|
10
|
-
|
|
11
|
-
}
|
|
33
|
+
"engines": {
|
|
34
|
+
"node": ">=20.9.0"
|
|
35
|
+
},
|
|
36
|
+
"skuba": {
|
|
37
|
+
"entryPoint": "src/index.ts",
|
|
38
|
+
"template": "oss-npm-package",
|
|
39
|
+
"type": "package",
|
|
40
|
+
"version": "13.0.0"
|
|
41
|
+
},
|
|
42
|
+
"scripts": {
|
|
43
|
+
"build": "pnpm skuba build",
|
|
44
|
+
"format": "pnpm skuba format",
|
|
45
|
+
"lint": "pnpm skuba lint",
|
|
46
|
+
"skuba": "node ../../lib/skuba",
|
|
47
|
+
"test": "vitest run",
|
|
48
|
+
"test:ci": "vitest run --coverage",
|
|
49
|
+
"test:watch": "vitest"
|
|
50
|
+
}
|
|
51
|
+
}
|