vue-jest compiles <script />, <template />, and <style /> blocks with supported lang attributes into JavaScript that Jest can run.
Supported script languages
typescript (lang="ts", lang="typescript")
coffeescript (lang="coffee", lang="coffeescript")
Global Jest options
You can change the behavior of vue-jest by using jest.globals.
Supporting custom blocks
A great feature of the Vue SFC compiler is that it can support custom blocks. You might want to use those blocks in your tests. To render out custom blocks for testing purposes, you'll need to write a transformer. Once you have your transformer, you'll add an entry to vue-jest's transform map. This is how vue-i18n's<i18n> custom blocks are supported in unit tests.
Tip: Need programmatic configuration? Use the --config option in Jest CLI, and export a .js file
A jest.config.js Example - If you're using a dedicated configuration file like you can reference and require your processor in the config file instead of using a file reference.
Processors must return an object with a "process" method, like so...
module.exports = {
/**
* Process the content inside of a custom block and prepare it for execution in a testing environment
* @param {SFCCustomBlock[]} blocks All of the blocks matching your type, returned from `@vue/component-compiler-utils`
* @param {string} vueOptionsNamespace The internal namespace for a component's Vue Options in vue-jest
* @param {string} filename The SFC file being processed
* @param {Object} config The full Jest config
* @returns {string} The code to be output after processing all of the blocks matched by this type
*/
process({ blocks, vueOptionsNamepsace, filename, config }) {}
}
babelConfig
Provide babelConfig in one of the following formats:
<Boolean>
<Object>
<String>
Boolean
true - Enable Babel processing. vue-jest will try to find Babel configuration using find-babel-config.
This is the default behavior if babelConfig is not defined.
The Sass compiler supports Jest's moduleNameMapper which is the suggested way of dealing with Webpack aliases. Webpack's sass-loader uses a special syntax for indicating non-relative imports, so you'll likely need to copy this syntax into your moduleNameMapper entries if you make use of it. For aliases of bare imports (imports that require node module resolution), the aliased value must also be prepended with this ~ or vue-jest's custom resolver won't recognize it.
{
"jest": {
"moduleNameMapper": {
"^~foo/(.*)": "<rootDir>/foo/$1",
// @import '~foo'; -> @import 'path/to/project/foo';
"^~bar/(.*)": "~baz/lib/$1"
// @import '~bar/qux'; -> @import 'path/to/project/node_modules/baz/lib/qux';
// Notice how the tilde (~) was needed on the bare import to baz.
}
}
}
To import globally included files (ie. variables, mixins, etc.), include them in the Jest configuration at jest.globals['vue-jest'].resources.scss: