Add Jest-Serializer-Vue-TJW

This commit is contained in:
The Jared Wilcurt 2020-01-19 08:04:38 -05:00
parent 3db2da197c
commit 446b730430
5 changed files with 13 additions and 43 deletions

View File

@ -21,7 +21,7 @@ module.exports = {
'<rootDir>/tests/unit/setup.js'
],
snapshotSerializers: [
'<rootDir>/tests/unit/serializer.js'
'<rootDir>/node_modules/jest-serializer-vue-tjw'
],
testEnvironment: 'jest-environment-jsdom-global',
testMatch: [

10
package-lock.json generated
View File

@ -15616,6 +15616,16 @@
"pretty": "2.0.0"
}
},
"jest-serializer-vue-tjw": {
"version": "3.3.0",
"resolved": "https://registry.npmjs.org/jest-serializer-vue-tjw/-/jest-serializer-vue-tjw-3.3.0.tgz",
"integrity": "sha512-pvUqkwTYqmlHRuE6KtzNitBD5LRuuXgBt83cfSMabM9+QJEetm87nhIIAi4drDZeSOdkuVthBrcWE5E9AfUokw==",
"dev": true,
"requires": {
"lodash.clonedeep": "^4.5.0",
"pretty": "^2.0.0"
}
},
"jest-transform-stub": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/jest-transform-stub/-/jest-transform-stub-2.0.0.tgz",

View File

@ -69,6 +69,7 @@
"jest": "^24.9.0",
"jest-environment-jsdom": "^24.9.0",
"jest-environment-jsdom-global": "^1.2.0",
"jest-serializer-vue-tjw": "^3.3.0",
"jest-transform-stub": "^2.0.0",
"nw": "0.43.6-sdk",
"nw-vue-devtools-prebuilt": "^0.0.10",

View File

@ -1,42 +0,0 @@
// Based on jest-serializer-vue
const beautify = require('pretty');
function isHtmlString (received) {
return received && typeof(received) === 'string' && received.startsWith('<');
}
function isVueWrapper (received) {
return received && typeof(received) === 'object' && typeof(received.isVueInstance) === 'function';
}
// This removes data-test="whatever" from your snapshots
// If you also want to remove them from your production builds, see:
// https://forum.vuejs.org/t/how-to-remove-attributes-from-tags-inside-vue-components/24138
function removeDataTestAttributes (html) {
// [-\w]+ will catch 1 or more instaces of a-z, A-Z, 0-9, hyphen (-), or underscore (_)
return html.replace(/ data-test="[-\w]+"/g, '');
}
module.exports = {
test: function (received) {
return isHtmlString(received) || isVueWrapper(received);
},
print: function (received) {
let html = received || '';
if (isVueWrapper(received)) {
html = received.html();
}
html = removeDataTestAttributes(html);
// To see available options: https://github.com/beautify-web/js-beautify/blob/master/js/src/html/options.js
const options = {
indent_size: 2,
unformatted: ['code', 'pre'],
inline: [],
indent_inner_html: true,
indent_char: ' ',
sep: '\n'
};
return beautify(html, options);
}
};

View File

@ -22,6 +22,7 @@ window.getComputedStyle = function getComputedStyleStub (el) {
global.beforeEach(() => {
window.process = {
cwd: process.cwd,
env: {
NODE_ENV: 'development'
},