Open trusktr opened 2 years ago
This lib works fine with babel-plugin-istanbul coverage test
babel-plugin-istanbul
to your root/babel.config.js
, not vue.config.js
// instrument code only during testing
const plugins = []
if (process.env.NODE_ENV === 'test') {
plugins.push([
"babel-plugin-istanbul", {
extension: ['.js', '.vue'],
}
])
}
module.exports = {
presets: [
'@vue/app'
],
plugins
}
jest || cypress
I refered to this article.
And I added [vue-template-babel-compiler](https://github.com/JuniorTour/vue-template-babel-compiler)
to this project, they all works fine.
Hello @JuniorTour, does that instrument JS expressions inside the <template>
, not just in the <script>
?
We can add instrument to template, but we have some obstacles from vue/component-compiler-utils
We can add instrument to template, by add option filename: 'newFilename.js'
like this:
// vue.config.js
module.exports = {
chainWebpack: (config) => {
config.module
.rule('vue')
.use('vue-loader')
.tap((options) => {
options.compilerOptions.babelOptions = {
filename: 'VueTemplateBabelCompilerCompiledRender.js',
}
options.compiler = require('vue-template-babel-compiler');
return options;
});
},
};
This option value should be default to source code, Welcome for Pull Request to change it: https://github.com/JuniorTour/vue-template-babel-compiler/blob/53a01c08fd49be58499e99c54af16c9d118ad896/src/renderCompiler.js#L17
Then the render function from vue template will get instrument like this:
function cov_1b2esh9sgc() {
// ...
}
cov_1b2esh9sgc()
var render = function {
// ...
}
Because vue-template-babel-compiler use babel to compile the render function:
node_modules/@vue/component-compiler-utils/dist/compileTemplate.js
will reformat the instrumented render function, and throw a error, this is the obstacle.// after process by @vue/component-compiler-utils
function render () {
// ...
}
cov_1b2esh9sgc() // Error: cov_1b2esh9sgc not defined
var render = function {
// ...
}
I am still thinking about solution, I want this lib can work for coverage too.
Your suggestions or pull request are also welcome. 😊
I'm trying to do this in Webpack:
hoping it will, for example, add coverage instrumentation for JS expressions inside
<template>
, but no luck.Any ideas?