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 26, 2017
1 parent b8649cf commit 4db5871
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 56 deletions.
10 changes: 7 additions & 3 deletions package.json
Expand Up @@ -13,7 +13,7 @@
"node": ">=4"
},
"scripts": {
"test": "xo && mocha"
"test": "xo && ava"
},
"files": [
"index.js"
Expand All @@ -39,11 +39,15 @@
"tildify": "^1.1.2"
},
"devDependencies": {
"ava": "*",
"gulp": "^3.8.10",
"mocha": "*",
"p-event": "^1.0.0",
"proxyquire": "^1.0.1",
"sinon": "^1.9.1",
"sinon": "^2.1.0",
"strip-ansi": "^3.0.0",
"xo": "*"
},
"ava": {
"serial": true
}
}
100 changes: 47 additions & 53 deletions test.js
@@ -1,101 +1,95 @@
/* eslint-env mocha */
'use strict';
const fs = require('fs');
const path = require('path');
const assert = require('assert');
const sinon = require('sinon');
const gutil = require('gulp-util');
const proxyquire = require('proxyquire');
const stripAnsi = require('strip-ansi');
import fs from 'fs';
import path from 'path';
import test from 'ava';
import sinon from 'sinon';
import gutil from 'gulp-util';
import proxyquire from 'proxyquire';
import stripAnsi from 'strip-ansi';
import pEvent from 'p-event';

const gutilStub = {
log() {
// uncomment the next line to see the log messages written by gulp-debug
// during test (by default they are swallowed by the sinon stub replacing the log method)
// Uncomment the next line to see the log messages written by `gulp-debug` during test
// (by default they are swallowed by the sinon stub replacing the log method)
// gutil.log.apply(gutil.log, arguments);
}
};

const debug = proxyquire('./', {
const debug = proxyquire('.', {
'gulp-util': gutilStub
});

let file;

beforeEach(() => {
test.beforeEach(() => {
sinon.spy(gutilStub, 'log');

file = new gutil.File({
cwd: __dirname,
base: __dirname,
path: path.join(__dirname, 'foo.js'),
stat: fs.statSync('test.js'),
contents: new Buffer('Lorem ipsum dolor sit amet, consectetuer adipiscing elit.')
contents: Buffer.from('Lorem ipsum dolor sit amet, consectetuer adipiscing elit.')
});
});

afterEach(() => {
test.afterEach(() => {
gutilStub.log.restore();
});

it('should output debug info', cb => {
test('output debug info', async t => {
const stream = debug({title: 'unicorn:'});
const finish = pEvent(stream, 'finish');
stream.end(file);
await finish;

stream.write(file, 'utf8', () => {
assert(gutilStub.log.calledOnce);
assert.strictEqual(stripAnsi(gutilStub.log.firstCall.args[0]).split('\n')[0], 'unicorn: foo.js');
cb();
});
t.is(stripAnsi(gutilStub.log.firstCall.args[0]).split('\n')[0], 'unicorn: foo.js');
});

it('should output singular item count', cb => {
test('output singular item count', async t => {
const stream = debug({title: 'unicorn:'});
const finish = pEvent(stream, 'finish');
stream.end(file);
await finish;

stream.on('finish', () => {
assert.strictEqual(stripAnsi(gutilStub.log.lastCall.args[0]).split('\n')[0], 'unicorn: 1 item');
cb();
});

stream.write(file, () => {
stream.end();
});
t.is(stripAnsi(gutilStub.log.lastCall.args[0]).split('\n')[0], 'unicorn: 1 item');
});

it('should output zero item count', cb => {
test('output zero item count', async t => {
const stream = debug({title: 'unicorn:'});

stream.on('finish', () => {
assert.strictEqual(stripAnsi(gutilStub.log.lastCall.args[0]).split('\n')[0], 'unicorn: 0 items');
cb();
});

const finish = pEvent(stream, 'finish');
stream.end();
await finish;

t.is(stripAnsi(gutilStub.log.lastCall.args[0]).split('\n')[0], 'unicorn: 0 items');
});

it('should output plural item count', cb => {
test('output plural item count', async t => {
const stream = debug({title: 'unicorn:'});

stream.on('finish', () => {
assert.strictEqual(stripAnsi(gutilStub.log.lastCall.args[0]).split('\n')[0], 'unicorn: 2 items');
cb();
});
const finish = pEvent(stream, 'finish');

stream.write(file, () => {
stream.write(file, () => {
stream.end();
});
stream.end(file);
});
});

it('should not output file names when `showFiles` is false.', cb => {
const stream = debug({title: 'unicorn:', showFiles: false});
await finish;

stream.on('finish', () => {
assert.strictEqual(stripAnsi(gutilStub.log.lastCall.args[0]).split('\n')[0], 'unicorn: 1 item');
cb();
t.is(stripAnsi(gutilStub.log.lastCall.args[0]).split('\n')[0], 'unicorn: 2 items');
});

test('not output file names when `showFiles` is false.', async t => {
const stream = debug({
title: 'unicorn:',
showFiles: false
});
const finish = pEvent(stream, 'finish');

stream.write(file, () => {
assert(gutilStub.log.notCalled);
t.true(gutilStub.log.notCalled);
stream.end();
});

await finish;

t.is(stripAnsi(gutilStub.log.lastCall.args[0]).split('\n')[0], 'unicorn: 1 item');
});

0 comments on commit 4db5871

Please sign in to comment.