Files
KaTeX/test/setup.js
ylemkimon 71035c7111 Refactor test helpers (#1336)
* Refactor test helpers

* Combine common codes from `parse` and `build`, and dispatch using
`Mode`
* Remove `toNotBuild` and `toNotParse` and use `expect.not`
* Remove `Warning` and instead mock `console.warn`
* Add `expect.toHavePassed` and check whether parse/build succeeded
lazily
* Improve failed test `message`:
  - Use color
  - Print stack traces(excluding internals)
  - Print diff
  - Follow jest matcher output style

* Update helpers.js

* Remove toBeTruthy checks

getParsed throws an error if parsing fails.

* Used tagged literals

* Use .toHaveLength

* Use tagged literals

* Use to{Build,Parse}Like where possible

* Remove compareParseTree

* Use snapshot where possible

* Join into one line where <88 chars

* Revert console.warn() to throw an error

Merge `expectToWarn` to `expectKaTeX`, like
`expect.toFailWithParseError`

* Remove call to console.warn from stack traces

* Fix merge errors

* Remove `getTree`

* Move `_getBuilt` into `getBuilt`
* Move default settings and tagging literal support in to `getParsed` 
and `getBuilt`
* Remove stack traces clean-up
* Remove `toHavePassed` matcher

* Extract `expected` string construction into `printExpectedResult`
2018-07-21 19:06:07 -07:00

72 lines
1.8 KiB
JavaScript

/* global expect: false */
import stringify from 'json-stable-stringify';
import ParseError from "../src/ParseError";
import {
Mode, ConsoleWarning,
expectKaTeX, expectEquivalent,
} from "./helpers";
// Serializer support
const typeFirstCompare = (a, b) => {
if (a.key === 'type') {
return -1;
} else if (b.key === 'type') {
return 1;
} else {
return a.key < b.key ? -1 : 1;
}
};
const regExpReplacer = (key, value) => {
return value instanceof RegExp ? {lastIndex: value.lastIndex} : value;
};
const serializer = {
print(val) {
return stringify(val, {
cmp: typeFirstCompare,
space: ' ',
replacer: regExpReplacer,
});
},
test(val) {
// Leave strings (e.g. XML) to other serializers
return typeof val !== "string";
},
};
expect.addSnapshotSerializer(serializer);
// Mock console.warn to throw an error
global.console.warn = x => { throw new ConsoleWarning(x); };
// Expect extensions
expect.extend({
toParse(expr, settings) {
return expectKaTeX(expr, settings, Mode.PARSE, this.isNot);
},
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);
},
});