Skip to content

Commit

Permalink
Rewrite tests to use AVA
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Apr 27, 2017
1 parent 4ac3c98 commit 315275f
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 80 deletions.
10 changes: 3 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"node": ">=4"
},
"scripts": {
"test": "xo && mocha"
"test": "xo && ava"
},
"files": [
"index.js",
Expand Down Expand Up @@ -42,13 +42,9 @@
"through2": "^2.0.3"
},
"devDependencies": {
"ava": "*",
"gulp": "^3.9.1",
"p-event": "^1.0.0",
"xo": "*"
},
"xo": {
"envs": [
"node",
"mocha"
]
}
}
2 changes: 1 addition & 1 deletion test/fixtures/fixture-async.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';
const assert = require('assert');

it('should fail after timeout', (done) => {
it('should fail after timeout', done => {
setTimeout(() => {
assert(false);
}, 10);
Expand Down
96 changes: 35 additions & 61 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict';
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const gutil = require('gulp-util');
const mocha = require('..');
import fs from 'fs';
import path from 'path';
import test from 'ava';
import gutil from 'gulp-util';
import pEvent from 'p-event';
import mocha from '..';

function fixture(name) {
const fileName = path.join(__dirname, 'fixtures', name);
Expand All @@ -14,65 +14,39 @@ function fixture(name) {
});
}

describe('mocha()', () => {
it('should run unit test and pass', done => {
const stream = mocha({suppress: true});

stream.once('_result', result => {
assert(/1 passing/.test(result.stdout));
done();
});
stream.write(fixture('fixture-pass.js'));
stream.end();
});

it('should run unit test and fail', done => {
const stream = mocha({suppress: true});

stream.once('error', err => {
assert(/1 failing/.test(err.stdout));
done();
});
stream.write(fixture('fixture-fail.js'));
stream.end();
});

it('should pass async AssertionError to mocha', done => {
const stream = mocha({suppress: true});

stream.once('error', err => {
const throws = /throws after timeout/.test(err.stdout);
const uncaught = /Uncaught AssertionError: false == true/.test(err.stdout);

assert(throws || uncaught);
done();
});
stream.write(fixture('fixture-async.js'));
stream.end();
});
test('run unit test and pass', async t => {
const stream = mocha({suppress: true});
const result = pEvent(stream, '_result');
stream.end(fixture('fixture-pass.js'));
t.regex((await result).stdout, /1 passing/);
});

it('should not suppress output', done => {
const stream = mocha();
test('run unit test and fail', async t => {
const stream = mocha({suppress: true});
const error = pEvent(stream, 'error');
stream.end(fixture('fixture-fail.js'));
t.regex((await error).stdout, /1 failing/);
});

stream.once('_result', result => {
assert(/should pass/.test(result.stdout));
done();
});
stream.write(fixture('fixture-pass.js'));
stream.end();
});
test('pass async AssertionError to mocha', async t => {
const stream = mocha({suppress: true});
const event = pEvent(stream, 'error');
stream.end(fixture('fixture-async.js'));
const error = await event;
const throws = /throws after timeout/.test(error.stdout);
const uncaught = /Uncaught AssertionError: false == true/.test(error.stdout);
t.true(throws || uncaught);
});

it('should require two files', done => {
const stream = mocha({require: [
test('require two files', async t => {
const stream = mocha({
suppress: true,
require: [
'test/fixtures/fixture-require1.js',
'test/fixtures/fixture-require2.js'
]});

stream.once('_result', result => {
assert(/1 passing/.test(result.stdout));
done();
});
stream.write(fixture('fixture-pass.js'));
stream.end();
]
});
const result = pEvent(stream, '_result');
stream.end(fixture('fixture-pass.js'));
t.regex((await result).stdout, /1 passing/);
});
18 changes: 7 additions & 11 deletions test/utils.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
'use strict';
const assert = require('assert');
const utils = require('../utils');
import test from 'ava';
import m from '../utils';

describe('Utils', () => {
describe('convertObjectToList', () => {
it('produces a comma separated string of k=v', () => {
const actual = utils.convertObjectToList({key1: 'value1', key2: 'value2', key99: 'value99'});
const expected = 'key1=value1,key2=value2,key99=value99';
assert.strictEqual(actual, expected);
});
});
test('convertObjectToList produces a comma separated string of k=v', t => {
t.is(
m.convertObjectToList({key1: 'value1', key2: 'value2', key99: 'value99'}),
'key1=value1,key2=value2,key99=value99'
);
});

0 comments on commit 315275f

Please sign in to comment.