Skip to content

alexandlazaris/jest-test-mutation

Repository files navigation

jest-test-mutation

zornstriker

jest

https://jestjs.io

stryker

https://stryker-mutator.io

Summary: Use Stryker to identify tests that do not cover various logic or conditional mutations.

Mutations

Original logic

function sum(a, b) { return a + b)}
// sum(2, 2)
// test is expecting 4
// result is 4 
// PASS

Mutation

Below, Stryker has modified the + operator to be - instead, and will check if the existing test case caters to this new scenario aka mutation.

function sum(a, b) { return a - b)}
// sum(2, 2)
// test is expecting 4
// result is 0
// FAIL

Stryker takes your code, mutates and runs your tests against the mutated logic, returning the results of those additional scenarios against your existing unit tests.

mutants

  • are generated based off operators or logic provided in functions. e.g
#3. [NoCoverage] ArithmeticOperator
src/multiply.js:6:12
-       return (a * b) * -1 > the original logic
+       return a * b / -1 > the mutated logic aka the mutant

#4. [NoCoverage] ArithmeticOperator
src/multiply.js:6:13
-       return (a * b) * -1 > the original logic
+       return (a / b) * -1 > the mutated logic aka the mutant

Mutation variations & support can be found here https://stryker-mutator.io/docs/mutation-testing-elements/supported-mutators/

killed

  • if a mutant is covered a test, the mutant is killed

surivor mutant

  • test passed even when changes were made
  • if you have these, it means your test is not handling the mutated (different) logic

Reporting

Stryker also provides in-built reporting, highlight a stack of info. A html sample is below, showing the mutation of a function:

mutation-survived-report

TODO

  • import mutation logic for tests
  • convert JS code to TS
  • explore jest mock + spy functionality

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published