| 1 |
# Writing a test |
| 2 |
--- |
| 3 |
|
| 4 |
All our tests are written using [AVA](http://ava.il). |
| 5 |
|
| 6 |
Because I am a [lazy programmer](http://threevirtues.com/), I wrote some helpers for writing tests (you can see them |
| 7 |
in [`./_utils.js`](_utils.js). I need to document all of them better, but the one you really care about is |
| 8 |
`utils.doTest`. |
| 9 |
It basically abstracts away all of the boilerplate of writing a test, to make tests super easy and expressive. Here's |
| 10 |
an example: |
| 11 |
```javascript |
| 12 |
import test from 'ava'; |
| 13 |
import { doTest } from './_utils'; |
| 14 |
|
| 15 |
test('something', t => { |
| 16 |
doTest( |
| 17 |
t, |
| 18 |
{ |
| 19 |
// These are mappings of filenames to content. The filenames a relative to test/fixture, and will get '.js' added |
| 20 |
// to the end. Since it is only possible to mock files that exist, it is recommended that you only use the |
| 21 |
// names of files in test/fixture, which are currently `foo`, `bar`, `baz`, and `qux`. |
| 22 |
foo: 'module.exports = @@a require('./bar');' |
| 23 |
bar: 'module.exports = 5 @@b', |
| 24 |
}, |
| 25 |
[ |
| 26 |
// This is an array of arrays of arguments. pirates.addHook will be invoked with each array as its argument. |
| 27 |
// For example, for the following: |
| 28 |
code => code.replace('@@a', '\'aa\' + '), |
| 29 |
[code => code.replace('@@b', '* 2'), { matcher: path => path.indexOf('bar') !== -1 }], |
| 30 |
// Will be converted to: |
| 31 |
// pirates.addHook(code => code.replace('@@a', '\'aa\' + ')); |
| 32 |
// pirates.addHook(code => code.replace('@@b', '* 2'), { matcher: path => path.indexOf('bar') !== -1 }); |
| 33 |
], |
| 34 |
{ |
| 35 |
// These are mappings of filenames to their expected *export*. Each file will be required and the export |
| 36 |
// compared with `t.same` (deep equality). The keys should be the same as above. |
| 37 |
foo: '10aa', |
| 38 |
bar: 10, |
| 39 |
} |
| 40 |
); |
| 41 |
}); |
| 42 |
``` |
| 43 |
This will handle clearing the require cache, mocking the file system, registering hooks, requiring the files, and |
| 44 |
restoring the filesystem and hooks. Optionally, any one of the arguments can instead be a function, which accepts no |
| 45 |
arguments and should return an array of `revert` functions. Each function will be called once with no arguments after |
| 46 |
the test is over. |
| 47 |
|
| 48 |
For extreme laziness purposes, you can also do the following (it's a thin wrapper around `doTest`): |
| 49 |
```javascript |
| 50 |
import test from 'ava'; |
| 51 |
import { makeTest } from './_utils'; |
| 52 |
|
| 53 |
test('something', makeTest( // This returns a function, which accepts `t`, then invokes `doTest`. |
| 54 |
{ |
| 55 |
// These are mappings of filenames to content. The filenames a relative to test/fixture, and will get '.js' added |
| 56 |
// to the end. Since it is only possible to mock files that exist, it is recommended that you only use the |
| 57 |
// names of files in test/fixture, which are currently `foo`, `bar`, `baz`, and `qux`. |
| 58 |
foo: 'module.exports = @@a require(\'./bar\');' |
| 59 |
bar: 'module.exports = 5 @@b', |
| 60 |
}, |
| 61 |
[ |
| 62 |
// This is an array of arrays of arguments. pirates.addHook will be invoked with each array as its argument. |
| 63 |
// For example, for the following: |
| 64 |
code => code.replace('@@a', '\'aa\' + '), |
| 65 |
[code => code.replace('@@b', '* 2'), { matcher: path => path.indexOf('bar') !== -1 }], |
| 66 |
// Will be converted to: |
| 67 |
// pirates.addHook(code => code.replace('@@a', '\'aa\' + ')); |
| 68 |
// pirates.addHook(code => code.replace('@@b', '* 2'), { matcher: path => path.indexOf('bar') !== -1 }); |
| 69 |
], |
| 70 |
{ |
| 71 |
// These are mappings of filenames to their expected *export*. Each file will be required and the export |
| 72 |
// compared with `t.same` (deep equality). The keys should be the same as above. |
| 73 |
foo: '10aa', |
| 74 |
bar: 10, |
| 75 |
} |
| 76 |
)); |
| 77 |
``` |
| 78 |
|
| 79 |
Currently, there is no code coverage, as all CC tools use a require hook, which would mess with the tests. However, |
| 80 |
we could certainly use more tests, so please contribute some. |