import { assert } from 'chai';
import { describe, it } from 'mocha';
import * as LoggedError from '../../../main/ts/api/LoggedError';
import * as Reporter from '../../../main/ts/api/Reporter';
import { AssertionError, HtmlDiffAssertionError } from '../../../main/ts/api/TestError';
const htmlAssertion = (): HtmlDiffAssertionError => {
const e: any = new Error('message"');
e.diff = {
expected: 'abc"hello"',
actual: 'ab"hello"',
comparison: 'blahhello"hello"'
};
e.label = '"label"1';
e.name = 'HtmlAssertion';
return e;
};
const assertion = (): AssertionError => {
const e: any = new Error('message"');
e.expected = 'abc"hello"';
e.actual = 'ab"hello"';
e.showDiff = true;
e.label = '"label"1';
e.name = 'AssertionError';
return e;
};
const cleanStack = (message: string): string =>
message.replace(/Stack:(\n|.)*/, 'Stack:\n');
describe('Reporter', () => {
it('Reports thrown js errors as html', () => {
try {
// noinspection ExceptionCaughtLocallyJS
throw new Error('blarg');
} catch (e) {
const actual = Reporter.html(LoggedError.loggedError(e, [ ' * Log Message' ]));
const expected = 'Error: blarg<span>\n\nLogs:\n * Log Message';
assert.deepEqual(actual, expected, 'Error message');
}
});
it('Reports thrown js errors with html in logs as html', () => {
try {
// noinspection ExceptionCaughtLocallyJS
throw new Error('blarg');
} catch (e) {
const actual = Reporter.html(LoggedError.loggedError(e, [
'PprintAssertionError: Checking attribute: "height" of '
]));
const expected = 'Error: blarg<span>\n\nLogs:\n' +
'PprintAssertionError: Checking attribute: "height" of <iframe src="http://www.example.com/" width="200" height="200"></iframe>';
assert.deepEqual(actual, expected, 'Error message');
}
});
it('Reports thrown js errors as text', () => {
try {
// noinspection ExceptionCaughtLocallyJS
throw new Error('blarg');
} catch (e) {
const actual = Reporter.text(LoggedError.loggedError(e, [ ' * Log Message' ]));
const expected = 'Error: blarg\n\nLogs:\n * Log Message';
assert.deepEqual(actual, expected, 'Error message');
}
});
it('Reports thrown HtmlDiffAssertionError errors as html', () => {
try {
// noinspection ExceptionCaughtLocallyJS
throw htmlAssertion();
} catch (e) {
const actual = Reporter.html(LoggedError.loggedError(e, []));
// NOTE: the and are supposed to remain
const expected =
'Test failure: message"\n' +
'Expected:\n' +
'abc"hello"\n' +
'Actual:\n' +
'ab"hello"\n' +
'Diff:\n' +
'blahhello"hello"<span>\n' +
'\n' +
'Stack:\n';
assert.deepEqual(cleanStack(actual), expected, 'Error message');
}
});
it('Reports thrown HtmlDiffAssertionError errors as text', () => {
try {
// noinspection ExceptionCaughtLocallyJS
throw htmlAssertion();
} catch (e) {
const actual = Reporter.text(LoggedError.loggedError(e, []));
const expected =
'Test failure: message"\n' +
'Expected:\n' +
'abc"hello"\n' +
'Actual:\n' +
'ab"hello"\n' +
'Diff:\n' +
'blahhello"hello"\n' +
'\n' +
'Stack:\n';
assert.deepEqual(cleanStack(actual), expected, 'Error message');
}
});
it('Reports thrown AssertionError errors as html', () => {
try {
// noinspection ExceptionCaughtLocallyJS
throw assertion();
} catch (e) {
const actual = Reporter.html(LoggedError.loggedError(e, []));
const expected =
'Assertion error: message"\n' +
'Expected:\n' +
'abc"hello"\n' +
'Actual:\n' +
'ab"hello"\n' +
'Diff:\n' +
'ab"hello"
abc"hello"
\n' +
'\n' +
'Stack:\n';
assert.deepEqual(cleanStack(actual), expected, 'Error message');
}
});
it('Reports thrown AssertionError errors as text', () => {
try {
// noinspection ExceptionCaughtLocallyJS
throw assertion();
} catch (e) {
const actual = Reporter.text(LoggedError.loggedError(e, []));
const expected =
'Assertion error: message"\n' +
'Expected:\n' +
'abc"hello"\n' +
'Actual:\n' +
'ab"hello"\n' +
'Diff:\n' +
'- | ab"hello"\n' +
'+ | abc"hello"\n' +
'\n' +
'Stack:\n';
assert.deepEqual(cleanStack(actual), expected, 'Error message');
}
});
});