Skip to content

Commit

Permalink
Revert console.warn() to throw an error
Browse files Browse the repository at this point in the history
Merge `expectToWarn` to `expectKaTeX`, like
`expect.toFailWithParseError`
  • Loading branch information
ylemkimon committed Jun 18, 2018
1 parent aeb5350 commit 4e84e93
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 67 deletions.
88 changes: 30 additions & 58 deletions test/helpers.js
@@ -1,5 +1,4 @@
/* global expect: false */
/* global jest: false */

import katex from "../katex";
import ParseError from "../src/ParseError";
Expand All @@ -10,6 +9,13 @@ import diff from 'jest-diff';
import {RECEIVED_COLOR, printReceived, printExpected} from 'jest-matcher-utils';
import {formatStackTrace, separateMessageFromStack} from 'jest-message-util';

export function ConsoleWarning(message) {
Error.captureStackTrace(this, this.constructor);
this.name = this.constructor.name;
this.message = message;
}
Object.setPrototypeOf(ConsoleWarning.prototype, Error.prototype);

/**
* Return the first raw string if x is tagged literal. Otherwise return x.
*/
Expand All @@ -26,7 +32,7 @@ const printActualErrorMessage = error => {
if (error) {
const {message, stack} = separateMessageFromStack(error.stack);
return (
`Instead, it threw:\n` +
'Instead, it threw:\n' +
RECEIVED_COLOR(
` ${message}` +
formatStackTrace(
Expand All @@ -45,7 +51,7 @@ const printActualErrorMessage = error => {
)
);
}
return `But it didn't throw anything.`;
return 'But it didn\'t throw anything.';
};

export const nonstrictSettings = new Settings({strict: false});
Expand Down Expand Up @@ -130,47 +136,46 @@ const getTree = (expr, settings, mode) => {
return result._tree;
};

export const expectKaTeX = (expr, settings = new Settings(), mode,
expectFail, expected) => {
export const expectKaTeX = (expr, settings = new Settings(), mode, isNot,
expectedError) => {
expr = r(expr); // support tagging literals
let pass = true; // whether succeeded
let pass = expectedError == null;
let _tree;
let error;
try {
_tree = mode.apply(expr, settings);
} catch (e) {
error = e;
if (e instanceof ParseError) {
pass = expected !== undefined &&
e.message !== `KaTeX parse error: ${expected}`;
pass = expectedError === ParseError || (typeof expectedError ===
"string" && e.message === `KaTeX parse error: ${expectedError}`);
} else if (e instanceof ConsoleWarning) {
pass = expectedError === ConsoleWarning;
} else {
pass = !!expectFail; // always fail if error is not ParserError
pass = !!isNot; // always fail
}
}

let expected;
if (expectedError == null) {
expected = (isNot ? 'fail ' : 'success ') + mode.noun;
} else {
expected = (isNot ? 'not throw a ' : `fail ${mode.noun} with a `) +
(expectedError.name || `ParseError matching "${expectedError}"`);
}
return {
pass,
message: expectFail
? () =>
`Expected the expression to fail ${mode.noun} with ParseError` +
(expected ? ` matching ${printExpected(expected)}` : '') +
`:\n ${printReceived(expr)}\n` +
printActualErrorMessage(error)
: () =>
`Expected the expression to ` + (expected
? `not throw ParserError matching ${printExpected(expected)}`
: `success ${mode.noun}`) +
`:\n ${printReceived(expr)}\n` +
printActualErrorMessage(error),
message: () => 'Expected the expression to ' + expected +
`:\n ${printReceived(expr)}\n` +
printActualErrorMessage(error),
_tree, // jest allows the return value of matcher to have custom properties
};
};

export const expectEquivalent = (actual, expected, settings, mode, expand) => {
const actualTree = getTree(actual, settings, mode);
const expectedTree = getTree(expected, settings, mode);
const pass = JSON.stringify(stripPositions(actualTree)) ===
JSON.stringify(stripPositions(expectedTree));
const actualTree = stripPositions(getTree(actual, settings, mode));
const expectedTree = stripPositions(getTree(expected, settings, mode));
const pass = JSON.stringify(actualTree) === JSON.stringify(expectedTree);

return {
pass,
Expand All @@ -189,36 +194,3 @@ export const expectEquivalent = (actual, expected, settings, mode, expand) => {
},
};
};

export const expectToWarn = (expr, settings) => {
const oldConsoleWarn = global.console.warn;
const mockConsoleWarn = jest.fn();

global.console.warn = mockConsoleWarn;
expect(expr).toBuild(settings);
global.console.warn = oldConsoleWarn;
const length = mockConsoleWarn.mock.calls.length;

return {
pass: length > 0,
message: length > 0
? () => {
let warnings = '';
for (let i = 0; i < length; i++) {
warnings += ` ${mockConsoleWarn.mock.calls[i][0]}`;
if (i !== length - 1) {
warnings += '\n';
}
}

return `Expected the expression to not generate a warning:\n` +
` ${printReceived(expr)}\n` +
`Instead, it generated:\n` +
RECEIVED_COLOR(warnings);
}
: () =>
`Expected the expression to generate a warning:\n` +
` ${printReceived(expr)}\n` +
`But it didn't generate any warning.`,
};
};
21 changes: 12 additions & 9 deletions test/setup.js
@@ -1,9 +1,10 @@
/* global expect: false */

import stringify from 'json-stable-stringify';
import ParseError from "../src/ParseError";
import {
Mode,
expectKaTeX, expectEquivalent, expectToWarn,
Mode, ConsoleWarning,
expectKaTeX, expectEquivalent,
} from "./helpers";

// Serializer support
Expand All @@ -30,6 +31,9 @@ const serializer = {

expect.addSnapshotSerializer(serializer);

// Mock console.warn to throw an error
global.console.warn = x => { throw new ConsoleWarning(x); };

// Expect extensions

expect.extend({
Expand All @@ -39,24 +43,23 @@ expect.extend({
return expectKaTeX(expr, settings, Mode.PARSE, this.isNot);
},

toFailWithParseError: function(expr, expected) {
const result = expectKaTeX(expr, undefined, Mode.PARSE, !this.isNot,
expected);
result.pass = !result.pass; // expectKaTeX.pass is true if succeeded
return result;
toFailWithParseError: function(expr, expected = ParseError) {
return expectKaTeX(expr, undefined, Mode.PARSE, this.isNot, expected);
},

toBuild(expr, settings) {
return expectKaTeX(expr, settings, Mode.BUILD, this.isNot);
},

toWarn(expr, settings) {
return expectKaTeX(expr, settings, Mode.BUILD, this.isNot, ConsoleWarning);
},

toParseLike(expr, expected, settings) {
return expectEquivalent(expr, expected, settings, Mode.PARSE, this.expand);
},

toBuildLike(expr, expected, settings) {
return expectEquivalent(expr, expected, settings, Mode.BUILD, this.expand);
},

toWarn: (expr, settings) => expectToWarn(expr, settings),
});

0 comments on commit 4e84e93

Please sign in to comment.