-
Notifications
You must be signed in to change notification settings - Fork 14
/
setup-test.js
43 lines (32 loc) · 1.08 KB
/
setup-test.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
const escapeStringRegexp = require('escape-string-regexp');
const dedent = require('dedent');
function dedentStrings(strings) {
const PATTERN_TOKEN = '__PATTERN_TOKEN__';
const stringWithPatterns = strings.join(PATTERN_TOKEN);
const dedentedString = dedent(stringWithPatterns);
const dedentedStringWithoutQuotes = dedentedString.slice(1, -1);
const dedentedStrings = dedentedStringWithoutQuotes.split(PATTERN_TOKEN);
return dedentedStrings;
}
expect.extend({
toMatchStringWithPatterns(received, strings, ...patterns) {
const dedentedStrings = dedentStrings(strings);
let regexpString = '';
dedentedStrings.forEach((string, index) => {
regexpString += escapeStringRegexp(string);
if (patterns[index]) {
regexpString += patterns[index].source;
}
});
regexpString = '^' + regexpString + '$';
const regex = new RegExp(regexpString);
const pass = regex.test(received);
return {
pass,
message: () =>
`expected the output to ${
pass ? 'not ' : ''
}match the string with patterns`,
};
},
});