@wordpress/jest-console 8.37.0 → 8.37.1-next.06ee73755.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.
Files changed (31) hide show
  1. package/LICENSE.md +1 -1
  2. package/build/declarations.d.cjs +2 -0
  3. package/build/{index.js → index.cjs} +5 -4
  4. package/build/{index.js.map → index.cjs.map} +2 -2
  5. package/build/{matchers.js → matchers.cjs} +2 -2
  6. package/build/{supported-matchers.js → supported-matchers.cjs} +1 -1
  7. package/build/{types.js → types.cjs} +1 -1
  8. package/build-module/declarations.d.mjs +1 -0
  9. package/build-module/{index.js → index.mjs} +5 -4
  10. package/build-module/{index.js.map → index.mjs.map} +2 -2
  11. package/build-module/{matchers.js → matchers.mjs} +2 -2
  12. package/build-module/{supported-matchers.js → supported-matchers.mjs} +1 -1
  13. package/build-module/types.mjs +1 -0
  14. package/package.json +14 -8
  15. package/src/declarations.d.ts +46 -0
  16. package/src/index.ts +54 -0
  17. package/src/matchers.ts +121 -0
  18. package/src/supported-matchers.ts +8 -0
  19. package/src/test/index.test.ts +93 -0
  20. package/src/types.ts +34 -0
  21. package/build/declarations.d.js +0 -2
  22. package/build-module/declarations.d.js +0 -1
  23. package/build-module/types.js +0 -1
  24. /package/build/{declarations.d.js.map → declarations.d.cjs.map} +0 -0
  25. /package/build/{matchers.js.map → matchers.cjs.map} +0 -0
  26. /package/build/{supported-matchers.js.map → supported-matchers.cjs.map} +0 -0
  27. /package/build/{types.js.map → types.cjs.map} +0 -0
  28. /package/build-module/{declarations.d.js.map → declarations.d.mjs.map} +0 -0
  29. /package/build-module/{matchers.js.map → matchers.mjs.map} +0 -0
  30. /package/build-module/{supported-matchers.js.map → supported-matchers.mjs.map} +0 -0
  31. /package/build-module/{types.js.map → types.mjs.map} +0 -0
package/LICENSE.md CHANGED
@@ -1,6 +1,6 @@
1
1
  ## Gutenberg
2
2
 
3
- Copyright 2016-2025 by the contributors
3
+ Copyright 2016-2026 by the contributors
4
4
 
5
5
  **License for Contributions (on and after April 15, 2021)**
6
6
 
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ //# sourceMappingURL=declarations.d.cjs.map
@@ -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"));
28
28
  var setConsoleMethodSpy = (args) => {
29
29
  const [methodName, matcherName] = args;
30
30
  const spy = jest.spyOn(console, methodName).mockName(`console.${methodName}`);
@@ -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
- expect(console).not[matcherName]();
37
+ const name = matcherName;
38
+ expect(console).not[name]();
38
39
  }
39
40
  }
40
41
  beforeAll(resetSpy);
@@ -45,4 +46,4 @@ var setConsoleMethodSpy = (args) => {
45
46
  afterEach(assertExpectedCalls);
46
47
  };
47
48
  Object.entries(import_supported_matchers.default).forEach(setConsoleMethodSpy);
48
- //# sourceMappingURL=index.js.map
49
+ //# sourceMappingURL=index.cjs.map
@@ -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[ matcherName ]();\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;AAC9D,aAAQ,OAAQ,EAAE,IAAK,WAAY,EAAE;AAAA,IACtC;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;",
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
  }
@@ -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"));
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.js.map
93
+ //# sourceMappingURL=matchers.cjs.map
@@ -30,4 +30,4 @@ var supportedMatchers = {
30
30
  warn: "toHaveWarned"
31
31
  };
32
32
  var supported_matchers_default = supportedMatchers;
33
- //# sourceMappingURL=supported-matchers.js.map
33
+ //# sourceMappingURL=supported-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.js.map
19
+ //# sourceMappingURL=types.cjs.map
@@ -0,0 +1 @@
1
+ //# sourceMappingURL=declarations.d.mjs.map
@@ -1,6 +1,6 @@
1
1
  // packages/jest-console/src/index.ts
2
- import "./matchers";
3
- import supportedMatchers from "./supported-matchers";
2
+ import "./matchers.mjs";
3
+ import supportedMatchers from "./supported-matchers.mjs";
4
4
  var setConsoleMethodSpy = (args) => {
5
5
  const [methodName, matcherName] = args;
6
6
  const spy = jest.spyOn(console, methodName).mockName(`console.${methodName}`);
@@ -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
- expect(console).not[matcherName]();
13
+ const name = matcherName;
14
+ expect(console).not[name]();
14
15
  }
15
16
  }
16
17
  beforeAll(resetSpy);
@@ -21,4 +22,4 @@ var setConsoleMethodSpy = (args) => {
21
22
  afterEach(assertExpectedCalls);
22
23
  };
23
24
  Object.entries(supportedMatchers).forEach(setConsoleMethodSpy);
24
- //# sourceMappingURL=index.js.map
25
+ //# sourceMappingURL=index.mjs.map
@@ -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[ matcherName ]();\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;AAC9D,aAAQ,OAAQ,EAAE,IAAK,WAAY,EAAE;AAAA,IACtC;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;",
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
  }
@@ -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.mjs";
4
4
  var createErrorMessage = (spyInfo) => {
5
5
  const { spy, pass, calls, matcherName, methodName, expected } = spyInfo;
6
6
  const hint = pass ? `.not${matcherName}` : matcherName;
@@ -66,4 +66,4 @@ expect.extend(
66
66
  {}
67
67
  )
68
68
  );
69
- //# sourceMappingURL=matchers.js.map
69
+ //# sourceMappingURL=matchers.mjs.map
@@ -9,4 +9,4 @@ var supported_matchers_default = supportedMatchers;
9
9
  export {
10
10
  supported_matchers_default as default
11
11
  };
12
- //# sourceMappingURL=supported-matchers.js.map
12
+ //# sourceMappingURL=supported-matchers.mjs.map
@@ -0,0 +1 @@
1
+ //# sourceMappingURL=types.mjs.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wordpress/jest-console",
3
- "version": "8.37.0",
3
+ "version": "8.37.1-next.06ee73755.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,23 +25,29 @@
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
- "main": "build/index.js",
33
- "module": "build-module/index.js",
33
+ "main": "build/index.cjs",
34
+ "module": "build-module/index.mjs",
34
35
  "exports": {
35
36
  ".": {
36
37
  "types": "./build-types/index.d.ts",
37
- "import": "./build-module/index.js",
38
- "require": "./build/index.js"
38
+ "import": "./build-module/index.mjs",
39
+ "require": "./build/index.cjs"
39
40
  },
40
41
  "./package.json": "./package.json"
41
42
  },
42
- "types": "build-types",
43
+ "types": "index.d.ts",
43
44
  "dependencies": {
44
- "jest-matcher-utils": "^29.6.2"
45
+ "jest-matcher-utils": "^29.6.2",
46
+ "jest-mock": "^29.6.2"
47
+ },
48
+ "devDependencies": {
49
+ "@testing-library/jest-dom": "^6.6.3",
50
+ "@types/jest": "^29.5.14"
45
51
  },
46
52
  "peerDependencies": {
47
53
  "jest": ">=29"
@@ -49,5 +55,5 @@
49
55
  "publishConfig": {
50
56
  "access": "public"
51
57
  },
52
- "gitHead": "2cf13ec6cf86153c9b3cf369bf5c59046f5cd950"
58
+ "gitHead": "fd315436f44683a993d5f053789b5279d95b2872"
53
59
  }
@@ -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,54 @@
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 );
@@ -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,8 @@
1
+ const supportedMatchers: Record< string, string > = {
2
+ error: 'toHaveErrored',
3
+ info: 'toHaveInformed',
4
+ log: 'toHaveLogged',
5
+ warn: 'toHaveWarned',
6
+ };
7
+
8
+ export default supportedMatchers;
@@ -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;
@@ -1,2 +0,0 @@
1
- "use strict";
2
- //# sourceMappingURL=declarations.d.js.map
@@ -1 +0,0 @@
1
- //# sourceMappingURL=declarations.d.js.map
@@ -1 +0,0 @@
1
- //# sourceMappingURL=types.js.map
File without changes
File without changes
File without changes