diff --git a/src/tests/backend/assert-legacy.js b/src/tests/backend/assert-legacy.js new file mode 100644 index 000000000..b3760da9d --- /dev/null +++ b/src/tests/backend/assert-legacy.js @@ -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; diff --git a/src/tests/backend/specs/caching_middleware.js b/src/tests/backend/specs/caching_middleware.js index a40ada7d8..e3eef98b7 100644 --- a/src/tests/backend/specs/caching_middleware.js +++ b/src/tests/backend/specs/caching_middleware.js @@ -7,7 +7,7 @@ */ const common = require('../common'); -const assert = require('assert').strict; +const assert = require('../assert-legacy').strict; const queryString = require('querystring'); const settings = require('../../../node/utils/Settings'); diff --git a/src/tests/backend/specs/hooks.js b/src/tests/backend/specs/hooks.js index 9dbf6974b..e601c9344 100644 --- a/src/tests/backend/specs/hooks.js +++ b/src/tests/backend/specs/hooks.js @@ -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');