Skip to content

Commit 70c1bfe

Browse files
committed
Decode errors in production test suite from minified versions
1 parent 23e098a commit 70c1bfe

1 file changed

Lines changed: 40 additions & 0 deletions

File tree

scripts/jest/test-framework-setup.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,46 @@ if (process.env.REACT_CLASS_EQUIVALENCE_TEST) {
66
require('./setupSpecEquivalenceReporter.js');
77
} else {
88
var env = jasmine.getEnv();
9+
var existingErrorMap = require('../error-codes/codes.json');
10+
11+
function wrapUserCode(fn) {
12+
return function() {
13+
try {
14+
return fn.apply(this, arguments);
15+
} catch (err) {
16+
if (!global.__DEV__) {
17+
if (err && typeof err.message === 'string') {
18+
const re = /error-decoder.html\?invariant=(\d+)([^\s]*)/;
19+
const matches = err.message.match(re);
20+
if (matches && matches.length === 3) {
21+
const code = parseInt(matches[1], 10);
22+
const args = matches[2]
23+
.split('&')
24+
.filter(s => s.startsWith('args[]='))
25+
.map(s => s.substr('args[]='.length))
26+
.map(decodeURIComponent);
27+
const format = existingErrorMap[code];
28+
let argIndex = 0;
29+
err.message = format.replace(/%s/g, () => args[argIndex++]);
30+
}
31+
}
32+
}
33+
throw err;
34+
}
35+
}
36+
}
37+
38+
env.beforeEach(() => {
39+
const matchers = global[Symbol.for('$$jest-matchers-object')].matchers;
40+
const toThrow = matchers.toThrow;
41+
matchers.toThrow = (actual, expected) => {
42+
return toThrow(wrapUserCode(actual), expected);
43+
};
44+
const toThrowError = matchers.toThrowError;
45+
matchers.toThrowError = (actual, expected) => {
46+
return toThrowError(wrapUserCode(actual), expected);
47+
};
48+
});
949

1050
// TODO: Stop using spyOn in all the test since that seem deprecated.
1151
// This is a legacy upgrade path strategy from:

0 commit comments

Comments
 (0)