Browse Source

Docs: Jest

docs
Eli Perelman 8 years ago
parent
commit
cae69d8770
  1. 88
      docs/presets/neutrino-preset-jest/README.md
  2. 2
      docs/presets/neutrino-preset-mocha/README.md

88
docs/presets/neutrino-preset-jest/README.md

@ -6,6 +6,7 @@
- Zero upfront configuration necessary to start testing
- Babel compilation that compiles your tests using the same Babel options used by your source code
- Source watching for re-running of tests on change
- Easily extensible to customize your testing as needed
## Requirements
@ -36,7 +37,7 @@ another Neutrino preset for building your application source code.
`neutrino-preset-jest` follows the standard [project layout](/project-layout.md) specified by Neutrino. This
means that by default all project test code should live in a directory named `test` in the root of the
project. Test files end in either `_test.js` or `.test.js`.
project. Test files end in either `_test.js`, `.test.js`, `_test.jsx`, or `.test.jsx`.
## Quickstart
@ -74,14 +75,16 @@ Run the tests, and view the results in your console:
```bash
❯ yarn test
PASS test/simple_test.js
simple
✓ should be sane
1 passing (426ms)
✨ Done in 4.17s.
✓ should be sane (2ms)
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 1.973s
Ran all test suites.
✨ Done in 4.43s.
```
#### npm
@ -89,11 +92,15 @@ Run the tests, and view the results in your console:
```bash
❯ npm test
PASS test/simple_test.js
simple
✓ should be sane
✓ should be sane (2ms)
1 passing (409ms)
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 1.075s
Ran all test suites.
```
To run tests against files from your source code, simply import them:
@ -102,58 +109,71 @@ To run tests against files from your source code, simply import them:
import thingToTest from '../src/thing';
```
For more details on specific Jest usage, please refer to their [documentation](https://facebook.github.io/jest/).
## Executing single tests
By default this preset will execute every test file located in your test directory ending in `_test.js`.
By default this preset will execute every test file located in your test directory ending in the appropriate file
extension.
Use the command line [`files` parameters](/cli/README.md#neutrino-test) to execute individual tests.
## Watching for changes
`neutrino-preset-jest` can watch for changes on your source directory and subsequently re-run tests. Simply use the
`--watch` flag with your `neutrino test` command.
## Customizing
To override the test configuration, start with the documentation on [customization](/customization/README.md).
`neutrino-preset-mocha` creates some conventions to make overriding the configuration easier once you are ready to make
`neutrino-preset-jest` creates some conventions to make overriding the configuration easier once you are ready to make
changes.
### Rules
The following is a list of rules and their identifiers which can be overridden:
- `compile`: Compiles JS files from the `test` directory using Babel. Contains a single loader named `babel`.
- `compile`: Compiles JS files from the `test` directory using adopted Babel settings from other build presets.
Contains a single loader named `babel`.
### Simple customization
By following the [customization guide](/customization/simple.md) and knowing the rule, loader, and plugin IDs above,
By following the [customization guide](/customization/simple.md) and knowing the rule, and loader IDs above,
you can override and augment the build directly from package.json.
Jest configuration settings can also be modified directly from package.json, but it is not required.
`neutrino-preset-jest` will import Jest configuration from your package.json's `jest` object; the format is
defined on the [Jest documentation site](https://facebook.github.io/jest/docs/configuration.html).
_Example: Turn off bailing on test failures._
```json
{
"jest": {
"bail": false
}
}
```
### Advanced configuration
By following the [customization guide](/customization/advanced.md) and knowing the rule, and loader IDs above,
you can override and augment testing by creating a JS module which overrides the config.
you can override and augment testing by creating a JS module which overrides the config.
You can modify Mocha settings by overriding the preset with any options Mocha accepts. This is stored in the
`neutrino.custom.mocha` object.
You can also modify Jest settings by overriding with any options Jest accepts. In a standalone Jest project this is
typically done in the package.json file, but `neutrino-preset-jest` allows advanced configuration through this
mechanism as well. This is stored in the `neutrino.custom.jest` object, and takes the same configuration options as
outlined in the [Jest documentation](https://facebook.github.io/jest/docs/configuration.html).
_Example: Switch the test reporter from the default `spec` to `nyan`:_
_Example: Create a global `__DEV__` variable set to `true` in all test environments._
```js
module.exports = neutrino => {
neutrino.custom.mocha.reporter = 'nyan';
neutrino.custom.jest.globals = {
__DEV__: true
};
};
```
```bash
❯ yarn test
yarn test v0.19.1
$ node_modules/neutrino/bin/neutrino test
1 -__,------,
0 -__| /\_/\
0 -_~|_( ^ .^)
-_ "" ""
1 passing (362ms)
✨ Done in 3.28s.
```
## Contributing
This preset is part of the [neutrino-dev](https://github.com/mozilla-neutrino/neutrino-dev) repository, a monorepo

2
docs/presets/neutrino-preset-mocha/README.md

@ -104,6 +104,8 @@ To run tests against files from your source code, simply import them:
import thingToTest from '../src/thing';
```
For more details on specific Mocha usage, please refer to their [documentation](http://mochajs.org/).
## Executing single tests
By default this preset will execute every test file located in your test directory ending in `_test.js`.

Loading…
Cancel
Save