'use strict';
const toThrowMatchers = require('expect/build/toThrowMatchers').default;
const builtInToThrow = toThrowMatchers.toThrow;
let newErrorFormat = false;
try {
null.test();
} catch (error) {
if (error.message.includes('Cannot read properties of null')) {
newErrorFormat = true;
}
}
const regex = /Cannot read property '([^']+)' of (.+)/;
function normalizeErrorMessage(message) {
if (newErrorFormat) {
const match = message.match(regex);
if (match) {
return `Cannot read properties of ${match[2]} (reading '${match[1]}')`;
}
}
return message;
}
function toThrow(value, expectedValue) {
if (typeof expectedValue === 'string') {
expectedValue = normalizeErrorMessage(expectedValue);
} else if (expectedValue instanceof Error) {
expectedValue.message = normalizeErrorMessage(expectedValue.message);
}
return builtInToThrow.call(this, value, expectedValue);
}
module.exports = {
toThrow,
};