@wordpress/jest-console 8.37.1-next.ba3aee3a2.0 → 8.38.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wordpress/jest-console",
3
- "version": "8.37.1-next.ba3aee3a2.0",
3
+ "version": "8.38.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,22 +25,21 @@
25
25
  "npm": ">=8.19.2"
26
26
  },
27
27
  "files": [
28
- "src",
29
28
  "build",
30
29
  "build-module",
31
- "build-types"
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": "./build-types/index.d.ts",
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": "index.d.ts",
42
+ "types": "declarations.d.ts",
44
43
  "dependencies": {
45
44
  "jest-matcher-utils": "^29.6.2",
46
45
  "jest-mock": "^29.6.2"
@@ -55,5 +54,5 @@
55
54
  "publishConfig": {
56
55
  "access": "public"
57
56
  },
58
- "gitHead": "67d2e486fcd40c753591cf911ca0659132f519ca"
57
+ "gitHead": "50c4c0f51e4797c217946ce42adfaa5eb026f33f"
59
58
  }
@@ -1,2 +0,0 @@
1
- "use strict";
2
- //# sourceMappingURL=declarations.d.cjs.map
@@ -1,7 +0,0 @@
1
- {
2
- "version": 3,
3
- "sources": [],
4
- "sourcesContent": [],
5
- "mappings": "",
6
- "names": []
7
- }
@@ -1 +0,0 @@
1
- //# sourceMappingURL=declarations.d.mjs.map
@@ -1,7 +0,0 @@
1
- {
2
- "version": 3,
3
- "sources": [],
4
- "sourcesContent": [],
5
- "mappings": "",
6
- "names": []
7
- }
@@ -1,5 +0,0 @@
1
- /**
2
- * Internal dependencies
3
- */
4
- import './matchers';
5
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,YAAY,CAAC"}
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=matchers.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"matchers.d.ts","sourceRoot":"","sources":["../src/matchers.ts"],"names":[],"mappings":""}
@@ -1,3 +0,0 @@
1
- declare const supportedMatchers: Record<string, string>;
2
- export default supportedMatchers;
3
- //# sourceMappingURL=supported-matchers.d.ts.map
@@ -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"}
@@ -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,54 +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
- // Using 'as' to satisfy TypeScript compiler about the matcher name.
33
- type MatcherName = `toHave${
34
- | 'Errored'
35
- | 'Informed'
36
- | 'Logged'
37
- | 'Warned' }`;
38
- const name = matcherName as MatcherName;
39
-
40
- expect( console ).not[ name ]();
41
- }
42
- }
43
-
44
- beforeAll( resetSpy );
45
-
46
- beforeEach( () => {
47
- assertExpectedCalls();
48
- resetSpy();
49
- } );
50
-
51
- afterEach( assertExpectedCalls );
52
- };
53
-
54
- 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
- );
@@ -1,8 +0,0 @@
1
- const supportedMatchers: Record< string, string > = {
2
- error: 'toHaveErrored',
3
- info: 'toHaveInformed',
4
- log: 'toHaveLogged',
5
- warn: 'toHaveWarned',
6
- };
7
-
8
- export default supportedMatchers;
@@ -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