Why does Jest coverage (Istanbul) measure 0 branches in this Vue component method?
Please consider the following - somewhat contrived - Vue component:
<!-- FooBar.vue -->
<template>
<div @click="onClick">{{text}}</div>
</template>
<script>
export default {
name: 'foo-bar',
data() {
return {
text: 'foo'
}
},
methods: {
onClick() {
this.text = 'bar';
}
}
}
</script>
I've covered the component with a Jest unit test like so:
// FooBar.spec.js
import FooBar from '~/FooBar.vue';
import { shallowMount } from '@vue/test-utils';
import { expect } from 'chai';
describe('FooBar onClick()', () => {
it('should change the text to "bar"', () => {
// Arrange
const target = shallowMount(FooBar);
// Act
target.trigger('click');
// Assert
const div = target.find('div');
expect(div.text()).to.equal('bar');
});
});
The test passes green.
When I run Jest with --coverage
for this file, I get the following summary report:
=============================== Coverage summary ===============================
Statements : 0.1% ( 2/1868 )
Branches : 0% ( 0/1402 )
Functions : 0% ( 0/505 )
Lines : 0.2% ( 2/982 )
================================================================================
As you can see, the number of branches covered by the unit test is shown to be 0 - Even though Jest (or more accurately Instanbul, which Jest uses behind the scenes for coverage) did detect that the test covered two lines of code.
When I've made a tiny experiment and added an if
statement inside onClick()
like so:
onClick() {
if (this.text != undefined) {
this.text = 'bar';
}
}
then Jest did indeed count 1 branch covered.
My question is - Why does Jest / Istanbul not count the code in the onClick()
as a branch covered?
javascript vue.js jestjs istanbul
add a comment |
Please consider the following - somewhat contrived - Vue component:
<!-- FooBar.vue -->
<template>
<div @click="onClick">{{text}}</div>
</template>
<script>
export default {
name: 'foo-bar',
data() {
return {
text: 'foo'
}
},
methods: {
onClick() {
this.text = 'bar';
}
}
}
</script>
I've covered the component with a Jest unit test like so:
// FooBar.spec.js
import FooBar from '~/FooBar.vue';
import { shallowMount } from '@vue/test-utils';
import { expect } from 'chai';
describe('FooBar onClick()', () => {
it('should change the text to "bar"', () => {
// Arrange
const target = shallowMount(FooBar);
// Act
target.trigger('click');
// Assert
const div = target.find('div');
expect(div.text()).to.equal('bar');
});
});
The test passes green.
When I run Jest with --coverage
for this file, I get the following summary report:
=============================== Coverage summary ===============================
Statements : 0.1% ( 2/1868 )
Branches : 0% ( 0/1402 )
Functions : 0% ( 0/505 )
Lines : 0.2% ( 2/982 )
================================================================================
As you can see, the number of branches covered by the unit test is shown to be 0 - Even though Jest (or more accurately Instanbul, which Jest uses behind the scenes for coverage) did detect that the test covered two lines of code.
When I've made a tiny experiment and added an if
statement inside onClick()
like so:
onClick() {
if (this.text != undefined) {
this.text = 'bar';
}
}
then Jest did indeed count 1 branch covered.
My question is - Why does Jest / Istanbul not count the code in the onClick()
as a branch covered?
javascript vue.js jestjs istanbul
add a comment |
Please consider the following - somewhat contrived - Vue component:
<!-- FooBar.vue -->
<template>
<div @click="onClick">{{text}}</div>
</template>
<script>
export default {
name: 'foo-bar',
data() {
return {
text: 'foo'
}
},
methods: {
onClick() {
this.text = 'bar';
}
}
}
</script>
I've covered the component with a Jest unit test like so:
// FooBar.spec.js
import FooBar from '~/FooBar.vue';
import { shallowMount } from '@vue/test-utils';
import { expect } from 'chai';
describe('FooBar onClick()', () => {
it('should change the text to "bar"', () => {
// Arrange
const target = shallowMount(FooBar);
// Act
target.trigger('click');
// Assert
const div = target.find('div');
expect(div.text()).to.equal('bar');
});
});
The test passes green.
When I run Jest with --coverage
for this file, I get the following summary report:
=============================== Coverage summary ===============================
Statements : 0.1% ( 2/1868 )
Branches : 0% ( 0/1402 )
Functions : 0% ( 0/505 )
Lines : 0.2% ( 2/982 )
================================================================================
As you can see, the number of branches covered by the unit test is shown to be 0 - Even though Jest (or more accurately Instanbul, which Jest uses behind the scenes for coverage) did detect that the test covered two lines of code.
When I've made a tiny experiment and added an if
statement inside onClick()
like so:
onClick() {
if (this.text != undefined) {
this.text = 'bar';
}
}
then Jest did indeed count 1 branch covered.
My question is - Why does Jest / Istanbul not count the code in the onClick()
as a branch covered?
javascript vue.js jestjs istanbul
Please consider the following - somewhat contrived - Vue component:
<!-- FooBar.vue -->
<template>
<div @click="onClick">{{text}}</div>
</template>
<script>
export default {
name: 'foo-bar',
data() {
return {
text: 'foo'
}
},
methods: {
onClick() {
this.text = 'bar';
}
}
}
</script>
I've covered the component with a Jest unit test like so:
// FooBar.spec.js
import FooBar from '~/FooBar.vue';
import { shallowMount } from '@vue/test-utils';
import { expect } from 'chai';
describe('FooBar onClick()', () => {
it('should change the text to "bar"', () => {
// Arrange
const target = shallowMount(FooBar);
// Act
target.trigger('click');
// Assert
const div = target.find('div');
expect(div.text()).to.equal('bar');
});
});
The test passes green.
When I run Jest with --coverage
for this file, I get the following summary report:
=============================== Coverage summary ===============================
Statements : 0.1% ( 2/1868 )
Branches : 0% ( 0/1402 )
Functions : 0% ( 0/505 )
Lines : 0.2% ( 2/982 )
================================================================================
As you can see, the number of branches covered by the unit test is shown to be 0 - Even though Jest (or more accurately Instanbul, which Jest uses behind the scenes for coverage) did detect that the test covered two lines of code.
When I've made a tiny experiment and added an if
statement inside onClick()
like so:
onClick() {
if (this.text != undefined) {
this.text = 'bar';
}
}
then Jest did indeed count 1 branch covered.
My question is - Why does Jest / Istanbul not count the code in the onClick()
as a branch covered?
javascript vue.js jestjs istanbul
javascript vue.js jestjs istanbul
edited Jan 1 at 19:45
urig
asked Jan 1 at 19:39


urigurig
6,4881464118
6,4881464118
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53998392%2fwhy-does-jest-coverage-istanbul-measure-0-branches-in-this-vue-component-metho%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53998392%2fwhy-does-jest-coverage-istanbul-measure-0-branches-in-this-vue-component-metho%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown