cypress-fail-on-console-error 2.1.4 → 3.2.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/e2e.js`
14
14
 
15
15
  ```js
16
16
  import failOnConsoleError from 'cypress-fail-on-console-error';
@@ -20,24 +20,25 @@ 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 | <div style="width:300px">Description</div> |
24
+ |--- |--- |--- |
25
+ | `excludeMessages` | `undefined` | Exclude console messages from throwing `AssertionError`. Regular expression parameters are acceptable. String parameters will be interpreted as regular expression. [String.match()](https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/String/match) will be used for evaluation. Be sure to [escape the string regular expression](https://javascript.info/regexp-escaping) for special characters. When console message property `stacktrace` exists, then the whole stacktrace can be matched. |
26
+ | `includeConsoleTypes` | `[consoleType.ERROR]` | Define console types for observation
27
+ | `cypressLog` | `false` | Enable debug logs for errorMessage_excludeMessage_match and errorMessage_excluded to cypress runner
27
28
 
28
29
  <br/>
29
30
 
30
- <!-- prettier-ignore -->
31
31
  ```js
32
32
  import failOnConsoleError, { consoleType } from 'cypress-fail-on-console-error';
33
33
 
34
34
  const config = {
35
- excludeMessages: ['foo', '^some bar-regex.*'],
35
+ excludeMessages: ['foo', /^some bar-regex.*/],
36
36
  includeConsoleTypes: [
37
37
  consoleType.ERROR,
38
38
  consoleType.WARN,
39
39
  consoleType.INFO,
40
40
  ],
41
+ cypressLog: true,
41
42
  };
42
43
 
43
44
  failOnConsoleError(config);
@@ -45,6 +46,7 @@ failOnConsoleError(config);
45
46
  // excludeMessages[0] matches example console message 'this is a foo message'
46
47
  // excludeMessages[1] matches example console message 'some bar-regex message'
47
48
  // includeConsoleTypes observe console types ERROR, WARN and INFO
49
+ //debug information will be printed to the cypress runner
48
50
  ```
49
51
 
50
52
  Using Javascript, consoleType Enum can be parsed as number values
@@ -58,3 +60,11 @@ failOnConsoleError({
58
60
  // 1 = WARN
59
61
  // 2 = ERROR
60
62
  ```
63
+
64
+ ### Debugging
65
+ When Cypress log is activated, debug information about the matching and exclude process are available from the cypress runner. As a plus, the generated error message string can be verified.
66
+ ![image info](./docs/cypressLogTrue.png)
67
+
68
+ ### Contributing
69
+ 1. Create an project issue with proper description and expected behaviour
70
+ 2. Provide a PR with implementation and tests. Command `npm run verify` have to pass locally
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,10 +26,13 @@ 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
+ var chai_1 = require("chai");
32
+ var os_1 = require("os");
31
33
  var sinon = __importStar(require("sinon"));
32
34
  var sinon_chai_1 = __importDefault(require("sinon-chai"));
35
+ var type_detect_1 = __importDefault(require("type-detect"));
33
36
  var ConsoleType_1 = require("./types/ConsoleType");
34
37
  chai.should();
35
38
  chai.use(sinon_chai_1.default);
@@ -38,21 +41,15 @@ function failOnConsoleError(config) {
38
41
  var spies;
39
42
  (0, exports.validateConfig)(config);
40
43
  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
- }
44
+ Cypress.on('window:before:load', function (window) {
45
+ spies = (0, exports.createSpies)(config, window.console);
48
46
  });
49
47
  Cypress.on('command:end', function () {
50
- if (!spies || !(0, exports.someSpyCalled)(spies)) {
48
+ if (!spies)
51
49
  return;
52
- }
53
- var spy = (0, exports.getIncludedSpy)(spies, config);
54
- if (spy) {
55
- chai.expect(spy).to.have.callCount(0);
50
+ var errorMessage = (0, exports.getIncludedCall)(spies, config);
51
+ if (errorMessage) {
52
+ throw new chai_1.AssertionError("cypress-fail-on-console-error: ".concat(os_1.EOL, " ").concat(errorMessage));
56
53
  }
57
54
  });
58
55
  }
@@ -61,25 +58,29 @@ var validateConfig = function (config) {
61
58
  if (config.excludeMessages) {
62
59
  chai.expect(config.excludeMessages).not.to.be.empty;
63
60
  config.excludeMessages.forEach(function (_excludeMessage) {
64
- chai.expect(_excludeMessage).to.be.a('string');
65
- chai.expect(_excludeMessage).to.have.length.above(0);
61
+ chai.expect((0, type_detect_1.default)(_excludeMessage)).to.be.oneOf([
62
+ 'string',
63
+ 'RegExp',
64
+ ]);
65
+ chai.expect(_excludeMessage.toString()).to.have.length.above(0);
66
66
  });
67
67
  }
68
68
  if (config.includeConsoleTypes) {
69
69
  chai.expect(config.includeConsoleTypes).not.to.be.empty;
70
70
  config.includeConsoleTypes.forEach(function (_includeConsoleType) {
71
- return chai.expect((0, ConsoleType_1.someConsoleType)(_includeConsoleType), "includeConsoleTypes '".concat(_includeConsoleType, "' is an unknown ConsoleType")).to.be.true;
71
+ chai.expect((0, ConsoleType_1.someConsoleType)(_includeConsoleType), "includeConsoleTypes '".concat(_includeConsoleType, "' is an unknown ConsoleType")).to.be.true;
72
72
  });
73
73
  }
74
74
  };
75
75
  exports.validateConfig = validateConfig;
76
76
  var createConfig = function (config) {
77
- var _a;
77
+ var _a, _b;
78
78
  return ({
79
79
  excludeMessages: config.excludeMessages,
80
80
  includeConsoleTypes: ((_a = config.includeConsoleTypes) === null || _a === void 0 ? void 0 : _a.length)
81
81
  ? config.includeConsoleTypes
82
82
  : [ConsoleType_1.ConsoleType.ERROR],
83
+ cypressLog: (_b = config.cypressLog) !== null && _b !== void 0 ? _b : false,
83
84
  });
84
85
  };
85
86
  exports.createConfig = createConfig;
@@ -93,43 +94,69 @@ var createSpies = function (config, console) {
93
94
  return spies;
94
95
  };
95
96
  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); });
97
+ var getIncludedCall = function (spies, config) {
98
+ var errorMessage;
99
+ Array.from(spies.values()).forEach(function (spy) {
100
+ if (!spy.called)
101
+ return;
102
+ var includedCall = (0, exports.findIncludedCall)(spy, config);
103
+ if (includedCall !== undefined) {
104
+ errorMessage = includedCall;
105
+ }
106
+ });
107
+ return errorMessage;
105
108
  };
106
- exports.getIncludedSpy = getIncludedSpy;
107
- var someIncludedCall = function (spy, config) {
108
- if (!config.excludeMessages) {
109
- return true;
109
+ exports.getIncludedCall = getIncludedCall;
110
+ var findIncludedCall = function (spy, config) {
111
+ var errorMessages = spy.args.map(function (call) { return (0, exports.callToString)(call); });
112
+ if (config.excludeMessages === undefined) {
113
+ return errorMessages[0];
110
114
  }
111
- return spy.args.some(function (call) {
112
- return !(0, exports.isExcludedMessage)(config.excludeMessages, (0, exports.callToString)(call));
115
+ return errorMessages.find(function (_errorMessage) {
116
+ var _isErrorMessageExcluded = config.excludeMessages.some(function (_excludeMessage) {
117
+ return (0, exports.isErrorMessageExcluded)(_errorMessage, _excludeMessage, config.cypressLog);
118
+ });
119
+ if (config.cypressLog === true) {
120
+ (0, exports.cypressLogger)('errorMessage_excluded', {
121
+ _errorMessage: _errorMessage,
122
+ _isErrorMessageExcluded: _isErrorMessageExcluded,
123
+ });
124
+ }
125
+ return !_isErrorMessageExcluded;
113
126
  });
114
127
  };
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
- });
128
+ exports.findIncludedCall = findIncludedCall;
129
+ var isErrorMessageExcluded = function (errorMessage, excludeMessage, cypressLog) {
130
+ var _a;
131
+ var match = (((_a = errorMessage.match(excludeMessage)) === null || _a === void 0 ? void 0 : _a.length) || 0) > 0;
132
+ if (cypressLog) {
133
+ (0, exports.cypressLogger)('errorMessage_excludeMessage_match', {
134
+ errorMessage: errorMessage,
135
+ excludeMessage: excludeMessage,
136
+ match: match,
137
+ });
138
+ }
139
+ return match;
122
140
  };
123
- exports.isExcludedMessage = isExcludedMessage;
141
+ exports.isErrorMessageExcluded = isErrorMessageExcluded;
124
142
  var callToString = function (calls) {
125
143
  return calls
126
144
  .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);
145
+ var _a;
146
+ var _value = (_a = currentValue === null || currentValue === void 0 ? void 0 : currentValue.stack) !== null && _a !== void 0 ? _a : currentValue;
147
+ var _currentValue = typeof _value !== 'string' ? JSON.stringify(_value) : _value;
130
148
  return "".concat(previousValue, " ").concat(_currentValue);
131
149
  }, '')
132
150
  .trim();
133
151
  };
134
152
  exports.callToString = callToString;
153
+ var cypressLogger = function (name, message) {
154
+ Cypress.log({
155
+ name: name,
156
+ displayName: name,
157
+ message: JSON.stringify(message),
158
+ consoleProps: function () { return message; },
159
+ });
160
+ };
161
+ exports.cypressLogger = cypressLogger;
135
162
  exports.consoleType = ConsoleType_1.ConsoleType;
@@ -1,5 +1,6 @@
1
1
  import { ConsoleType } from "./ConsoleType";
2
2
  export interface Config {
3
- excludeMessages?: string[];
3
+ excludeMessages?: (string | RegExp)[];
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.2.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
- "verify": "npm run build && npm run prettier && npm run test",
19
- "ci:prettier": "prettier --check '**/*'"
18
+ "verify": "npm run build && npm run ci:prettier && npm run test",
19
+ "ci:prettier": "prettier --check \"**/*\""
20
20
  },
21
21
  "repository": {
22
22
  "type": "git",
@@ -42,13 +42,15 @@
42
42
  "@types/chai": "4.3.1",
43
43
  "@types/expect": "24.3.0",
44
44
  "@types/mocha": "9.1.1",
45
- "@types/sinon": "10.0.11",
45
+ "@types/sinon": "10.0.12",
46
46
  "@types/sinon-chai": "3.2.8",
47
- "cypress": "9.7.0",
47
+ "@types/type-detect": "4.0.1",
48
+ "cypress": "10.3.1",
48
49
  "mocha": "10.0.0",
49
- "prettier": "2.6.2",
50
- "prettier-plugin-organize-imports": "2.3.4",
50
+ "prettier": "2.7.1",
51
+ "prettier-plugin-organize-imports": "3.0.0",
52
+ "rimraf": "3.0.2",
51
53
  "ts-mocha": "10.0.0",
52
- "typescript": "4.7.2"
54
+ "typescript": "4.7.4"
53
55
  }
54
56
  }