cypress-fail-on-console-error 2.1.4 → 3.0.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/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # cypress-fail-on-console-error
2
2
 
3
- This Plugin observes console.error() function from [window object](https://developer.mozilla.org/de/docs/Web/API/Window). It will automatically fail the executed cypress test as soon as the error function is executed once.
3
+ This Plugin observes `console.error()` function from [window object](https://developer.mozilla.org/de/docs/Web/API/Window). Cypress test will fail when the error function gets executed.
4
4
 
5
5
  ### Installation
6
6
 
@@ -10,7 +10,7 @@ npm install cypress-fail-on-console-error --save-dev
10
10
 
11
11
  ### Usage
12
12
 
13
- `cypress/support/index.ts`
13
+ `cypress/support/index.js`
14
14
 
15
15
  ```js
16
16
  import failOnConsoleError from 'cypress-fail-on-console-error';
@@ -20,10 +20,11 @@ failOnConsoleError();
20
20
 
21
21
  ### Config (optional)
22
22
 
23
- | Parameter | Default | Description |
24
- | --------------------- | --------------------- | ------------------------------------------------------------------------------------------------------------------------- |
25
- | `excludeMessages` | `undefined` | Exclude console messages from throwing `assertionError` <br /> String parameter will be interpreted as regular expression |
26
- | `includeConsoleTypes` | `[consoleType.ERROR]` | Include console types for observation |
23
+ | Parameter | Default | Description |
24
+ | --------------------- | --------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
25
+ | `excludeMessages` | `undefined` | Exclude console messages from throwing `AssertionError` <br /> When `console.error()` contains an error object from `new Error()`, then the whole stacktrace can be matched <br /> String parameter will be interpreted as regular expression |
26
+ | `includeConsoleTypes` | `[consoleType.ERROR]` | Include console types for observation |
27
+ | `cypressLog` | `false` | Include debug logs for `errorMessage_excludeMessage_match` and `errorMessage_excluded` to cypress runner |
27
28
 
28
29
  <br/>
29
30
 
@@ -38,6 +39,7 @@ const config = {
38
39
  consoleType.WARN,
39
40
  consoleType.INFO,
40
41
  ],
42
+ cypressLog: true,
41
43
  };
42
44
 
43
45
  failOnConsoleError(config);
package/dist/index.d.ts CHANGED
@@ -5,10 +5,9 @@ export default function failOnConsoleError(config?: Config): void;
5
5
  export declare const validateConfig: (config: Config) => void;
6
6
  export declare const createConfig: (config: Config) => Config;
7
7
  export declare const createSpies: (config: Config, console: Console) => Map<ConsoleType, sinon.SinonSpy>;
8
- export declare const resetSpies: (spies: Map<ConsoleType, sinon.SinonSpy>) => Map<ConsoleType, sinon.SinonSpy>;
9
- export declare const someSpyCalled: (spies: Map<ConsoleType, sinon.SinonSpy>) => boolean;
10
- export declare const getIncludedSpy: (spies: Map<ConsoleType, sinon.SinonSpy>, config: Config) => sinon.SinonSpy | undefined;
11
- export declare const someIncludedCall: (spy: sinon.SinonSpy, config: Config) => boolean;
12
- export declare const isExcludedMessage: (excludeMessages: string[], message: string) => boolean;
8
+ export declare const getIncludedCall: (spies: Map<ConsoleType, sinon.SinonSpy>, config: Config) => string | undefined;
9
+ export declare const findIncludedCall: (spy: sinon.SinonSpy, config: Config) => string | undefined;
10
+ export declare const isErrorMessageExcluded: (errorMessage: string, excludeMessage: string, cypressLog: boolean) => boolean;
13
11
  export declare const callToString: (calls: any[]) => string;
12
+ export declare const cypressLogger: (name: string, message: any) => void;
14
13
  export declare const consoleType: typeof ConsoleType;
package/dist/index.js CHANGED
@@ -26,7 +26,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
26
26
  return (mod && mod.__esModule) ? mod : { "default": mod };
27
27
  };
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.consoleType = exports.callToString = exports.isExcludedMessage = exports.someIncludedCall = exports.getIncludedSpy = exports.someSpyCalled = exports.resetSpies = exports.createSpies = exports.createConfig = exports.validateConfig = void 0;
29
+ exports.consoleType = exports.cypressLogger = exports.callToString = exports.isErrorMessageExcluded = exports.findIncludedCall = exports.getIncludedCall = exports.createSpies = exports.createConfig = exports.validateConfig = void 0;
30
30
  var chai = __importStar(require("chai"));
31
31
  var sinon = __importStar(require("sinon"));
32
32
  var sinon_chai_1 = __importDefault(require("sinon-chai"));
@@ -38,21 +38,15 @@ function failOnConsoleError(config) {
38
38
  var spies;
39
39
  (0, exports.validateConfig)(config);
40
40
  config = (0, exports.createConfig)(config);
41
- Cypress.on('window:before:load', function (win) {
42
- spies = (0, exports.createSpies)(config, win.console);
43
- });
44
- Cypress.on('command:enqueued', function () {
45
- if (spies) {
46
- spies = (0, exports.resetSpies)(spies);
47
- }
41
+ Cypress.on('window:before:load', function (window) {
42
+ spies = (0, exports.createSpies)(config, window.console);
48
43
  });
49
44
  Cypress.on('command:end', function () {
50
- if (!spies || !(0, exports.someSpyCalled)(spies)) {
45
+ if (!spies)
51
46
  return;
52
- }
53
- var spy = (0, exports.getIncludedSpy)(spies, config);
54
- if (spy) {
55
- chai.expect(spy).to.have.callCount(0);
47
+ var errorMessage = (0, exports.getIncludedCall)(spies, config);
48
+ if (errorMessage) {
49
+ chai.expect(errorMessage, 'console match found').to.be.undefined;
56
50
  }
57
51
  });
58
52
  }
@@ -68,18 +62,19 @@ var validateConfig = function (config) {
68
62
  if (config.includeConsoleTypes) {
69
63
  chai.expect(config.includeConsoleTypes).not.to.be.empty;
70
64
  config.includeConsoleTypes.forEach(function (_includeConsoleType) {
71
- return chai.expect((0, ConsoleType_1.someConsoleType)(_includeConsoleType), "includeConsoleTypes '".concat(_includeConsoleType, "' is an unknown ConsoleType")).to.be.true;
65
+ chai.expect((0, ConsoleType_1.someConsoleType)(_includeConsoleType), "includeConsoleTypes '".concat(_includeConsoleType, "' is an unknown ConsoleType")).to.be.true;
72
66
  });
73
67
  }
74
68
  };
75
69
  exports.validateConfig = validateConfig;
76
70
  var createConfig = function (config) {
77
- var _a;
71
+ var _a, _b;
78
72
  return ({
79
73
  excludeMessages: config.excludeMessages,
80
74
  includeConsoleTypes: ((_a = config.includeConsoleTypes) === null || _a === void 0 ? void 0 : _a.length)
81
75
  ? config.includeConsoleTypes
82
76
  : [ConsoleType_1.ConsoleType.ERROR],
77
+ cypressLog: (_b = config.cypressLog) !== null && _b !== void 0 ? _b : false,
83
78
  });
84
79
  };
85
80
  exports.createConfig = createConfig;
@@ -93,43 +88,69 @@ var createSpies = function (config, console) {
93
88
  return spies;
94
89
  };
95
90
  exports.createSpies = createSpies;
96
- var resetSpies = function (spies) {
97
- spies.forEach(function (_spy) { return _spy.resetHistory(); });
98
- return spies;
99
- };
100
- exports.resetSpies = resetSpies;
101
- var someSpyCalled = function (spies) { return Array.from(spies.values()).some(function (value) { return value.called; }); };
102
- exports.someSpyCalled = someSpyCalled;
103
- var getIncludedSpy = function (spies, config) {
104
- return Array.from(spies.values()).find(function (spy) { return spy.called && (0, exports.someIncludedCall)(spy, config); });
91
+ var getIncludedCall = function (spies, config) {
92
+ var errorMessage;
93
+ Array.from(spies.values()).forEach(function (spy) {
94
+ if (!spy.called)
95
+ return;
96
+ var includedCall = (0, exports.findIncludedCall)(spy, config);
97
+ if (includedCall !== undefined) {
98
+ errorMessage = includedCall;
99
+ }
100
+ });
101
+ return errorMessage;
105
102
  };
106
- exports.getIncludedSpy = getIncludedSpy;
107
- var someIncludedCall = function (spy, config) {
108
- if (!config.excludeMessages) {
109
- return true;
103
+ exports.getIncludedCall = getIncludedCall;
104
+ var findIncludedCall = function (spy, config) {
105
+ var errorMessages = spy.args.map(function (call) { return (0, exports.callToString)(call); });
106
+ if (config.excludeMessages === undefined) {
107
+ return errorMessages[0];
110
108
  }
111
- return spy.args.some(function (call) {
112
- return !(0, exports.isExcludedMessage)(config.excludeMessages, (0, exports.callToString)(call));
109
+ return errorMessages.find(function (_errorMessage) {
110
+ var _isErrorMessageExcluded = config.excludeMessages.some(function (_excludeMessage) {
111
+ return (0, exports.isErrorMessageExcluded)(_errorMessage, _excludeMessage, config.cypressLog);
112
+ });
113
+ if (config.cypressLog === true) {
114
+ (0, exports.cypressLogger)('errorMessage_excluded', {
115
+ _errorMessage: _errorMessage,
116
+ _isErrorMessageExcluded: _isErrorMessageExcluded,
117
+ });
118
+ }
119
+ return !_isErrorMessageExcluded;
113
120
  });
114
121
  };
115
- exports.someIncludedCall = someIncludedCall;
116
- var isExcludedMessage = function (excludeMessages, message) {
117
- return excludeMessages.some(function (_excludeMessage) {
118
- var _a;
119
- var hasMatch = ((_a = message.match(_excludeMessage)) === null || _a === void 0 ? void 0 : _a.length) || 0;
120
- return hasMatch > 0;
121
- });
122
+ exports.findIncludedCall = findIncludedCall;
123
+ var isErrorMessageExcluded = function (errorMessage, excludeMessage, cypressLog) {
124
+ var _a;
125
+ var match = (((_a = errorMessage.match(excludeMessage)) === null || _a === void 0 ? void 0 : _a.length) || 0) > 0;
126
+ if (cypressLog) {
127
+ (0, exports.cypressLogger)('errorMessage_excludeMessage_match', {
128
+ errorMessage: errorMessage,
129
+ excludeMessage: excludeMessage,
130
+ match: match,
131
+ });
132
+ }
133
+ return match;
122
134
  };
123
- exports.isExcludedMessage = isExcludedMessage;
135
+ exports.isErrorMessageExcluded = isErrorMessageExcluded;
124
136
  var callToString = function (calls) {
125
137
  return calls
126
138
  .reduce(function (previousValue, currentValue) {
127
- var _currentValue = typeof currentValue === 'string'
128
- ? currentValue
129
- : (currentValue === null || currentValue === void 0 ? void 0 : currentValue.message) || JSON.stringify(currentValue);
139
+ var _a;
140
+ var _value = (_a = currentValue === null || currentValue === void 0 ? void 0 : currentValue.stack) !== null && _a !== void 0 ? _a : currentValue;
141
+ var _currentValue = typeof _value !== 'string' ? JSON.stringify(_value) : _value;
130
142
  return "".concat(previousValue, " ").concat(_currentValue);
131
143
  }, '')
132
144
  .trim();
133
145
  };
134
146
  exports.callToString = callToString;
147
+ var cypressLogger = function (name, message) {
148
+ Cypress.log({
149
+ name: name,
150
+ displayName: name,
151
+ message: JSON.stringify(message),
152
+ consoleProps: function () { return message; },
153
+ });
154
+ };
155
+ exports.cypressLogger = cypressLogger;
135
156
  exports.consoleType = ConsoleType_1.ConsoleType;
@@ -2,4 +2,5 @@ import { ConsoleType } from "./ConsoleType";
2
2
  export interface Config {
3
3
  excludeMessages?: string[];
4
4
  includeConsoleTypes?: ConsoleType[];
5
+ cypressLog?: boolean;
5
6
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cypress-fail-on-console-error",
3
- "version": "2.1.4",
3
+ "version": "3.0.0",
4
4
  "description": "fail cypress test on console error",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -8,15 +8,15 @@
8
8
  "dist/**/*"
9
9
  ],
10
10
  "scripts": {
11
- "build": "rm -rf dist/ && tsc",
12
- "prettier": "prettier --write '**/*'",
11
+ "build": "npx rimraf dist/ && tsc",
12
+ "prettier": "prettier --write \"**/*\"",
13
13
  "test": "npm run test:ut && npm run test:it",
14
14
  "test:ut": "ts-mocha test/unitTest.ts",
15
15
  "test:ut:debug": "ts-mocha --inspect-brk test/unitTest.ts",
16
16
  "test:it": "ts-mocha test/integrationTest.ts --timeout 60000",
17
17
  "test:it:debug": "cypress open",
18
18
  "verify": "npm run build && npm run prettier && npm run test",
19
- "ci:prettier": "prettier --check '**/*'"
19
+ "ci:prettier": "prettier --check \"**/*\""
20
20
  },
21
21
  "repository": {
22
22
  "type": "git",
@@ -49,6 +49,7 @@
49
49
  "prettier": "2.6.2",
50
50
  "prettier-plugin-organize-imports": "2.3.4",
51
51
  "ts-mocha": "10.0.0",
52
- "typescript": "4.7.2"
52
+ "typescript": "4.7.3",
53
+ "rimraf": "3.0.2"
53
54
  }
54
55
  }