./tool is a script in the root of this project. ./tool test runs the tests in this directory.
- Tests should pair small examples with the expectations for each example.
- We should dogfood Flow
Check out tool_test_example, which is an example test.
- A test is a file named
./**/test.js. - Each
test.jsfile exports aSuiteby default - Each
Suitecontains a list ofTests. - Each
Testcontains a list ofTestSteps
The only way to create a Suite is to call the suite() function. The suite() function takes a callback, like so
import {suite} from '../../tsrc/test/Tester';
import type TestStep from '../../tsrc/test/TestStep';
export default suite((emptyTestStep: TestStep) => [ < List of Tests >]);(Why the suite() function? Why not just export the callback directly? Well, it removes the need for type annotations!)
The only way to create a Test is to call the test() function. The test() function takes a test name and a list of TestSteps, like so
import {suite, test} from '../../tsrc/test/Tester';
import type TestStep from '../../tsrc/test/TestStep';
export default suite((emptyTestStep: TestStep) => [
test('My first test, [ < List of TestSteps > ]'),
]);A TestStep is made up of 0 or more actions and 0 or more assertions. The emptyTestStep passed to suite()'s callback is a TestStep with 0 actions and 0 assertions. TestSteps are immutable, so when you call emptyTestStep.addFile('foo.js') you get back a new TestStep with 1 action and 0 assertions. So a test looks like
import {suite, test} from '../../tsrc/test/Tester';
import type TestStep from '../../tsrc/test/TestStep';
export default suite((emptyTestStep: TestStep) => [
test('My first test', [
emptyTestStep
.addCode('var x = 123')
.noNewErrors(),
emptyTestStep
.addCode('var y = "hello"')
.noNewErrors(),
]),
]);More concisely, this can be written
import {suite, test} from '../../tsrc/test/Tester';
export default suite(({addCode}) => [
test('My first test', [
addCode('var x = 123')
.noNewErrors(),
addCode('var y = "hello"')
.noNewErrors(),
]),
]);Note: You cannot add actions to a TestStep after an assertion because @gabelevi felt like messing around with the type system to prevent it :)