Jest Serializer for Vue Snapshots
jest-serializer-vue-tjw is a Jest snapshot serializer specifically designed to make Vue component snapshots more readable and maintainable by normalizing HTML output. It handles common issues like attribute ordering, inline functions, and various `data-` attributes that can cause unnecessary snapshot churn. The package is currently at version 4.0.0, but this version primarily serves as a deprecation notice. The library is no longer actively maintained and has been replaced by `vue3-snapshot-serializer`. While `jest-serializer-vue-tjw` supported both Vue 2 and Vue 3 (with some caveats for the latter), its replacement is Vue 3-only and introduces several new features and improved configuration options. Users are advised to migrate to `vue3-snapshot-serializer` for new projects or Vue 3 codebases, or to pin this library to `3.x.x` if they must remain on Vue 2.
Common errors
-
Error: Jest: a snapshot serializer must be a string or a module
cause Incorrectly specifying the serializer path or importing it directly instead of providing the module path string in `jest.config.js`.fixEnsure your `jest.config.js`'s `snapshotSerializers` array contains the string path to the serializer, e.g., `"<rootDir>/node_modules/jest-serializer-vue-tjw"`. -
Snapshots changed unexpectedly (diff shows attribute reordering)
cause The `sortAttributes` feature was enabled by default in v3.18.0, causing attributes within HTML tags to be sorted alphabetically.fixThis is often an intentional improvement. Accept the new snapshots or set `sortAttributes: false` in your configuration if you need to preserve original attribute order. -
Snapshots show 'function () { /* istanbul ignore next */ ... }' artifactscause Code coverage tools like Istanbul inject comments into compiled code, which can appear in snapshots when inline functions are present and `removeIstanbulComments` is not active.fixUpgrade to v3.16.0 or later, which enables `removeIstanbulComments` by default, or explicitly set `removeIstanbulComments: true` in your serializer configuration. -
My Vue 3 project snapshots are not working correctly or the serializer is not recommended.
cause This library has limited support for Vue 3 and is officially deprecated in favor of a new, dedicated Vue 3 serializer.fixMigrate your project to use `vue3-snapshot-serializer` for full Vue 3 compatibility and access to new features. Follow the migration guide in the `jest-serializer-vue-tjw` README.
Warnings
- breaking The library is deprecated as of v4.0.0. While v4.0.0 introduces no functional changes from v3.x.x, it adds a deprecation notice on install and strongly recommends migration to `vue3-snapshot-serializer`.
- breaking Improved arrow function detection in v3.20.0 may cause some snapshots to change around inline functions. Comments might also be restored to previous styles.
- breaking Attributes are sorted by default since v3.18.0. This can lead to snapshot diffs where attribute order changes.
- breaking In v3.16.0, the `removeIstanbulComments` setting was added and enabled by default to clean up Istanbul-injected flags in snapshots when running with code coverage.
- breaking A major bump of the HTML parser in v3.15.0 (4.1.0 -> 5.0.0) might cause minor formatting changes, such as end tags appearing on new lines.
- gotcha Versions prior to `3.14.0` might format empty tags (`<div></div>`) differently when `addInputValues` and `stringifyObjects` are false, potentially splitting them onto two lines.
Install
-
npm install jest-serializer-vue-tjw -
yarn add jest-serializer-vue-tjw -
pnpm add jest-serializer-vue-tjw
Imports
- Snapshot Serializer Configuration
"snapshotSerializers": [ "<rootDir>/node_modules/jest-serializer-vue-tjw" ]
- Configuration options in vue.config.js
import { configure } from 'jest-serializer-vue-tjw'// vue.config.js module.exports = { pluginOptions: { jestSerializerVue: { removeDataTest: true } } }
Quickstart
{
"jest": {
"snapshotSerializers": [
"<rootDir>/node_modules/jest-serializer-vue-tjw"
]
},
"jest-serializer-vue-tjw": {
"removeDataTest": true,
"sortAttributes": true,
"clearInlineFunctions": true,
"attributesToClear": ["data-random", "id"]
}
}