Skip to content

Add serial mode #101

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 25, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var cli = meow({
'Options',
' --init Add AVA to your project',
' --fail-fast Stop after first test failure',
' --serial Run tests serially',
'',
'Examples',
' ava',
Expand All @@ -36,7 +37,7 @@ var cli = meow({
]
}, {
string: ['_'],
boolean: ['fail-fast']
boolean: ['fail-fast', 'serial']
});

var fileCount = 0;
Expand Down Expand Up @@ -96,6 +97,10 @@ function run(file) {
args.push('--fail-fast');
}

if (cli.flags.serial) {
args.push('--serial');
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of doing this for every option I think we should just JSON.stringify the options and pass them as the first argument.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooh, this will not look good... I propose to postpone this, until we have more options, what do you say?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

k


return fork(args)
.on('test', test)
.on('data', function (data) {
Expand Down
6 changes: 6 additions & 0 deletions lib/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ var EventEmitter = require('events').EventEmitter;
var Promise = require('bluebird');
var Test = require('./test');

var isSerial = process.argv.indexOf('--serial') !== -1;

function noop() {}

function each(items, fn) {
Expand Down Expand Up @@ -105,6 +107,10 @@ Runner.prototype._runTest = function (test) {
};

Runner.prototype.concurrent = function (tests) {
if (isSerial) {
return this.serial(tests);
}

return each(tests, this._runTestWithHooks.bind(this));
};

Expand Down
9 changes: 9 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ $ ava --help
Options
--init Add AVA to your project
--fail-fast Stop after first test failure
--serial Run tests serially

Examples
ava
Expand Down Expand Up @@ -468,6 +469,14 @@ Have fun!

Running tests concurrently comes with some challenges, doing IO is one. Usually, serial tests just create temp directories in the current test directory and cleans it up at the end. This won't work when you run tests concurrently as tests will conflict with each other. The correct way to do it is to use a new temp directory for each test. The [`tempfile`](https://github.com/sindresorhus/tempfile) and [`temp-write`](https://github.com/sindresorhus/temp-write) modules can be helpful.

### Debugging

AVA runs tests concurrently by default, which is suboptimal when you need to debug something. Instead, run tests serially with the `--serial` option:

```
$ ava --serial
```


## FAQ

Expand Down
26 changes: 26 additions & 0 deletions test/fixture/serial.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import test from '../../';

function randomDelay() {
return parseInt(Math.random() * 1000, 10);
}

const tests = [];

test('first', t => {
setTimeout(() => {
tests.push('first');
t.end();
}, randomDelay());
});

test('second', t => {
setTimeout(() => {
tests.push('second');
t.end();
}, randomDelay());
});

test(t => {
t.same(tests, ['first', 'second']);
t.end();
});
11 changes: 11 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,15 @@ test('fail-fast mode', function (t) {
});
});

test('serial execution mode', function (t) {
t.plan(1);

execCli(['fixture/serial.js', '--serial'], function (err) {
t.ifError(err);
t.end();
});
});

test('power-assert support', function (t) {
t.plan(2);

Expand All @@ -853,6 +862,8 @@ test('power-assert support', function (t) {
});

test('change process.cwd() to a test\'s directory', function (t) {
t.plan(2);

execCli('fixture/process-cwd.js', function (err, stdout) {
t.ifError(err);
t.is(stdout.trim(), join(__dirname, 'fixture'));
Expand Down