Skip to content

Commit

Permalink
ES2015ify #1113
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Dec 2, 2016
1 parent ee65b6d commit 262911f
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 91 deletions.
34 changes: 18 additions & 16 deletions lib/assert.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
'use strict';
var util = require('util');
var assert = require('core-assert');
var deepEqual = require('lodash.isequal');
var observableToPromise = require('observable-to-promise');
var indentString = require('indent-string');
var isObservable = require('is-observable');
var isPromise = require('is-promise');
var jestSnapshot = require('jest-snapshot');
var snapshotState = require('./snapshot-state');
const util = require('util');
const assert = require('core-assert');
const deepEqual = require('lodash.isequal');
const observableToPromise = require('observable-to-promise');
const indentString = require('indent-string');
const isObservable = require('is-observable');
const isPromise = require('is-promise');
const jestSnapshot = require('jest-snapshot');
const snapshotState = require('./snapshot-state');

const x = module.exports;
const noop = () => {};
Expand Down Expand Up @@ -153,23 +153,25 @@ x.ifError = (err, msg) => {

x._snapshot = function (tree, optionalMessage, match, snapshotStateGetter) {
// set defaults - this allows tests to mock deps easily
var toMatchSnapshot = match || jestSnapshot.toMatchSnapshot;
var getState = snapshotStateGetter || snapshotState.get;
const toMatchSnapshot = match || jestSnapshot.toMatchSnapshot;
const getState = snapshotStateGetter || snapshotState.get;

var state = getState();
const state = getState();

var context = {
dontThrow: function () {},
const context = {
dontThrow() {},
currentTestName: this.title,
snapshotState: state
};

var result = toMatchSnapshot.call(context, tree);
const result = toMatchSnapshot.call(context, tree);

let message = 'Please check your code or --update-snapshots\n\n';

var message = 'Please check your code or --update-snapshots\n\n';
if (optionalMessage) {
message += indentString(optionalMessage, 2);
}

if (typeof result.message === 'function') {
message += indentString(result.message(), 2);
}
Expand Down
26 changes: 13 additions & 13 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,19 @@ exports.run = function () {
' ava [<file|directory|glob> ...]',
'',
'Options',
' --init Add AVA to your project',
' --fail-fast Stop after first test failure',
' --serial, -s Run tests serially',
' --tap, -t Generate TAP output',
' --verbose, -v Enable verbose output',
' --no-cache Disable the transpiler cache',
' --no-power-assert Disable Power Assert',
' --match, -m Only run tests with matching title (Can be repeated)',
' --watch, -w Re-run tests when tests and source files change',
' --source, -S Pattern to match source files so tests can be re-run (Can be repeated)',
' --timeout, -T Set global timeout',
' --concurrency, -c Maximum number of test files running at the same time (EXPERIMENTAL)',
' --update-snapshots, -u Update snapshots',
' --init Add AVA to your project',
' --fail-fast Stop after first test failure',
' --serial, -s Run tests serially',
' --tap, -t Generate TAP output',
' --verbose, -v Enable verbose output',
' --no-cache Disable the transpiler cache',
' --no-power-assert Disable Power Assert',
' --match, -m Only run tests with matching title (Can be repeated)',
' --watch, -w Re-run tests when tests and source files change',
' --source, -S Pattern to match source files so tests can be re-run (Can be repeated)',
' --timeout, -T Set global timeout',
' --concurrency, -c Maximum number of test files running at the same time (EXPERIMENTAL)',
' --update-snapshots, -u Update snapshots',
'',
'Examples',
' ava',
Expand Down
10 changes: 5 additions & 5 deletions lib/enhance-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ module.exports.NON_ENHANCED_PATTERNS = [
];

function enhanceAssert(opts) {
var empower = require('empower-core');
const empower = require('empower-core');

var enhanced = empower(
const enhanced = empower(
opts.assert,
{
destructive: false,
Expand All @@ -49,9 +49,9 @@ function enhanceAssert(opts) {
}

function formatter() {
var createFormatter = require('power-assert-context-formatter');
var SuccinctRenderer = require('power-assert-renderer-succinct');
var AssertionRenderer = require('power-assert-renderer-assertion');
const createFormatter = require('power-assert-context-formatter');
const SuccinctRenderer = require('power-assert-renderer-succinct');
const AssertionRenderer = require('power-assert-renderer-assertion');

return createFormatter({
renderers: [
Expand Down
22 changes: 11 additions & 11 deletions lib/snapshot-state.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
'use strict';
var path = require('path');
var jestSnapshot = require('jest-snapshot');
var globals = require('./globals');
const path = require('path');
const jestSnapshot = require('jest-snapshot');
const globals = require('./globals');

var x = module.exports;
const x = module.exports;

x.get = function (initializeState, globalsOptions) {
x.get = (initializeState, globalsOptions) => {
if (!x.state) {
// set defaults - this allows tests to mock deps easily
var options = globalsOptions || globals.options;
var initializeSnapshotState = initializeState || jestSnapshot.initializeSnapshotState;
const options = globalsOptions || globals.options;
const initializeSnapshotState = initializeState || jestSnapshot.initializeSnapshotState;

var filename = options.file;
var dirname = path.dirname(filename);
var snapshotFileName = path.basename(filename) + '.snap';
var snapshotsFolder = path.join(dirname, '__snapshots__', snapshotFileName);
const filename = options.file;
const dirname = path.dirname(filename);
const snapshotFileName = path.basename(filename) + '.snap';
const snapshotsFolder = path.join(dirname, '__snapshots__', snapshotFileName);

x.state = initializeSnapshotState(
filename,
Expand Down
26 changes: 13 additions & 13 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,19 +153,19 @@ $ ava --help
ava [<file|directory|glob> ...]

Options
--init Add AVA to your project
--fail-fast Stop after first test failure
--serial, -s Run tests serially
--tap, -- [ ] Generate TAP output
--verbose, -v Enable verbose output
--no-cache Disable the transpiler cache
--no-power-assert Disable Power Assert
--match, -m Only run tests with matching title (Can be repeated)
--watch, -w Re-run tests when tests and source files change
--source, -S Pattern to match source files so tests can be re-run (Can be repeated)
--timeout, -T Set global timeout
--concurrency, -c Maximum number of test files running at the same time (EXPERIMENTAL)
--update-snapshots, -u Update all snapshots
--init Add AVA to your project
--fail-fast Stop after first test failure
--serial, -s Run tests serially
--tap, -- [ ] Generate TAP output
--verbose, -v Enable verbose output
--no-cache Disable the transpiler cache
--no-power-assert Disable Power Assert
--match, -m Only run tests with matching title (Can be repeated)
--watch, -w Re-run tests when tests and source files change
--source, -S Pattern to match source files so tests can be re-run (Can be repeated)
--timeout, -T Set global timeout
--concurrency, -c Maximum number of test files running at the same time (EXPERIMENTAL)
--update-snapshots, -u Update all snapshots

Examples
ava
Expand Down
35 changes: 16 additions & 19 deletions test/assert.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
'use strict';
var test = require('tap').test;
var Promise = require('bluebird');
var sinon = require('sinon');
var assert = require('../lib/assert');
const test = require('tap').test;
const sinon = require('sinon');
const assert = require('../lib/assert');

test('.pass()', t => {
t.doesNotThrow(() => {
Expand Down Expand Up @@ -492,19 +491,17 @@ test('.deepEqual() should not mask RangeError from underlying assert', t => {
t.end();
});

test('snapshot makes a snapshot using a library and global options', function (t) {
var saveSpy = sinon.spy();
var state = {save: saveSpy};
var stateGetter = sinon.stub().returns(state);
var matchStub = sinon.stub().returns({
pass: true
});
test('snapshot makes a snapshot using a library and global options', t => {
const saveSpy = sinon.spy();
const state = {save: saveSpy};
const stateGetter = sinon.stub().returns(state);
const matchStub = sinon.stub().returns({pass: true});

assert.title = 'Test name';

t.plan(4);

t.doesNotThrow(function () {
t.doesNotThrow(() => {
assert._snapshot('tree', undefined, matchStub, stateGetter);
});

Expand All @@ -522,19 +519,19 @@ test('snapshot makes a snapshot using a library and global options', function (t
t.end();
});

test('if snapshot fails, prints a message', function (t) {
var saveSpy = sinon.spy();
var state = {save: saveSpy};
var stateGetter = sinon.stub().returns(state);
var messageStub = sinon.stub().returns('message');
var matchStub = sinon.stub().returns({
test('if snapshot fails, prints a message', t => {
const saveSpy = sinon.spy();
const state = {save: saveSpy};
const stateGetter = sinon.stub().returns(state);
const messageStub = sinon.stub().returns('message');
const matchStub = sinon.stub().returns({
pass: false,
message: messageStub
});

t.plan(2);

t.throws(function () {
t.throws(() => {
assert._snapshot('tree', undefined, matchStub, stateGetter);
});

Expand Down
26 changes: 12 additions & 14 deletions test/snapshot-state.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
'use strict';
const path = require('path');
const test = require('tap').test;
const sinon = require('sinon');
const snapshotState = require('../lib/snapshot-state');

var path = require('path');
var test = require('tap').test;
var sinon = require('sinon');
var snapshotState = require('../lib/snapshot-state');

test('snapshot state gets created and returned', function (t) {
var stateStub = sinon.stub().returns('state');
test('snapshot state gets created and returned', t => {
const stateStub = sinon.stub().returns('state');

t.plan(3);

t.doesNotThrow(function () {
var result = snapshotState.get(stateStub, {
t.doesNotThrow(() => {
const result = snapshotState.get(stateStub, {
file: path.join('hello', 'world.test.js'),
updateSnapshots: false
});
Expand All @@ -29,16 +28,15 @@ test('snapshot state gets created and returned', function (t) {
t.end();
});

test('snapshot state is returned immediately if it already exists', function (t) {
var stateSpy = sinon.spy();
test('snapshot state is returned immediately if it already exists', t => {
const stateSpy = sinon.spy();

t.plan(3);

snapshotState.state = 'already made state';

t.doesNotThrow(function () {
var result = snapshotState.get(stateSpy);

t.doesNotThrow(() => {
const result = snapshotState.get(stateSpy);
t.is(result, 'already made state');
});

Expand Down

0 comments on commit 262911f

Please sign in to comment.