Let’s say you have a function
isPrime(). When given a number, it returns
false depending on whether the number is a prime number.
If you’re using a framework like jasmine, your tests might look something like this:
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:
Sazerac runs the
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:
Install Sazerac as an npm module and save it to your
package.json file as a development dependency:
given helper functions into your project:
given() functions are the basic building blocks for creating data-driven test cases.
test() sets the function you want to test.
given() creates a test case by defining the arguments that will be passed to your function.
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.
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.
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
Each assertion will be executed as a separate test.
Sazerac generates messages that are passed to
it() functions, and ultimately end up in your test report. You can customize these, or stick with the default format.
Messages for each test case are defined based on the given parameters and the expected return value.
describe message for a group of test cases is defined based on the name of the function being tested.
Here’s an example:
You can set a message describing the test case using the
You can set a message describing the test assertion using the
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:
If you have multiple test case inputs with the same expected return value, they can be grouped with the
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.
All test case functions can be chained, in any order.