Guide
What is Sazerac?
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.
Why Use It?
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) { |
If you’re using a framework like jasmine, your tests might look something like this:
describe('isPrime()', () => { |
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, () => { |
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() |
Installation
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' |
Basic Use
Sazerac’s test()
and given()
functions are the basic building blocks for creating data-driven test cases.
test()
sets the function you want to test.
test(mySumFunction, () => { |
given()
creates a test case by defining the arguments that will be passed to your function.
test(mySumFunction, () => { |
The object returned by given()
provides functions for defining your test case. expect()
is the most basic of these. It defines the expected value for you test case.
test(mySumFunction, () => { |
Custom Assertions
Sometimes the expected return value for your test case is too complex to define with expect()
. To define a custom assertion, assert()
can be used instead. The assert()
function expects a message describing the assertion, and a function that defines the assertion.
t.assert( |
Since you have the actual return value of your test case, any assertion can be performed on the return value. For example, you could add assertions with the library of your choice, such as chai.js or should.js
You can also add multiple assertions for a single test case
t.assert( |
Each assertion will be executed as a separate test.
Expect Error Thrown
The expectError()
function can be used to assert that a test case threw a specified error message. The test case will pass if an error is thrown, and the error message matches the string passed to expectError()
exactly.
function throwAnError(msg) { |
Reporting Messages
Sazerac generates messages that are passed to describe()
and it()
functions, and ultimately end up in your test report. You can customize these, or stick with the default format.
Default Format
Messages for each test case are defined based on the given parameters and the expected return value.
The describe
message for a group of test cases is defined based on the name of the function being tested.
{name_of_tested_function} |
Here’s an example:
test(mySumFunction, () => { |
will output:
mySumFunction() |
Setting Describe Messages
You can set a message describing the test case using the describe()
function
test(mySumFunction, () => { |
will output:
mySumFunction() |
Setting Should Messages
You can set a message describing the test assertion using the should()
function
test(mySumFunction, () => { |
will output:
mySumFunction() |
Formatting With sprintf.js
Sazerac uses sprintf.js to provide given and expected values for custom string formatting.
You can use any sprintf.js functionality to format your message strings. For example:
test(addTwoNumbers, () => { |
will output:
addTwoNumbers() |
Setup and Teardown
The before
and after
functions can be used to run setup and teardown code for a test case, or a collection of test cases. This is equivalent to running beforeEach
and afterEach
in Jasmine, Mocha, or Jest.
Mocking function behaviors is a common use case for these:
import sinon from 'sinon' |
Grouping Multiple Cases
If you have multiple test case inputs with the same expected return value, they can be grouped with the forCases(cases)
function:
import { test, given, forCases } from 'sazerac' |
forCases()
will execute a separate test for each case that you pass it. It accepts either an array or a series of arguments, depending on which style you prefer.
|
Chaining
All test case functions can be chained, in any order.
test(mySumFunction, () => { |