Our company’s open-source team is delighted to launch AssertiveTS, a new library built by the Stack Builders team to help you test your TypeScript applications. This library allows you and your team to create fluent tests through a set of predefined matchers.
And you might ask: How is this library different from other available options? This one adds type safety to your tests by only allowing you to use matchers that correspond to a particular data type. For example: Only allowing a length matcher on a string, and disallowing it for boolean values. Let’s look at the following:
// Boolean assertion
expect(isEven(2)).toBeTrue();
// String assertion
expect("foobar").toStartWith("foo");
// Number assertion
expect(sum(1, 2)).toBePositive();
// An incorrect way to assert on a numeric value
expect(14).toEndWith("4");
^ ? type error: `toEndWith` does not exist in `NumberAssertion`
As you can see, through the power of TypeScript you cannot create test cases that are inconsistent at the type level, which results in more expressive, reasonable, and readable tests that you can execute with your favorite test runner. Our initial release of AssertiveTS has integrations with Jest and Mocha, but we will be adding more of them over time.
Another great feature of this library is that you can write fluid tests, which means you can chain them one after another. With this, you will be able to write concise tests and avoid doing one assertion per line, which results in several unnecessary function calls and repeated code in your suite. Here’s a little example of this:
expect("assertive-ts is awesome!")
.toStartWith("assertive-ts")
.not.toContain("unsafe")
.toEndWith("awesome!");
So give it a go! As a company, we value high-quality software and good software practices. This library will definitely make your application shine in these two areas!
Getting started with it
Please refer to the usage section in the library’s readme for a quick start. You can also go to the documentation to see the complete list of matchers available for each data type.
Our contributors
We’d like to thank José Luis León for putting this idea together, and his amazing team: Christian Samaniego, Byron Motoche, David Villamarín, Alejandro Vivanco, and Alexander Mejía.
Contributing to this library
We look forward to seeing your suggestions to improve this library! Hop on to the project’s repository and add an issue or a pull request if you find any improvement opportunities. We’d love to hear from the community so this library grows and improves over time!