Skip to content

Commit df2bfa2

Browse files
committed
docs: document the custom block processor feature
1 parent 7a2e92a commit df2bfa2

File tree

3 files changed

+63
-2
lines changed

3 files changed

+63
-2
lines changed

README.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,73 @@ vue-jest compiles the script and template of SFCs into a JavaScript file that Je
6363
- **typescript** (`lang="ts"`, `lang="typescript"`)
6464
- **coffeescript** (`lang="coffee"`, `lang="coffeescript"`)
6565

66+
### Natively Supported Vue custom blocks
67+
68+
- **vue-i18n** (`<i18n lang="json">`, `<i18n lang="yaml">`)
69+
6670
### Global Jest options
6771

6872
You can change the behavior of `vue-jest` by using `jest.globals`.
6973

74+
#### Supporting custom blocks
75+
76+
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](https://github.com/kazupon/vue-i18n) `<i18n>` custom blocks are supported in unit tests.
77+
78+
A `package.json` Example
79+
80+
```json
81+
{
82+
"jest": {
83+
"moduleFileExtensions": ["js", "json", "vue"],
84+
"transform": {
85+
"^.+\\.js$": "babel-jest",
86+
"^.+\\.vue$": "vue-jest"
87+
},
88+
"globals": {
89+
"vue-jest": {
90+
"transform": {
91+
"your-custom-block": "./custom-block-processor.js"
92+
}
93+
}
94+
}
95+
}
96+
}
97+
```
98+
7099
> _Tip:_ Need programmatic configuration? Use the [--config](https://jestjs.io/docs/en/cli.html#config-path) option in Jest CLI, and export a `.js` file
71100

101+
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.
102+
103+
```js
104+
module.exports = {
105+
globals: {
106+
'vue-jest': {
107+
transform: {
108+
'your-custom-block': require('./custom-block-processor')
109+
}
110+
}
111+
}
112+
}
113+
```
114+
115+
#### Writing a processor
116+
117+
Processors must return an object with a "process" method, like so...
118+
119+
```js
120+
module.exports = {
121+
/**
122+
* Process the content inside of a custom block and prepare it for execution in a testing environment
123+
* @param {SFCCustomBlock[]} blocks All of the blocks matching your type, returned from `@vue/component-compiler-utils`
124+
* @param {string} vueOptionsNamespace The internal namespace for a component's Vue Options in vue-jest
125+
* @param {string} filename The SFC file being processed
126+
* @param {Object} config The full Jest config
127+
* @returns {string} The code to be output after processing all of the blocks matched by this type
128+
*/
129+
process(blocks, vueOptionsNamepsace, filename, config) {}
130+
}
131+
```
132+
72133
#### babelConfig
73134

74135
Provide `babelConfig` in one of the following formats:

lib/process-custom-blocks.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ function applyTransformer(
88
filename,
99
config
1010
) {
11-
return transformer.process(blocks, vueOptionsNamespace, filename, config)
11+
return transformer.process({ blocks, vueOptionsNamespace, filename, config })
1212
}
1313

1414
function groupByType(acc, block) {

lib/transformers/i18n.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ function parseLanguageAndContent(block, filename) {
3939
}
4040

4141
module.exports = {
42-
process(blocks, vueOptionsNamespace, filename) {
42+
process({ blocks, vueOptionsNamespace, filename }) {
4343
const base = `${vueOptionsNamespace}.${VUE_I18N_OPTION} = []`
4444
const codes = blocks.map(block => {
4545
if (block.type === 'i18n') {

0 commit comments

Comments
 (0)