@wordpress/jest-console 8.37.1-next.79a2f3cdd.0 → 8.37.1-next.v.0
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/build/index.cjs +2 -1
- package/build/index.cjs.map +2 -2
- package/build-module/index.mjs +2 -1
- package/build-module/index.mjs.map +2 -2
- package/package.json +11 -7
- package/build/declarations.d.cjs +0 -2
- package/build/declarations.d.cjs.map +0 -7
- package/build-module/declarations.d.mjs +0 -1
- package/build-module/declarations.d.mjs.map +0 -7
- package/build-types/index.d.ts +0 -5
- package/build-types/index.d.ts.map +0 -1
- package/build-types/matchers.d.ts +0 -2
- package/build-types/matchers.d.ts.map +0 -1
- package/build-types/supported-matchers.d.ts +0 -3
- package/build-types/supported-matchers.d.ts.map +0 -1
- package/build-types/types.d.ts +0 -26
- package/build-types/types.d.ts.map +0 -1
- package/src/index.ts +0 -46
- package/src/matchers.ts +0 -121
- package/src/supported-matchers.ts +0 -8
- package/src/test/index.test.ts +0 -93
- package/src/types.ts +0 -34
- /package/{src/declarations.d.ts → declarations.d.ts} +0 -0
package/build/index.cjs
CHANGED
|
@@ -34,7 +34,8 @@ var setConsoleMethodSpy = (args) => {
|
|
|
34
34
|
}
|
|
35
35
|
function assertExpectedCalls() {
|
|
36
36
|
if (spy.assertionsNumber === 0 && spy.mock.calls.length > 0) {
|
|
37
|
-
|
|
37
|
+
const name = matcherName;
|
|
38
|
+
expect(console).not[name]();
|
|
38
39
|
}
|
|
39
40
|
}
|
|
40
41
|
beforeAll(resetSpy);
|
package/build/index.cjs.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/index.ts"],
|
|
4
|
-
"sourcesContent": ["/**\n * Internal dependencies\n */\nimport './matchers';\nimport supportedMatchers from './supported-matchers';\nimport type { ExtendedMock } from './types';\n\n/**\n * Sets spy on the console object's method to make it possible to fail test when method called without assertion.\n *\n * @param args\n */\nconst setConsoleMethodSpy = ( args: [ string, string ] ) => {\n\tconst [ methodName, matcherName ] = args;\n\tconst spy = jest\n\t\t.spyOn( console, methodName as 'error' | 'info' | 'log' | 'warn' )\n\t\t.mockName( `console.${ methodName }` ) as ExtendedMock;\n\n\t/**\n\t * Resets the spy to its initial state.\n\t */\n\tfunction resetSpy() {\n\t\tspy.mockReset();\n\t\tspy.assertionsNumber = 0;\n\t}\n\n\t/**\n\t * Verifies that the spy has only been called if expected.\n\t */\n\tfunction assertExpectedCalls() {\n\t\tif ( spy.assertionsNumber === 0 && spy.mock.calls.length > 0 ) {\n\t\t\texpect( console ).not[
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;AAGA,sBAAO;AACP,gCAA8B;AAQ9B,IAAM,sBAAsB,CAAE,SAA8B;AAC3D,QAAM,CAAE,YAAY,WAAY,IAAI;AACpC,QAAM,MAAM,KACV,MAAO,SAAS,UAAgD,EAChE,SAAU,WAAY,UAAW,EAAG;AAKtC,WAAS,WAAW;AACnB,QAAI,UAAU;AACd,QAAI,mBAAmB;AAAA,EACxB;AAKA,WAAS,sBAAsB;AAC9B,QAAK,IAAI,qBAAqB,KAAK,IAAI,KAAK,MAAM,SAAS,GAAI;
|
|
4
|
+
"sourcesContent": ["/**\n * Internal dependencies\n */\nimport './matchers';\nimport supportedMatchers from './supported-matchers';\nimport type { ExtendedMock } from './types';\n\n/**\n * Sets spy on the console object's method to make it possible to fail test when method called without assertion.\n *\n * @param args\n */\nconst setConsoleMethodSpy = ( args: [ string, string ] ) => {\n\tconst [ methodName, matcherName ] = args;\n\tconst spy = jest\n\t\t.spyOn( console, methodName as 'error' | 'info' | 'log' | 'warn' )\n\t\t.mockName( `console.${ methodName }` ) as ExtendedMock;\n\n\t/**\n\t * Resets the spy to its initial state.\n\t */\n\tfunction resetSpy() {\n\t\tspy.mockReset();\n\t\tspy.assertionsNumber = 0;\n\t}\n\n\t/**\n\t * Verifies that the spy has only been called if expected.\n\t */\n\tfunction assertExpectedCalls() {\n\t\tif ( spy.assertionsNumber === 0 && spy.mock.calls.length > 0 ) {\n\t\t\t// Using 'as' to satisfy TypeScript compiler about the matcher name.\n\t\t\ttype MatcherName = `toHave${\n\t\t\t\t| 'Errored'\n\t\t\t\t| 'Informed'\n\t\t\t\t| 'Logged'\n\t\t\t\t| 'Warned' }`;\n\t\t\tconst name = matcherName as MatcherName;\n\n\t\t\texpect( console ).not[ name ]();\n\t\t}\n\t}\n\n\tbeforeAll( resetSpy );\n\n\tbeforeEach( () => {\n\t\tassertExpectedCalls();\n\t\tresetSpy();\n\t} );\n\n\tafterEach( assertExpectedCalls );\n};\n\nObject.entries( supportedMatchers ).forEach( setConsoleMethodSpy );\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;AAGA,sBAAO;AACP,gCAA8B;AAQ9B,IAAM,sBAAsB,CAAE,SAA8B;AAC3D,QAAM,CAAE,YAAY,WAAY,IAAI;AACpC,QAAM,MAAM,KACV,MAAO,SAAS,UAAgD,EAChE,SAAU,WAAY,UAAW,EAAG;AAKtC,WAAS,WAAW;AACnB,QAAI,UAAU;AACd,QAAI,mBAAmB;AAAA,EACxB;AAKA,WAAS,sBAAsB;AAC9B,QAAK,IAAI,qBAAqB,KAAK,IAAI,KAAK,MAAM,SAAS,GAAI;AAO9D,YAAM,OAAO;AAEb,aAAQ,OAAQ,EAAE,IAAK,IAAK,EAAE;AAAA,IAC/B;AAAA,EACD;AAEA,YAAW,QAAS;AAEpB,aAAY,MAAM;AACjB,wBAAoB;AACpB,aAAS;AAAA,EACV,CAAE;AAEF,YAAW,mBAAoB;AAChC;AAEA,OAAO,QAAS,0BAAAA,OAAkB,EAAE,QAAS,mBAAoB;",
|
|
6
6
|
"names": ["supportedMatchers"]
|
|
7
7
|
}
|
package/build-module/index.mjs
CHANGED
|
@@ -10,7 +10,8 @@ var setConsoleMethodSpy = (args) => {
|
|
|
10
10
|
}
|
|
11
11
|
function assertExpectedCalls() {
|
|
12
12
|
if (spy.assertionsNumber === 0 && spy.mock.calls.length > 0) {
|
|
13
|
-
|
|
13
|
+
const name = matcherName;
|
|
14
|
+
expect(console).not[name]();
|
|
14
15
|
}
|
|
15
16
|
}
|
|
16
17
|
beforeAll(resetSpy);
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/index.ts"],
|
|
4
|
-
"sourcesContent": ["/**\n * Internal dependencies\n */\nimport './matchers';\nimport supportedMatchers from './supported-matchers';\nimport type { ExtendedMock } from './types';\n\n/**\n * Sets spy on the console object's method to make it possible to fail test when method called without assertion.\n *\n * @param args\n */\nconst setConsoleMethodSpy = ( args: [ string, string ] ) => {\n\tconst [ methodName, matcherName ] = args;\n\tconst spy = jest\n\t\t.spyOn( console, methodName as 'error' | 'info' | 'log' | 'warn' )\n\t\t.mockName( `console.${ methodName }` ) as ExtendedMock;\n\n\t/**\n\t * Resets the spy to its initial state.\n\t */\n\tfunction resetSpy() {\n\t\tspy.mockReset();\n\t\tspy.assertionsNumber = 0;\n\t}\n\n\t/**\n\t * Verifies that the spy has only been called if expected.\n\t */\n\tfunction assertExpectedCalls() {\n\t\tif ( spy.assertionsNumber === 0 && spy.mock.calls.length > 0 ) {\n\t\t\texpect( console ).not[
|
|
5
|
-
"mappings": ";AAGA,OAAO;AACP,OAAO,uBAAuB;AAQ9B,IAAM,sBAAsB,CAAE,SAA8B;AAC3D,QAAM,CAAE,YAAY,WAAY,IAAI;AACpC,QAAM,MAAM,KACV,MAAO,SAAS,UAAgD,EAChE,SAAU,WAAY,UAAW,EAAG;AAKtC,WAAS,WAAW;AACnB,QAAI,UAAU;AACd,QAAI,mBAAmB;AAAA,EACxB;AAKA,WAAS,sBAAsB;AAC9B,QAAK,IAAI,qBAAqB,KAAK,IAAI,KAAK,MAAM,SAAS,GAAI;
|
|
4
|
+
"sourcesContent": ["/**\n * Internal dependencies\n */\nimport './matchers';\nimport supportedMatchers from './supported-matchers';\nimport type { ExtendedMock } from './types';\n\n/**\n * Sets spy on the console object's method to make it possible to fail test when method called without assertion.\n *\n * @param args\n */\nconst setConsoleMethodSpy = ( args: [ string, string ] ) => {\n\tconst [ methodName, matcherName ] = args;\n\tconst spy = jest\n\t\t.spyOn( console, methodName as 'error' | 'info' | 'log' | 'warn' )\n\t\t.mockName( `console.${ methodName }` ) as ExtendedMock;\n\n\t/**\n\t * Resets the spy to its initial state.\n\t */\n\tfunction resetSpy() {\n\t\tspy.mockReset();\n\t\tspy.assertionsNumber = 0;\n\t}\n\n\t/**\n\t * Verifies that the spy has only been called if expected.\n\t */\n\tfunction assertExpectedCalls() {\n\t\tif ( spy.assertionsNumber === 0 && spy.mock.calls.length > 0 ) {\n\t\t\t// Using 'as' to satisfy TypeScript compiler about the matcher name.\n\t\t\ttype MatcherName = `toHave${\n\t\t\t\t| 'Errored'\n\t\t\t\t| 'Informed'\n\t\t\t\t| 'Logged'\n\t\t\t\t| 'Warned' }`;\n\t\t\tconst name = matcherName as MatcherName;\n\n\t\t\texpect( console ).not[ name ]();\n\t\t}\n\t}\n\n\tbeforeAll( resetSpy );\n\n\tbeforeEach( () => {\n\t\tassertExpectedCalls();\n\t\tresetSpy();\n\t} );\n\n\tafterEach( assertExpectedCalls );\n};\n\nObject.entries( supportedMatchers ).forEach( setConsoleMethodSpy );\n"],
|
|
5
|
+
"mappings": ";AAGA,OAAO;AACP,OAAO,uBAAuB;AAQ9B,IAAM,sBAAsB,CAAE,SAA8B;AAC3D,QAAM,CAAE,YAAY,WAAY,IAAI;AACpC,QAAM,MAAM,KACV,MAAO,SAAS,UAAgD,EAChE,SAAU,WAAY,UAAW,EAAG;AAKtC,WAAS,WAAW;AACnB,QAAI,UAAU;AACd,QAAI,mBAAmB;AAAA,EACxB;AAKA,WAAS,sBAAsB;AAC9B,QAAK,IAAI,qBAAqB,KAAK,IAAI,KAAK,MAAM,SAAS,GAAI;AAO9D,YAAM,OAAO;AAEb,aAAQ,OAAQ,EAAE,IAAK,IAAK,EAAE;AAAA,IAC/B;AAAA,EACD;AAEA,YAAW,QAAS;AAEpB,aAAY,MAAM;AACjB,wBAAoB;AACpB,aAAS;AAAA,EACV,CAAE;AAEF,YAAW,mBAAoB;AAChC;AAEA,OAAO,QAAS,iBAAkB,EAAE,QAAS,mBAAoB;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wordpress/jest-console",
|
|
3
|
-
"version": "8.37.1-next.
|
|
3
|
+
"version": "8.37.1-next.v.0+500f87dd8",
|
|
4
4
|
"description": "Custom Jest matchers for the Console object.",
|
|
5
5
|
"author": "The WordPress Contributors",
|
|
6
6
|
"license": "GPL-2.0-or-later",
|
|
@@ -25,24 +25,28 @@
|
|
|
25
25
|
"npm": ">=8.19.2"
|
|
26
26
|
},
|
|
27
27
|
"files": [
|
|
28
|
-
"src",
|
|
29
28
|
"build",
|
|
30
29
|
"build-module",
|
|
31
|
-
"
|
|
30
|
+
"declarations.d.ts"
|
|
32
31
|
],
|
|
33
32
|
"main": "build/index.cjs",
|
|
34
33
|
"module": "build-module/index.mjs",
|
|
35
34
|
"exports": {
|
|
36
35
|
".": {
|
|
37
|
-
"types": "./
|
|
36
|
+
"types": "./declarations.d.ts",
|
|
38
37
|
"import": "./build-module/index.mjs",
|
|
39
38
|
"require": "./build/index.cjs"
|
|
40
39
|
},
|
|
41
40
|
"./package.json": "./package.json"
|
|
42
41
|
},
|
|
43
|
-
"types": "
|
|
42
|
+
"types": "declarations.d.ts",
|
|
44
43
|
"dependencies": {
|
|
45
|
-
"jest-matcher-utils": "^29.6.2"
|
|
44
|
+
"jest-matcher-utils": "^29.6.2",
|
|
45
|
+
"jest-mock": "^29.6.2"
|
|
46
|
+
},
|
|
47
|
+
"devDependencies": {
|
|
48
|
+
"@testing-library/jest-dom": "^6.6.3",
|
|
49
|
+
"@types/jest": "^29.5.14"
|
|
46
50
|
},
|
|
47
51
|
"peerDependencies": {
|
|
48
52
|
"jest": ">=29"
|
|
@@ -50,5 +54,5 @@
|
|
|
50
54
|
"publishConfig": {
|
|
51
55
|
"access": "public"
|
|
52
56
|
},
|
|
53
|
-
"gitHead": "
|
|
57
|
+
"gitHead": "ca0db0ee8ac2116cd307650136027d26d0cdd9bd"
|
|
54
58
|
}
|
package/build/declarations.d.cjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
//# sourceMappingURL=declarations.d.mjs.map
|
package/build-types/index.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,YAAY,CAAC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"matchers.d.ts","sourceRoot":"","sources":["../src/matchers.ts"],"names":[],"mappings":""}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"supported-matchers.d.ts","sourceRoot":"","sources":["../src/supported-matchers.ts"],"names":[],"mappings":"AAAA,QAAA,MAAM,iBAAiB,EAAE,MAAM,CAAE,MAAM,EAAE,MAAM,CAK9C,CAAC;AAEF,eAAe,iBAAiB,CAAC"}
|
package/build-types/types.d.ts
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* External dependencies
|
|
3
|
-
*/
|
|
4
|
-
import type { Mock } from 'jest-mock';
|
|
5
|
-
/**
|
|
6
|
-
* Interface for the extended Jest Mock that includes the assertionsNumber property
|
|
7
|
-
*/
|
|
8
|
-
export interface ExtendedMock extends Mock {
|
|
9
|
-
assertionsNumber: number;
|
|
10
|
-
}
|
|
11
|
-
/**
|
|
12
|
-
* Interface for the matcher result object
|
|
13
|
-
*/
|
|
14
|
-
export interface MatcherResult {
|
|
15
|
-
pass: boolean;
|
|
16
|
-
message: () => string;
|
|
17
|
-
}
|
|
18
|
-
/**
|
|
19
|
-
* Interface for the matcher function
|
|
20
|
-
*/
|
|
21
|
-
export type MatcherFunction = (received: Record<string, Mock>) => MatcherResult;
|
|
22
|
-
/**
|
|
23
|
-
* Interface for the matcher function with arguments
|
|
24
|
-
*/
|
|
25
|
-
export type MatcherWithArgsFunction = (received: Record<string, Mock>, ...expected: unknown[]) => MatcherResult;
|
|
26
|
-
//# sourceMappingURL=types.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAEtC;;GAEG;AACH,MAAM,WAAW,YAAa,SAAQ,IAAI;IACzC,gBAAgB,EAAE,MAAM,CAAC;CACzB;AAED;;GAEG;AACH,MAAM,WAAW,aAAa;IAC7B,IAAI,EAAE,OAAO,CAAC;IACd,OAAO,EAAE,MAAM,MAAM,CAAC;CACtB;AAED;;GAEG;AACH,MAAM,MAAM,eAAe,GAAG,CAC7B,QAAQ,EAAE,MAAM,CAAE,MAAM,EAAE,IAAI,CAAE,KAC5B,aAAa,CAAC;AAEnB;;GAEG;AACH,MAAM,MAAM,uBAAuB,GAAG,CACrC,QAAQ,EAAE,MAAM,CAAE,MAAM,EAAE,IAAI,CAAE,EAChC,GAAG,QAAQ,EAAE,OAAO,EAAE,KAClB,aAAa,CAAC"}
|
package/src/index.ts
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Internal dependencies
|
|
3
|
-
*/
|
|
4
|
-
import './matchers';
|
|
5
|
-
import supportedMatchers from './supported-matchers';
|
|
6
|
-
import type { ExtendedMock } from './types';
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Sets spy on the console object's method to make it possible to fail test when method called without assertion.
|
|
10
|
-
*
|
|
11
|
-
* @param args
|
|
12
|
-
*/
|
|
13
|
-
const setConsoleMethodSpy = ( args: [ string, string ] ) => {
|
|
14
|
-
const [ methodName, matcherName ] = args;
|
|
15
|
-
const spy = jest
|
|
16
|
-
.spyOn( console, methodName as 'error' | 'info' | 'log' | 'warn' )
|
|
17
|
-
.mockName( `console.${ methodName }` ) as ExtendedMock;
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Resets the spy to its initial state.
|
|
21
|
-
*/
|
|
22
|
-
function resetSpy() {
|
|
23
|
-
spy.mockReset();
|
|
24
|
-
spy.assertionsNumber = 0;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Verifies that the spy has only been called if expected.
|
|
29
|
-
*/
|
|
30
|
-
function assertExpectedCalls() {
|
|
31
|
-
if ( spy.assertionsNumber === 0 && spy.mock.calls.length > 0 ) {
|
|
32
|
-
expect( console ).not[ matcherName ]();
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
beforeAll( resetSpy );
|
|
37
|
-
|
|
38
|
-
beforeEach( () => {
|
|
39
|
-
assertExpectedCalls();
|
|
40
|
-
resetSpy();
|
|
41
|
-
} );
|
|
42
|
-
|
|
43
|
-
afterEach( assertExpectedCalls );
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
Object.entries( supportedMatchers ).forEach( setConsoleMethodSpy );
|
package/src/matchers.ts
DELETED
|
@@ -1,121 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* External dependencies
|
|
3
|
-
*/
|
|
4
|
-
import { matcherHint, printExpected, printReceived } from 'jest-matcher-utils';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Internal dependencies
|
|
8
|
-
*/
|
|
9
|
-
import supportedMatchers from './supported-matchers';
|
|
10
|
-
import type { Mock } from 'jest-mock';
|
|
11
|
-
import type {
|
|
12
|
-
ExtendedMock,
|
|
13
|
-
MatcherFunction,
|
|
14
|
-
MatcherResult,
|
|
15
|
-
MatcherWithArgsFunction,
|
|
16
|
-
} from './types';
|
|
17
|
-
|
|
18
|
-
interface SpyInfo {
|
|
19
|
-
spy: Mock;
|
|
20
|
-
pass: boolean;
|
|
21
|
-
calls: unknown[][];
|
|
22
|
-
matcherName: string;
|
|
23
|
-
methodName: string;
|
|
24
|
-
expected?: unknown[];
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
const createErrorMessage = ( spyInfo: SpyInfo ) => {
|
|
28
|
-
const { spy, pass, calls, matcherName, methodName, expected } = spyInfo;
|
|
29
|
-
const hint = pass ? `.not${ matcherName }` : matcherName;
|
|
30
|
-
const message = pass
|
|
31
|
-
? `Expected mock function not to be called but it was called with:\n${ calls.map(
|
|
32
|
-
printReceived
|
|
33
|
-
) }`
|
|
34
|
-
: `Expected mock function to be called${
|
|
35
|
-
expected ? ` with:\n${ printExpected( expected ) }\n` : '.'
|
|
36
|
-
}\nbut it was called with:\n${ calls.map( printReceived ) }`;
|
|
37
|
-
|
|
38
|
-
return () =>
|
|
39
|
-
`${ matcherHint( hint, spy.getMockName() ) }` +
|
|
40
|
-
'\n\n' +
|
|
41
|
-
message +
|
|
42
|
-
'\n\n' +
|
|
43
|
-
`console.${ methodName }() should not be used unless explicitly expected\n` +
|
|
44
|
-
'See https://www.npmjs.com/package/@wordpress/jest-console for details.';
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
const createSpyInfo = (
|
|
48
|
-
spy: Mock,
|
|
49
|
-
matcherName: string,
|
|
50
|
-
methodName: string,
|
|
51
|
-
expected?: unknown[]
|
|
52
|
-
) => {
|
|
53
|
-
const calls = spy.mock.calls;
|
|
54
|
-
|
|
55
|
-
const pass = expected
|
|
56
|
-
? JSON.stringify( calls ).includes( JSON.stringify( expected ) )
|
|
57
|
-
: calls.length > 0;
|
|
58
|
-
|
|
59
|
-
const message = createErrorMessage( {
|
|
60
|
-
spy,
|
|
61
|
-
pass,
|
|
62
|
-
calls,
|
|
63
|
-
matcherName,
|
|
64
|
-
methodName,
|
|
65
|
-
expected,
|
|
66
|
-
} );
|
|
67
|
-
|
|
68
|
-
return {
|
|
69
|
-
pass,
|
|
70
|
-
message,
|
|
71
|
-
};
|
|
72
|
-
};
|
|
73
|
-
|
|
74
|
-
const createToHaveBeenCalledMatcher =
|
|
75
|
-
( matcherName: string, methodName: string ) =>
|
|
76
|
-
( received: Record< string, Mock > ): MatcherResult => {
|
|
77
|
-
const spy = received[ methodName ] as ExtendedMock;
|
|
78
|
-
const spyInfo = createSpyInfo( spy, matcherName, methodName );
|
|
79
|
-
spy.assertionsNumber += 1;
|
|
80
|
-
return spyInfo;
|
|
81
|
-
};
|
|
82
|
-
|
|
83
|
-
const createToHaveBeenCalledWith = (
|
|
84
|
-
matcherName: string,
|
|
85
|
-
methodName: string
|
|
86
|
-
) =>
|
|
87
|
-
function (
|
|
88
|
-
received: Record< string, Mock >,
|
|
89
|
-
...expected: unknown[]
|
|
90
|
-
): MatcherResult {
|
|
91
|
-
const spy = received[ methodName ] as ExtendedMock;
|
|
92
|
-
const spyInfo = createSpyInfo( spy, matcherName, methodName, expected );
|
|
93
|
-
spy.assertionsNumber += 1;
|
|
94
|
-
return spyInfo;
|
|
95
|
-
};
|
|
96
|
-
|
|
97
|
-
type MatchersObject = Record<
|
|
98
|
-
string,
|
|
99
|
-
MatcherFunction | MatcherWithArgsFunction
|
|
100
|
-
>;
|
|
101
|
-
|
|
102
|
-
expect.extend(
|
|
103
|
-
Object.entries( supportedMatchers ).reduce< MatchersObject >(
|
|
104
|
-
( result, [ methodName, matcherName ] ) => {
|
|
105
|
-
const matcherNameWith = `${ matcherName }With`;
|
|
106
|
-
|
|
107
|
-
return {
|
|
108
|
-
...result,
|
|
109
|
-
[ matcherName ]: createToHaveBeenCalledMatcher(
|
|
110
|
-
`.${ matcherName }`,
|
|
111
|
-
methodName
|
|
112
|
-
),
|
|
113
|
-
[ matcherNameWith ]: createToHaveBeenCalledWith(
|
|
114
|
-
`.${ matcherNameWith }`,
|
|
115
|
-
methodName
|
|
116
|
-
),
|
|
117
|
-
};
|
|
118
|
-
},
|
|
119
|
-
{}
|
|
120
|
-
)
|
|
121
|
-
);
|
package/src/test/index.test.ts
DELETED
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
/* eslint-disable no-console */
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Internal dependencies
|
|
5
|
-
*/
|
|
6
|
-
import '../matchers';
|
|
7
|
-
|
|
8
|
-
describe( 'jest-console', () => {
|
|
9
|
-
describe.each( [
|
|
10
|
-
[ 'error', 'toHaveErrored' ],
|
|
11
|
-
[ 'info', 'toHaveInformed' ],
|
|
12
|
-
[ 'log', 'toHaveLogged' ],
|
|
13
|
-
[ 'warn', 'toHaveWarned' ],
|
|
14
|
-
] )( 'console.%s', ( methodName, matcherName ) => {
|
|
15
|
-
const matcherNameWith = `${ matcherName }With`;
|
|
16
|
-
const message = `This is ${ methodName }!`;
|
|
17
|
-
|
|
18
|
-
test( `${ matcherName } works`, () => {
|
|
19
|
-
console[ methodName ]( message );
|
|
20
|
-
|
|
21
|
-
expect( console )[ matcherName ]();
|
|
22
|
-
} );
|
|
23
|
-
|
|
24
|
-
test( `${ matcherName } works when not called`, () => {
|
|
25
|
-
expect( console ).not[ matcherName ]();
|
|
26
|
-
expect( () => expect( console )[ matcherName ]() ).toThrow(
|
|
27
|
-
'Expected mock function to be called.'
|
|
28
|
-
);
|
|
29
|
-
} );
|
|
30
|
-
|
|
31
|
-
test( `${ matcherNameWith } works with arguments that match`, () => {
|
|
32
|
-
console[ methodName ]( message );
|
|
33
|
-
|
|
34
|
-
expect( console )[ matcherNameWith ]( message );
|
|
35
|
-
} );
|
|
36
|
-
|
|
37
|
-
test( `${ matcherNameWith } works when not called`, () => {
|
|
38
|
-
expect( console ).not[ matcherNameWith ]( message );
|
|
39
|
-
expect( () =>
|
|
40
|
-
expect( console )[ matcherNameWith ]( message )
|
|
41
|
-
).toThrow(
|
|
42
|
-
/Expected mock function to be called with:.*but it was called with:/s
|
|
43
|
-
);
|
|
44
|
-
} );
|
|
45
|
-
|
|
46
|
-
test( `${ matcherNameWith } works with many arguments that do not match`, () => {
|
|
47
|
-
console[ methodName ]( 'Unknown message.' );
|
|
48
|
-
console[ methodName ]( message, 'Unknown param.' );
|
|
49
|
-
|
|
50
|
-
expect( console ).not[ matcherNameWith ]( message );
|
|
51
|
-
expect( () =>
|
|
52
|
-
expect( console )[ matcherNameWith ]( message )
|
|
53
|
-
).toThrow(
|
|
54
|
-
/Expected mock function to be called with:.*but it was called with:.*Unknown param./s
|
|
55
|
-
);
|
|
56
|
-
} );
|
|
57
|
-
|
|
58
|
-
test( 'assertions number gets incremented after every matcher call', () => {
|
|
59
|
-
const spy = console[ methodName ];
|
|
60
|
-
|
|
61
|
-
expect( spy.assertionsNumber ).toBe( 0 );
|
|
62
|
-
|
|
63
|
-
console[ methodName ]( message );
|
|
64
|
-
|
|
65
|
-
expect( console )[ matcherName ]();
|
|
66
|
-
expect( spy.assertionsNumber ).toBe( 1 );
|
|
67
|
-
|
|
68
|
-
expect( console )[ matcherNameWith ]( message );
|
|
69
|
-
expect( spy.assertionsNumber ).toBe( 2 );
|
|
70
|
-
} );
|
|
71
|
-
|
|
72
|
-
describe( 'lifecycle', () => {
|
|
73
|
-
beforeAll( () => {
|
|
74
|
-
// Disable reason:
|
|
75
|
-
// This is a difficult one to test, since the matcher's
|
|
76
|
-
// own lifecycle is defined to run before ours. Infer
|
|
77
|
-
// that we're being watched by testing the console
|
|
78
|
-
// method as being a spy.
|
|
79
|
-
// eslint-disable-next-line jest/no-standalone-expect
|
|
80
|
-
expect(
|
|
81
|
-
console[ methodName ].assertionsNumber
|
|
82
|
-
).toBeGreaterThanOrEqual( 0 );
|
|
83
|
-
} );
|
|
84
|
-
|
|
85
|
-
// Disable reason:
|
|
86
|
-
// See beforeAll implementation and explanation added there.
|
|
87
|
-
// eslint-disable-next-line jest/expect-expect
|
|
88
|
-
it( 'captures logging in lifecycle', () => {} );
|
|
89
|
-
} );
|
|
90
|
-
} );
|
|
91
|
-
} );
|
|
92
|
-
|
|
93
|
-
/* eslint-enable no-console */
|
package/src/types.ts
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* External dependencies
|
|
3
|
-
*/
|
|
4
|
-
import type { Mock } from 'jest-mock';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Interface for the extended Jest Mock that includes the assertionsNumber property
|
|
8
|
-
*/
|
|
9
|
-
export interface ExtendedMock extends Mock {
|
|
10
|
-
assertionsNumber: number;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Interface for the matcher result object
|
|
15
|
-
*/
|
|
16
|
-
export interface MatcherResult {
|
|
17
|
-
pass: boolean;
|
|
18
|
-
message: () => string;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Interface for the matcher function
|
|
23
|
-
*/
|
|
24
|
-
export type MatcherFunction = (
|
|
25
|
-
received: Record< string, Mock >
|
|
26
|
-
) => MatcherResult;
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Interface for the matcher function with arguments
|
|
30
|
-
*/
|
|
31
|
-
export type MatcherWithArgsFunction = (
|
|
32
|
-
received: Record< string, Mock >,
|
|
33
|
-
...expected: unknown[]
|
|
34
|
-
) => MatcherResult;
|
|
File without changes
|