Skip to content

Commit b0180ee

Browse files
Add Input Value
* Move files around * Documentation * Add in different data types * Have Travis verify Node support * Force npm version in Travis * Improve stringify * EOL * Bump and Deps * segregate date because travis * Prevent test bleed
1 parent 1e7e6bb commit b0180ee

29 files changed

+3364
-3441
lines changed

.travis.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
sudo: required
22
dist: trusty
33
language: node_js
4+
# This ensures we don't accidentally stop supporting our lowest supported Node version
45
node_js:
5-
- "13"
6+
- "8.3.0"
7+
# However Node 8.3.0 comes with npm 5.3.0.
8+
# npm 5.7.1 is the first version to support npm ci
69
install:
10+
- npm install -g [email protected]
711
- npm ci
812
script:
913
- npm run travis

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,9 @@ module.exports = {
211211
removeDataVId: true,
212212
removeIdTest: false,
213213
removeServerRendered: true,
214-
stringifyObjects: false
214+
addInputValues: false,
215+
stringifyObjects: false,
216+
verbose: true
215217
}
216218
}
217219
};
@@ -229,6 +231,8 @@ removeDataQa | `false` | Removes `data-qa="whatever"` from you
229231
removeDataVId | `true` | Removes `data-v-1234abcd=""` from your snapshots. Important if a 3rd-party component uses scoped styles, to prevent ID changes from breaking your `mount` based tests when updating a dependency.
230232
removeIdTest | `false` | Removes `id="test-whatever"` or `id="testWhatever"`from snapshots. **Warning:** You should never use ID's for test tokens, as they can also be used by JS and CSS, making them more brittle. Use `data-test-id` instead.
231233
removeServerRendered | `true` | Removes `data-server-rendered="true"` from your snapshots if true.
234+
verbose | `true` | Logs to the console errors or other messages if true. **Strongly recommended** if using experimental features.
235+
addInputValues | `false` | **EXPERIMENTAL** Displays the value of form fields. `<input>` becomes `<input value="whatever">` in your snapshots. Requires you pass in `wrapper`, not `wrapper.html()`. On deeply nested components, it may exceed callstack.
232236
stringifyObjects | `false` | **EXPERIMENTAL** Replaces `title="[object Object]"` with `title="{a:'asdf'}"` in your snapshots, allowing you to see the data in the snapshot. Requires you to pass in `wrapper`, not `wrapper.html()`. This is still a work in progress. On deeply nested componets, it may exceed callstack.
233237

234238

@@ -294,16 +298,16 @@ describe('YourComponent.vue', () => {
294298
});
295299
```
296300

297-
**How do I opt out of stringifyObjects for one test?** - This is actually much easier. Stringify objects can only be done on a Vue VNode. So if you do `.html()` prior to sending it, it will always skip the `stringifyObjects` code. This allows you to use this experimental feature more easily, while opting out of the more troublesome tests.
301+
**How do I opt out of stringifyObjects or addInputValues for one test?** - This is actually much easier. These experimetnal features can only be done on a Vue VNode. So if you do `.html()` prior to sending it, it will always skip these transforms. This allows you to use these experimental feature more easily, while opting out of the more troublesome tests.
298302

299303
```js
300304
test('Assuming stringifyObjects is enabled', () => {
301305
const wrapper = shallowMount(YourComponent);
302306

303307
expect(wrapper)
304-
.toMatchSnapshot('Stringify objects');
308+
.toMatchSnapshot('Stringify objects and add input values');
305309

306310
expect(wrapper.html())
307-
.toMatchSnapshot('Opt out of stringify objects');
311+
.toMatchSnapshot('Opt out of stringify objects and adding input values');
308312
});
309313
```

index.js

Lines changed: 7 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1,106 +1,11 @@
11
const beautify = require('js-beautify').html;
22
const fs = require('fs');
3+
const _cloneDeep = require('lodash.clonedeep');
34

45
const helpers = require('./src/helpers.js');
56
const loadOptions = require('./src/loadOptions.js');
6-
const replaceObjectObject = require('./src/replaceObjectObject.js');
7-
const removeTestTokens = require('./src/removeTestTokens.js');
8-
9-
/**
10-
* Determines if the passed in value is markup.
11-
*
12-
* @param {string} received The markup to be serialized
13-
* @return {boolean} true = value is HTML
14-
*/
15-
function isHtmlString (received) {
16-
return (
17-
received &&
18-
typeof(received) === 'string' &&
19-
received.startsWith('<')
20-
);
21-
}
22-
23-
/**
24-
* Determines if the passed in value is a Vue wrapper.
25-
*
26-
* @param {object} received The Vue wrapper containing the markup to be serialized
27-
* @return {boolean} true = value is a Vue wrapper
28-
*/
29-
function isVueWrapper (received) {
30-
return (
31-
received &&
32-
typeof(received) === 'object' &&
33-
typeof(received.isVueInstance) === 'function'
34-
);
35-
}
36-
37-
/**
38-
* This removes the data-server-rendered="true" from your snapshots.
39-
*
40-
* @param {string} html The markup being serialized
41-
* @param {object} options Options object for this serializer
42-
* @return {string} Modified HTML string
43-
*/
44-
function removeServerRenderedText (html, options) {
45-
if (!options || options.removeServerRendered) {
46-
const $ = helpers.$(html);
47-
48-
$('[data-server-rendered]').removeAttr('data-server-rendered');
49-
50-
return $.html();
51-
}
52-
return html;
53-
}
54-
55-
/**
56-
* This removes data-v-1234abcd="" from your snapshots.
57-
*
58-
* @param {string} html The markup being serialized
59-
* @param {object} options Options object for this serializer
60-
* @return {string} Modified HTML string
61-
*/
62-
function removeScopedStylesDataVIDAttributes (html, options) {
63-
if (!options || options.removeDataVId) {
64-
// [-\w]+ will catch 1 or more instaces of a-z, A-Z, 0-9, hyphen (-), or underscore (_)
65-
const regex = / data-v-[-\w]+/g;
66-
67-
// [' data-v-asdf=""', ' data-v-qwer', ' data-v-asdf']
68-
let dataVIds = html.match(regex) || [];
69-
// ['data-v-asdf', 'data-v-qwer', 'data-v-asdf']
70-
dataVIds = dataVIds.map(function (match) {
71-
return match.trim().replace('=""', '');
72-
});
73-
// ['data-v-asdf', 'data-v-qwer']
74-
dataVIds = Array.from(new Set(dataVIds));
75-
76-
const $ = helpers.$(html);
77-
dataVIds.forEach(function (attribute) {
78-
$('[' + attribute + ']').removeAttr(attribute);
79-
});
80-
81-
html = $.html();
82-
}
83-
return html;
84-
}
85-
86-
/**
87-
* This removes all HTML comments from your snapshots.
88-
* Normal <!---->
89-
* Multi-line <!-- \n asdf \n asdf \n -->
90-
* Containing HTML <!-- <div></div> -->
91-
*
92-
* @param {string} html The markup being serialized
93-
* @param {object} options Options object for this serializer
94-
* @return {string} Modified HTML string
95-
*/
96-
function removeAllComments (html, options) {
97-
if (options && options.removeComments) {
98-
// The best Stackoverflow has to offer.
99-
// Also removes a trailing newline if it exists.
100-
return html.replace(/(?=<!--)([\s\S]*?)-->(\n)?/g, '');
101-
}
102-
return html;
103-
}
7+
const stringManipulation = require('./src/stringManipulation.js');
8+
const vnodeManipulation = require('./src/vnodeManipulation.js');
1049

10510
module.exports = {
10611
/**
@@ -111,7 +16,7 @@ module.exports = {
11116
* @return {boolean} true = Tells Jest to run the print function
11217
*/
11318
test: function (received) {
114-
return isHtmlString(received) || isVueWrapper(received);
19+
return helpers.isHtmlString(received) || helpers.isVueWrapper(received);
11520
},
11621
/**
11722
* Print function for Jest's serializer API.
@@ -124,14 +29,10 @@ module.exports = {
12429
const options = loadOptions(fs);
12530

12631
let html = received || '';
127-
if (isVueWrapper(received)) {
128-
html = replaceObjectObject(received, options) || '';
129-
}
130-
html = removeServerRenderedText(html, options);
131-
html = removeTestTokens(html, options);
132-
html = removeScopedStylesDataVIDAttributes(html, options);
133-
html = removeAllComments(html, options);
32+
html = vnodeManipulation(html, options, _cloneDeep);
33+
html = stringManipulation(html, options);
13434

35+
// Format markup
13536
return beautify(html, options.formatting);
13637
}
13738
};

0 commit comments

Comments
 (0)