jest-playback
Advanced tools
Comparing version 4.0.0 to 4.1.0
@@ -5,2 +5,8 @@ # Changelog | ||
# [4.1.0](https://github.com/ikatyang/jest-playback/compare/v4.0.0...v4.1.0) (2023-07-25) | ||
### Features | ||
- support specifying mode ([#433](https://github.com/ikatyang/jest-playback/issues/433)) ([a920d2d](https://github.com/ikatyang/jest-playback/commit/a920d2d88ccc5a1a0c190515a35874078678e019)) | ||
# [4.0.0](https://github.com/ikatyang/jest-playback/compare/v3.0.0...v4.0.0) (2023-07-22) | ||
@@ -7,0 +13,0 @@ |
@@ -0,2 +1,6 @@ | ||
import { Mode } from './modes.js'; | ||
import { Options } from './options.js'; | ||
export default function setup(options?: Options): Promise<void>; | ||
import { setup } from './setup.js'; | ||
export type { Options }; | ||
export { setup, Mode }; | ||
export default setup; |
@@ -1,35 +0,4 @@ | ||
import { BatchInterceptor } from '@mswjs/interceptors'; | ||
import { ClientRequestInterceptor } from '@mswjs/interceptors/ClientRequest'; | ||
import { FetchInterceptor } from '@mswjs/interceptors/fetch'; | ||
import { XMLHttpRequestInterceptor } from '@mswjs/interceptors/XMLHttpRequest'; | ||
import { PlaybackManager } from './manager.js'; | ||
import { JestRunner, VitestRunner } from './runners/index.js'; | ||
export default async function setup(options) { | ||
const runner = getRunner(); | ||
await runner.init(); | ||
const manager = new PlaybackManager(runner, options); | ||
const interceptor = new BatchInterceptor({ | ||
name: 'interceptor', | ||
interceptors: [ | ||
new ClientRequestInterceptor(), | ||
new FetchInterceptor(), | ||
new XMLHttpRequestInterceptor(), | ||
], | ||
}); | ||
interceptor.apply(); | ||
interceptor.on('request', async ({ request }) => { | ||
const response = await manager.onRequest(request); | ||
if (response) { | ||
request.respondWith(response); | ||
} | ||
}); | ||
} | ||
function getRunner() { | ||
if ('JEST_WORKER_ID' in process.env) { | ||
return new JestRunner(); | ||
} | ||
if ('VITEST_POOL_ID' in process.env) { | ||
return new VitestRunner(); | ||
} | ||
throw new Error('unexpected environment'); | ||
} | ||
import { Mode } from './modes.js'; | ||
import { setup } from './setup.js'; | ||
export { setup, Mode }; | ||
export default setup; |
@@ -8,2 +8,3 @@ import { Options } from './options.js'; | ||
onRequest(request: Request): Promise<Response | null>; | ||
private getSnapshotUpdateState; | ||
private play; | ||
@@ -10,0 +11,0 @@ private record; |
@@ -5,2 +5,3 @@ import assert from 'node:assert'; | ||
import { fromResponseRecordText, toResponseRecordText } from './records.js'; | ||
import { Mode } from './modes.js'; | ||
const INTERNAL_HEADER = 'X-Jest-Playback-Internal'; | ||
@@ -27,3 +28,4 @@ export class PlaybackManager { | ||
const snapshot = _snapshotData[snapshotId]?.replace(/^\n|\n$/g, ''); | ||
switch (_updateSnapshot) { | ||
const updateState = this.getSnapshotUpdateState() ?? _updateSnapshot; | ||
switch (updateState) { | ||
case 'none': | ||
@@ -39,6 +41,22 @@ assert(snapshot, 'playback not found'); | ||
default: | ||
_updateSnapshot; | ||
throw new Error(`Unexpected _updateSnapshot ${_updateSnapshot}`); | ||
updateState; | ||
throw new Error(`Unexpected updateState ${updateState}`); | ||
} | ||
} | ||
getSnapshotUpdateState() { | ||
const mode = this.options.mode ?? Mode.Auto; | ||
switch (mode) { | ||
case Mode.Auto: | ||
return null; | ||
case Mode.Play: | ||
return 'none'; | ||
case Mode.Record: | ||
return 'new'; | ||
case Mode.Update: | ||
return 'all'; | ||
default: | ||
mode; | ||
throw new Error(`unexpected mode '${mode}'`); | ||
} | ||
} | ||
async play(snapshot, name) { | ||
@@ -45,0 +63,0 @@ const response = fromResponseRecordText(snapshot); |
@@ -0,4 +1,7 @@ | ||
import { Mode } from './modes.js'; | ||
export interface Options { | ||
/** @default Mode.Auto */ | ||
mode?: Mode; | ||
getRequestCacheKey?: (request: Request) => string | Promise<string>; | ||
} | ||
export declare function defaultGetRequestCacheKey(request: Request): Promise<string>; |
@@ -18,3 +18,3 @@ export interface ExpectStatic { | ||
} | ||
type SnapshotUpdateState = 'all' | 'new' | 'none'; | ||
export type SnapshotUpdateState = 'all' | 'new' | 'none'; | ||
type SnapshotData = Record<string, string>; | ||
@@ -21,0 +21,0 @@ interface PrettyFormatPlugin { |
{ | ||
"name": "jest-playback", | ||
"type": "module", | ||
"version": "4.0.0", | ||
"version": "4.1.0", | ||
"description": "Record and playback HTTP requests from your Jest tests", | ||
@@ -6,0 +6,0 @@ "keywords": [ |
@@ -18,30 +18,49 @@ # jest-playback | ||
In setup file or test file: | ||
```js | ||
import * as jestPlayback from 'jest-playback' | ||
```js | ||
import setupPlayback from 'jest-playback' | ||
await setupPlayback() | ||
await jestPlayback.setup() | ||
test('example', async () => { | ||
const response = await fetch('http://www.example.com/') | ||
expect(response.status).toBe(200) | ||
}) | ||
``` | ||
The HTTP responses are stored as snapshots: | ||
The records are stored as snapshots. | ||
- default | ||
- new requests will be stored | ||
- stored records will be played | ||
- with Jest `--ci` flag specified | ||
- new requests will be blocked | ||
- stored records will be played | ||
- with Jest `--update-snapshot` flag specified | ||
- new requests will be stored | ||
- stored records will be updated | ||
- obsolete records will be removed | ||
## API | ||
```ts | ||
declare function setupPlayback(options?: Options): Promise<void> | ||
export default setup | ||
export declare function setup(options?: Options): Promise<void> | ||
interface Options { | ||
export interface Options { | ||
/** @default Mode.Auto */ | ||
mode?: Mode | ||
getRequestCacheKey?: (request: Request) => string | Promise<string> | ||
} | ||
export enum Mode { | ||
/** | ||
* - `Mode.Update` if Jest `--update-snapshot` flag specified | ||
* - `Mode.Play` if Jest `--ci` flag specified | ||
* - `Mode.Record` otherwise | ||
*/ | ||
Auto = 'auto', | ||
/** | ||
* - all requests are recorded | ||
*/ | ||
Update = 'update', | ||
/** | ||
* - play records | ||
* - new requests are blocked | ||
*/ | ||
Play = 'play', | ||
/** | ||
* - play records | ||
* - new requests are recorded | ||
*/ | ||
Record = 'record', | ||
} | ||
``` | ||
@@ -48,0 +67,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
19480
24
358
86
1