Data-driven unit testing for JavaScript.
Sazerac is a library for data-driven testing in JavaScript. It works with mocha, jasmine, and jest. Using Sazerac, and the data-driven testing pattern in general, will reduce the complexity and increase readability of your test code.
Check out sazeracjs.com for docs and sazerac-example for examples.
Let's say you have a function isPrime()
. When given a number, it returns true
or false
depending on whether the number is a prime number.
function isPrime(num) {
for(var i = 2; i < num; i++) {
if(num % i === 0) return false;
}
return num > 1;
}
If you're using a framework like jasmine, your tests might look something like this:
describe('isPrime()', () => {
describe('when given 2', () => {
it('should return true', () => {
assert.isTrue(isPrime(2))
})
})
describe('when given 3', () => {
it('should return true', () => {
assert.isTrue(isPrime(3))
})
})
describe('when given 4', () => {
it('should return false', () => {
assert.isFalse(isPrime(4))
})
})
// and more ...
})
It's a lot of code to write for only 3 test cases and such a basic function!
The same tests can be defined with Sazerac as follows:
test(isPrime, () => {
given(2).expect(true)
given(3).expect(true)
given(4).expect(false)
})
Sazerac runs the describe
and it
functions needed for these test cases. It adds reporting messages in a consistent format based on the input and output parameters. For this example, the test report ends up looking like this:
isPrime()
when given 2
✓ should return true
when given 3
✓ should return true
when given 4
✓ should return false
Install Sazerac as an npm module and save it to your package.json
file as a development dependency:
npm install sazerac --save-dev
Import the test
and given
helper functions into your project:
import { test, given } from 'sazerac'
Visit sazeracjs.com
Yes, please do :)