spy4js
Advanced tools
Comparing version 3.1.5 to 3.2.0
@@ -434,2 +434,3 @@ 'use strict'; | ||
const symbolSnap = Symbol('__Spy_snap__'); | ||
const symbolSnapSerializer = Symbol('__Spy_snapSerializer__'); | ||
const symbolIsSpy = Symbol('__Spy_isSpy__'); | ||
@@ -443,2 +444,3 @@ const symbolFunc = Symbol('__Spy_func__'); | ||
snap: symbolSnap, | ||
snapSerializer: symbolSnapSerializer, | ||
isSpy: symbolIsSpy, | ||
@@ -561,2 +563,15 @@ func: symbolFunc, | ||
addSnapshotSerializer(serialize) { | ||
if (typeof serialize === 'function') { | ||
this[Symbols.snapSerializer] = serialize; | ||
const calls = this[Symbols.calls]; | ||
const lastArgs = calls.length ? calls[calls.length - 1].args : []; | ||
this[Symbols.snap] = serialize(...lastArgs); | ||
} else { | ||
this[Symbols.snap] = serialize; | ||
} | ||
return this; | ||
}, | ||
wasCalled(callCount) { | ||
@@ -708,2 +723,7 @@ const calls = SpyHelperFunctions.getCalls(this); | ||
}); | ||
if (spy[Symbols.snapSerializer]) { | ||
spy[Symbols.snap] = spy[Symbols.snapSerializer](...args); | ||
} | ||
return spy[Symbols.func](...args); | ||
@@ -710,0 +730,0 @@ }; |
@@ -430,2 +430,3 @@ import { Serializer } from 'serialize-as-code'; | ||
const symbolSnap = Symbol('__Spy_snap__'); | ||
const symbolSnapSerializer = Symbol('__Spy_snapSerializer__'); | ||
const symbolIsSpy = Symbol('__Spy_isSpy__'); | ||
@@ -439,2 +440,3 @@ const symbolFunc = Symbol('__Spy_func__'); | ||
snap: symbolSnap, | ||
snapSerializer: symbolSnapSerializer, | ||
isSpy: symbolIsSpy, | ||
@@ -557,2 +559,15 @@ func: symbolFunc, | ||
addSnapshotSerializer(serialize) { | ||
if (typeof serialize === 'function') { | ||
this[Symbols.snapSerializer] = serialize; | ||
const calls = this[Symbols.calls]; | ||
const lastArgs = calls.length ? calls[calls.length - 1].args : []; | ||
this[Symbols.snap] = serialize(...lastArgs); | ||
} else { | ||
this[Symbols.snap] = serialize; | ||
} | ||
return this; | ||
}, | ||
wasCalled(callCount) { | ||
@@ -704,2 +719,7 @@ const calls = SpyHelperFunctions.getCalls(this); | ||
}); | ||
if (spy[Symbols.snapSerializer]) { | ||
spy[Symbols.snap] = spy[Symbols.snapSerializer](...args); | ||
} | ||
return spy[Symbols.func](...args); | ||
@@ -706,0 +726,0 @@ }; |
{ | ||
"name": "spy4js", | ||
"version": "3.1.5", | ||
"version": "3.2.0", | ||
"description": "Smart, compact and powerful spy test framework", | ||
@@ -28,33 +28,35 @@ "main": "dist/cjs/index.js", | ||
"dependencies": { | ||
"serialize-as-code": "^2.0.0" | ||
"serialize-as-code": "^2.0.1" | ||
}, | ||
"devDependencies": { | ||
"@babel/core": "^7.15.0", | ||
"@babel/eslint-parser": "^7.15.0", | ||
"@babel/plugin-proposal-class-properties": "^7.14.5", | ||
"@babel/preset-env": "^7.15.0", | ||
"@babel/preset-react": "^7.14.5", | ||
"@babel/preset-typescript": "^7.15.0", | ||
"@babel/core": "^7.16.0", | ||
"@babel/eslint-parser": "^7.16.3", | ||
"@babel/plugin-proposal-class-properties": "^7.16.0", | ||
"@babel/preset-env": "^7.16.4", | ||
"@babel/preset-react": "^7.16.0", | ||
"@babel/preset-typescript": "^7.16.0", | ||
"@rollup/plugin-babel": "^5.3.0", | ||
"@rollup/plugin-node-resolve": "^13.0.4", | ||
"@types/jest": "^27.0.1", | ||
"@typescript-eslint/eslint-plugin": "^4.30.0", | ||
"@typescript-eslint/parser": "^4.30.0", | ||
"babel-jest": "^27.1.0", | ||
"@rollup/plugin-node-resolve": "^13.0.6", | ||
"@sucrase/jest-plugin": "^2.2.0", | ||
"@types/jest": "^27.0.3", | ||
"@typescript-eslint/eslint-plugin": "^5.4.0", | ||
"@typescript-eslint/parser": "^5.4.0", | ||
"coveralls": "^3.1.1", | ||
"eslint": "^7.32.0", | ||
"eslint": "^8.3.0", | ||
"eslint-config-prettier": "^8.3.0", | ||
"eslint-plugin-prettier": "^4.0.0", | ||
"jest": "^27.1.0", | ||
"prettier": "^2.3.2", | ||
"rollup": "^2.56.3", | ||
"typescript": "^4.4.2" | ||
"jest": "^27.3.1", | ||
"prettier": "^2.4.1", | ||
"rollup": "^2.60.1", | ||
"typescript": "^4.5.2" | ||
}, | ||
"scripts": { | ||
"dist": "rm -rf dist && rollup -c && cp index.ts index.d.ts", | ||
"eslint": "eslint --cache --cache-location 'build/.eslintcache' --ext .ts src/ test/", | ||
"test": "tsc && pnpm eslint && pnpm test-coverage", | ||
"test-coverage": "jest --all --coverage", | ||
"lint": "pnpm lint:es && pnpm lint:ts", | ||
"lint:es": "eslint --ext .ts src/ test/", | ||
"lint:ts": "tsc", | ||
"test": "pnpm lint && pnpm test:coverage", | ||
"test:coverage": "jest --all --coverage", | ||
"coveralls": "cat ./coverage/lcov.info | coveralls" | ||
} | ||
} |
@@ -94,2 +94,3 @@ [![GitHub license][license-image]][license-url] | ||
- `restore` (does make the spy restore the mocked object) | ||
- `addSnapshotSerializer` (defines in `jest` snapshots how the spy will be serialized) | ||
@@ -252,3 +253,3 @@ All those methods on a spy has been designed in a builder pattern. So you may chain any of | ||
```ts | ||
Spy.on(object:Object, methodName:string) => SpyInstance | ||
Spy.on(object: object, methodName: string) => SpyInstance | ||
``` | ||
@@ -264,3 +265,3 @@ Initializing a spy on an object, simply replaces the original function by a spy and | ||
```ts | ||
Spy.mock(object:Object, ...methodNames: string[]) => Object (Mock) | ||
Spy.mock(object: object, ...methodNames: string[]) => Object (Mock) | ||
``` | ||
@@ -447,2 +448,8 @@ Creating an object that references spies for all given methodNames. | ||
### addSnapshotSerializer | ||
```ts | ||
spy.addSnapshotSerializer(serialize: string | ((...args: any[]) => string)) => (this) SpyInstance | ||
``` | ||
Determines the rendered output of `jest` snapshots when the certain spy would get rendered. | ||
### wasCalled (fact) | ||
@@ -449,0 +456,0 @@ ```ts |
@@ -34,2 +34,3 @@ /** | ||
const symbolSnap = Symbol('__Spy_snap__'); | ||
const symbolSnapSerializer = Symbol('__Spy_snapSerializer__'); | ||
const symbolIsSpy = Symbol('__Spy_isSpy__'); | ||
@@ -43,2 +44,3 @@ const symbolFunc = Symbol('__Spy_func__'); | ||
snap: symbolSnap, | ||
snapSerializer: symbolSnapSerializer, | ||
isSpy: symbolIsSpy, | ||
@@ -89,2 +91,3 @@ func: symbolFunc, | ||
transparentAfter: (callCount: number) => SpyInstance; | ||
addSnapshotSerializer: (serialize: string | ((...args: any[]) => string)) => SpyInstance; | ||
wasCalled: (callCount?: number) => void; | ||
@@ -104,2 +107,3 @@ hasCallHistory: (...callHistory: Array<any[] | any>) => void; | ||
[Symbols.snap]: string; | ||
[Symbols.snapSerializer]?: (...args: any[]) => string; | ||
[Symbols.calls]: { args: any[]; order: number }[]; | ||
@@ -347,2 +351,20 @@ [Symbols.index]: number; | ||
/** | ||
* This method allows to customize the snapshot serialization of the spy. | ||
* Only works for Jest test runner as of now. | ||
* | ||
* @return {SpyInstance} <- BuilderPattern | ||
*/ | ||
addSnapshotSerializer(this: SpyInstance, serialize: string | ((...args: any[]) => string)): SpyInstance { | ||
if (typeof serialize === 'function') { | ||
this[Symbols.snapSerializer] = serialize; | ||
const calls = this[Symbols.calls]; | ||
const lastArgs = calls.length ? calls[calls.length - 1].args : []; | ||
this[Symbols.snap] = serialize(...lastArgs); | ||
} else { | ||
this[Symbols.snap] = serialize; | ||
} | ||
return this; | ||
}, | ||
/** | ||
* Checks if the spy was called callCount times often. | ||
@@ -691,2 +713,5 @@ * | ||
spy[Symbols.calls].push({ args, order: ++CallOrder.Idx }); | ||
if (spy[Symbols.snapSerializer]) { | ||
spy[Symbols.snap] = spy[Symbols.snapSerializer](...args); | ||
} | ||
return spy[Symbols.func](...args); | ||
@@ -693,0 +718,0 @@ }; |
126228
2731
649
Updatedserialize-as-code@^2.0.1