@wordpress/jest-console 8.36.1-next.6deb34194.0 → 8.36.1-next.738bb1424.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/declarations.d.cjs +2 -0
- package/build/{index.js → index.cjs} +3 -3
- package/build/{matchers.js → matchers.cjs} +2 -2
- package/build/{supported-matchers.js → supported-matchers.cjs} +1 -1
- package/build/{types.js → types.cjs} +1 -1
- package/build-module/index.js +2 -2
- package/build-module/matchers.js +1 -1
- package/package.json +6 -4
- package/src/declarations.d.ts +46 -0
- package/src/index.ts +46 -0
- package/src/matchers.ts +121 -0
- package/src/supported-matchers.ts +8 -0
- package/src/test/index.test.ts +93 -0
- package/src/types.ts +34 -0
- package/build/declarations.d.js +0 -2
- /package/build/{declarations.d.js.map → declarations.d.cjs.map} +0 -0
- /package/build/{index.js.map → index.cjs.map} +0 -0
- /package/build/{matchers.js.map → matchers.cjs.map} +0 -0
- /package/build/{supported-matchers.js.map → supported-matchers.cjs.map} +0 -0
- /package/build/{types.js.map → types.cjs.map} +0 -0
|
@@ -23,8 +23,8 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
23
23
|
));
|
|
24
24
|
|
|
25
25
|
// packages/jest-console/src/index.ts
|
|
26
|
-
var import_matchers = require("./matchers");
|
|
27
|
-
var import_supported_matchers = __toESM(require("./supported-matchers"));
|
|
26
|
+
var import_matchers = require("./matchers.cjs");
|
|
27
|
+
var import_supported_matchers = __toESM(require("./supported-matchers.cjs"), 1);
|
|
28
28
|
var setConsoleMethodSpy = (args) => {
|
|
29
29
|
const [methodName, matcherName] = args;
|
|
30
30
|
const spy = jest.spyOn(console, methodName).mockName(`console.${methodName}`);
|
|
@@ -45,4 +45,4 @@ var setConsoleMethodSpy = (args) => {
|
|
|
45
45
|
afterEach(assertExpectedCalls);
|
|
46
46
|
};
|
|
47
47
|
Object.entries(import_supported_matchers.default).forEach(setConsoleMethodSpy);
|
|
48
|
-
//# sourceMappingURL=index.
|
|
48
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -24,7 +24,7 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
24
24
|
|
|
25
25
|
// packages/jest-console/src/matchers.ts
|
|
26
26
|
var import_jest_matcher_utils = require("jest-matcher-utils");
|
|
27
|
-
var import_supported_matchers = __toESM(require("./supported-matchers"));
|
|
27
|
+
var import_supported_matchers = __toESM(require("./supported-matchers.cjs"), 1);
|
|
28
28
|
var createErrorMessage = (spyInfo) => {
|
|
29
29
|
const { spy, pass, calls, matcherName, methodName, expected } = spyInfo;
|
|
30
30
|
const hint = pass ? `.not${matcherName}` : matcherName;
|
|
@@ -90,4 +90,4 @@ expect.extend(
|
|
|
90
90
|
{}
|
|
91
91
|
)
|
|
92
92
|
);
|
|
93
|
-
//# sourceMappingURL=matchers.
|
|
93
|
+
//# sourceMappingURL=matchers.cjs.map
|
|
@@ -16,4 +16,4 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
16
16
|
// packages/jest-console/src/types.ts
|
|
17
17
|
var types_exports = {};
|
|
18
18
|
module.exports = __toCommonJS(types_exports);
|
|
19
|
-
//# sourceMappingURL=types.
|
|
19
|
+
//# sourceMappingURL=types.cjs.map
|
package/build-module/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// packages/jest-console/src/index.ts
|
|
2
|
-
import "./matchers";
|
|
3
|
-
import supportedMatchers from "./supported-matchers";
|
|
2
|
+
import "./matchers.js";
|
|
3
|
+
import supportedMatchers from "./supported-matchers.js";
|
|
4
4
|
var setConsoleMethodSpy = (args) => {
|
|
5
5
|
const [methodName, matcherName] = args;
|
|
6
6
|
const spy = jest.spyOn(console, methodName).mockName(`console.${methodName}`);
|
package/build-module/matchers.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// packages/jest-console/src/matchers.ts
|
|
2
2
|
import { matcherHint, printExpected, printReceived } from "jest-matcher-utils";
|
|
3
|
-
import supportedMatchers from "./supported-matchers";
|
|
3
|
+
import supportedMatchers from "./supported-matchers.js";
|
|
4
4
|
var createErrorMessage = (spyInfo) => {
|
|
5
5
|
const { spy, pass, calls, matcherName, methodName, expected } = spyInfo;
|
|
6
6
|
const hint = pass ? `.not${matcherName}` : matcherName;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wordpress/jest-console",
|
|
3
|
-
"version": "8.36.1-next.
|
|
3
|
+
"version": "8.36.1-next.738bb1424.0",
|
|
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,17 +25,19 @@
|
|
|
25
25
|
"npm": ">=8.19.2"
|
|
26
26
|
},
|
|
27
27
|
"files": [
|
|
28
|
+
"src",
|
|
28
29
|
"build",
|
|
29
30
|
"build-module",
|
|
30
31
|
"build-types"
|
|
31
32
|
],
|
|
32
|
-
"
|
|
33
|
+
"type": "module",
|
|
34
|
+
"main": "build/index.cjs",
|
|
33
35
|
"module": "build-module/index.js",
|
|
34
36
|
"exports": {
|
|
35
37
|
".": {
|
|
36
38
|
"types": "./build-types/index.d.ts",
|
|
37
39
|
"import": "./build-module/index.js",
|
|
38
|
-
"require": "./build/index.
|
|
40
|
+
"require": "./build/index.cjs"
|
|
39
41
|
},
|
|
40
42
|
"./package.json": "./package.json"
|
|
41
43
|
},
|
|
@@ -49,5 +51,5 @@
|
|
|
49
51
|
"publishConfig": {
|
|
50
52
|
"access": "public"
|
|
51
53
|
},
|
|
52
|
-
"gitHead": "
|
|
54
|
+
"gitHead": "ab1b004c0d61c295aa34bc86ea07f979343983ce"
|
|
53
55
|
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// Definitions originally written by Damien Sorel <https://github.com/mistic100> under MIT license.
|
|
2
|
+
// https://github.com/DefinitelyTyped/DefinitelyTyped/blob/f0b72c12f6b561e4342dc8a1cf87432d2ad40ae7/types/wordpress__jest-console/index.d.ts
|
|
3
|
+
|
|
4
|
+
declare namespace jest {
|
|
5
|
+
interface Matchers< R, T > {
|
|
6
|
+
/**
|
|
7
|
+
* Ensure that `console.error` function was called.
|
|
8
|
+
*/
|
|
9
|
+
toHaveErrored(): R;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Ensure that `console.error` function was called with specific arguments.
|
|
13
|
+
*/
|
|
14
|
+
toHaveErroredWith( ...args: unknown[] ): R;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Ensure that `console.info` function was called.
|
|
18
|
+
*/
|
|
19
|
+
toHaveInformed(): R;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Ensure that `console.info` function was called with specific arguments.
|
|
23
|
+
*/
|
|
24
|
+
toHaveInformedWith( ...args: unknown[] ): R;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Ensure that `console.log` function was called.
|
|
28
|
+
*/
|
|
29
|
+
toHaveLogged(): R;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Ensure that `console.log` function was called with specific arguments.
|
|
33
|
+
*/
|
|
34
|
+
toHaveLoggedWith( ...args: unknown[] ): R;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Ensure that `console.warn` function was called.
|
|
38
|
+
*/
|
|
39
|
+
toHaveWarned(): R;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Ensure that `console.warn` function was called with specific arguments.
|
|
43
|
+
*/
|
|
44
|
+
toHaveWarnedWith( ...args: unknown[] ): R;
|
|
45
|
+
}
|
|
46
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
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
|
+
);
|
|
@@ -0,0 +1,93 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
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;
|
package/build/declarations.d.js
DELETED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|