Code Monkey home page Code Monkey logo

incorrect-coverage's Introduction

incorrect-coverage

Demo repo to demonstrate problems in code coverage reports with switch cases.

In the test file, we run tests that test the switch case for case 1, case 4 and the default case. case 2 and case 3 are not tested. However, the code coverage, as shown above, shows that we've handled all branches except case 3, thus including case 2 which is not actually covered.

The reason, I suspect, is because the program is transformed to be something like:

module.exports = function (x) {
	switch (x) {
		case 1:
		  incrementBranch_1();
		case 2:
			incrementBranch_2();
			incrementStatement_1();
			return 'ok';
		case 3:
			incrementBranch_3();
		case 4:
			incrementBranch_4();
			incrementStatement_2();
			return 'also ok';
		default:
			incrementBranch_default();
			incrementStatement_3();
			return 'why not';
	}
};

but because of the fallthrough behaviour, incrementBranch_2() also gets called when case 1 is entered.

This falsifies the code coverage, and makes you think you have 100% code coverage when you actually don't. In one of my packages, I duplicated some switch-case heavy code in order to have a reliable code coverage.

incorrect-coverage's People

Contributors

jfmengels avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.