unit tests (electron) - avoid sending circular structures over

This commit is contained in:
Benjamin Pasero 2021-04-21 08:13:08 +02:00
parent 23a2409675
commit 7095f4706c
No known key found for this signature in database
GPG key ID: E6380CC4C8219E65
2 changed files with 35 additions and 4 deletions

View file

@ -85,6 +85,12 @@ function importMochaReporter(name) {
function deserializeError(err) { function deserializeError(err) {
const inspect = err.inspect; const inspect = err.inspect;
err.inspect = () => inspect; err.inspect = () => inspect;
if (err.actual) {
err.actual = JSON.parse(err.actual).value;
}
if (err.expected) {
err.expected = JSON.parse(err.expected).value;
}
return err; return err;
} }

View file

@ -5,7 +5,7 @@
/*eslint-env mocha*/ /*eslint-env mocha*/
(function() { (function () {
const fs = require('fs'); const fs = require('fs');
const originals = {}; const originals = {};
let logging = false; let logging = false;
@ -21,7 +21,7 @@
}; };
function createSpy(element, cnt) { function createSpy(element, cnt) {
return function(...args) { return function (...args) {
if (logging) { if (logging) {
console.log(`calling ${element}: ` + args.slice(0, cnt).join(',') + (withStacks ? (`\n` + new Error().stack.split('\n').slice(2).join('\n')) : '')); console.log(`calling ${element}: ` + args.slice(0, cnt).join(',') + (withStacks ? (`\n` + new Error().stack.split('\n').slice(2).join('\n')) : ''));
} }
@ -213,14 +213,39 @@ function serializeError(err) {
return { return {
message: err.message, message: err.message,
stack: err.stack, stack: err.stack,
actual: err.actual, actual: safeStringify({ value: err.actual }),
expected: err.expected, expected: safeStringify({ value: err.expected }),
uncaught: err.uncaught, uncaught: err.uncaught,
showDiff: err.showDiff, showDiff: err.showDiff,
inspect: typeof err.inspect === 'function' ? err.inspect() : '' inspect: typeof err.inspect === 'function' ? err.inspect() : ''
}; };
} }
function safeStringify(obj) {
const seen = new Set();
return JSON.stringify(obj, (key, value) => {
if (isObject(value) || Array.isArray(value)) {
if (seen.has(value)) {
return '[Circular]';
} else {
seen.add(value);
}
}
return value;
});
}
function isObject(obj) {
// The method can't do a type cast since there are type (like strings) which
// are subclasses of any put not positvely matched by the function. Hence type
// narrowing results in wrong results.
return typeof obj === 'object'
&& obj !== null
&& !Array.isArray(obj)
&& !(obj instanceof RegExp)
&& !(obj instanceof Date);
}
class IPCReporter { class IPCReporter {
constructor(runner) { constructor(runner) {