-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
93 lines (86 loc) · 2.63 KB
/
index.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/**
* @file Entry point for the library. Exports test/print functions for Vitest/Jest.
* Also exports `vueMarkupFormatter` function for any other uses.
*/
import {
debugLogger,
isHtmlString,
isTestingLibraryVueContainer,
isTestingLibraryVueWrapper,
isVueTestUtilsWrapper,
isVueWrapper
} from './src/helpers.js';
import { loadOptions } from './src/loadOptions.js';
import { stringManipulation } from './src/stringManipulation.js';
import { formatMarkup } from './src/formatMarkup.js';
/**
* Test function for Vitest's serializer API.
* Determines whether to pass the markup through the print function.
*
* @param {string|object} received The markup or Vue wrapper to be formatted
* @return {boolean} true = Tells Vitest to run the print function
*/
export const test = function (received) {
const isHtml = isHtmlString(received);
// Because test is called so much, we want to skip instantiating
// the debug object unless debugging is enabled.
if (globalThis.vueSnapshots?.debug) {
debugLogger({
function: 'index.js:test',
details: [
'Vue 3 Snapshot Serializer will only run on a string of',
'HTML (first character is \'<\'), a Vue-Test-Utils wrapper,',
'or a @Testing-Library/Vue render wrapper or container.'
].join(' '),
data: {
isHtml,
isVueTestUtilsWrapper: isVueTestUtilsWrapper(received),
isTestingLibraryVueWrapper: isTestingLibraryVueWrapper(received),
isTestingLibraryVueContainer: isTestingLibraryVueContainer(received),
received
}
});
}
return isHtml || isVueWrapper(received);
};
/**
* Print function for Vitest's serializer API.
* Formats markup according to options.
*
* @param {string|object} received The markup or Vue wrapper to be formatted
* @return {string} The formatted markup
*/
export const print = function (received) {
debugLogger({
function: 'index.js:print',
data: { received }
});
loadOptions();
let html = received || '';
html = stringManipulation(html);
return formatMarkup(html);
};
/**
* For external use. Takes in a string of HTML, applies changes to
* the markup (based on global vueSnapshots settings). Then
* formats the markup.
*
* @param {string} html The markup to be formatted.
* @return {string} The formatted markup.
*/
export const vueMarkupFormatter = function (html) {
debugLogger({
function: 'index.js:vueMarkupFormatter',
data: { html }
});
loadOptions();
if (!isHtmlString(html)) {
return html;
}
html = stringManipulation(html);
return formatMarkup(html);
};
export default {
test,
print
};