forked from KaTeX/KaTeX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.js
78 lines (65 loc) · 1.91 KB
/
setup.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import stringify from 'json-stable-stringify';
import Lexer from "../src/Lexer";
import ParseError from "../src/ParseError";
import {
Mode, ConsoleWarning,
expectKaTeX, expectEquivalent,
} from "./helpers";
// JSON serializer
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 replacer = (key, value) => {
if (value instanceof Lexer) {
return {
input: value.input,
// omit value.settings
lastIndex: value.tokenRegex.lastIndex,
};
} else {
return value;
}
};
const serializer = {
print(val) {
return stringify(val, {
cmp: typeFirstCompare,
space: ' ',
replacer: replacer,
});
},
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, settings) {
return expectKaTeX(expr, settings, 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);
},
});