Adding tests should be extremely easy. Otherwise you’ll just skip it.
x9 provides three ways of creating tests:
- x9.test: testing with Dragon DNA via
x9.test
- this is the recommended way of writing tests. It provides a few new features such as
- block tests: block form syntax for inline testing
- similar to playgrounds, they are extremely convenient and concise
- jest/mocha: (soon)
- use
test
, describe
, it
, expect
, etc
- useful if you’re migrating an existing codebase and you don’t want to rewrite your tests
Mini Tests (or Test Blocks)
If you use expect
within a playground, it becomes a test. You’ll see its color change in the IDE.
function sum(a, b) {
return a + b;
}
{
expect(sum(1, 2)).toBe(3);
}
- If automatic detection fails, you can explicitly transform a playground into a test by adding a
// x9.test
comment (and // x9.test=false
if it fails the other way)
Async/Await
You can use await
at the top level (within the block). The containing block will be automatically treated as an async function
{ expect(await sum(1, 2)).toBe(3);}
Proposal: Terse Assertion Syntax
Experimental: When using the Block form
you also get some syntactic sugar.
{ // x9.test sum(1, 2) === 3; // expect(1,2).toBe(3) sum(1, 2) < 10; // expect(1,2).toBeLessThan(10)}
The goal of this syntax is twofold:
- It makes writing basic tests even easier (the ultimate goal is to remove all boundaries so writing basic tests becomes part of your normal workflow, with zero overhad)
- This terse syntax results in highly readable tests that can serve as documentation. For a related idea, see Python doctests
Notes:
- Only top-level (direct children of the block) binary expressions statements with a boolean operator (==, ===, !=, !==, >=, etc) are transformed.