Skip to content

Commit

Permalink
tests: add assert helper for supporting node 10.x
Browse files Browse the repository at this point in the history
  • Loading branch information
webzwo0i authored and rhansen committed Feb 15, 2021
1 parent ed02606 commit a550d2f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
48 changes: 48 additions & 0 deletions src/tests/backend/assert-legacy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
'use strict';
// support for older node versions (<12)
const assert = require('assert');

const internalMatch = (string, regexp, message, fn) => {
if (!regexp.test) {
throw new Error('regexp parameter is not a RegExp');
}
if (typeof string !== 'string') {
throw new Error('string parameter is not a string');
}
const match = fn.name === 'match';

const result = string.match(regexp);
if (match && !result) {
if (message) {
throw message;
} else {
throw new Error(`${string} does not match regex ${regexp}`);
}
}
if (!match && result) {
if (message) {
throw message;
} else {
throw new Error(`${string} does match regex ${regexp}`);
}
}
};


if (!assert.match) {
const match = (string, regexp, message) => {
internalMatch(string, regexp, message, match);
};
assert.match = match;
}
if (!assert.strict.match) assert.strict.match = assert.match;

if (!assert.doesNotMatch) {
const doesNotMatch = (string, regexp, message) => {
internalMatch(string, regexp, message, doesNotMatch);
};
assert.doesNotMatch = doesNotMatch;
}
if (!assert.strict.doesNotMatch) assert.strict.doesNotMatch = assert.doesNotMatch;

module.exports = assert;
2 changes: 1 addition & 1 deletion src/tests/backend/specs/caching_middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

const common = require('../common');
const assert = require('assert').strict;
const assert = require('../assert-legacy').strict;
const url = require('url');
const queryString = require('querystring');
const settings = require('../../../node/utils/Settings');
Expand Down
2 changes: 1 addition & 1 deletion src/tests/backend/specs/hooks.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const assert = require('assert').strict;
const assert = require('../assert-legacy').strict;
const hooks = require('../../../static/js/pluginfw/hooks');
const plugins = require('../../../static/js/pluginfw/plugin_defs');
const sinon = require('sinon');
Expand Down

0 comments on commit a550d2f

Please sign in to comment.