Skip to content

Commit

Permalink
Document t.title accessor
Browse files Browse the repository at this point in the history
Clean up tests which unnecessarily use the accessor, and add a single dedicated
test instead.
  • Loading branch information
novemberborn committed Jun 25, 2017
1 parent ec94f94 commit 549e99b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 66 deletions.
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -867,7 +867,7 @@ Should contain the actual test.

Type: `object`

The execution object of a particular test. Each test implementation receives a different object. Contains the [assertions](#assertions) as well as `.plan(count)` and `.end()` methods. `t.context` can contain shared state from `beforeEach` hooks.
The execution object of a particular test. Each test implementation receives a different object. Contains the [assertions](#assertions) as well as `.plan(count)` and `.end()` methods. `t.context` can contain shared state from `beforeEach` hooks. `t.title` returns the test's title.

###### `t.plan(count)`

Expand Down
15 changes: 12 additions & 3 deletions test/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,6 @@ test('macros: Customize test names attaching a `title` function', t => {
];

function macroFn(avaT) {
t.is(avaT.title, expectedTitles.shift());
t.deepEqual(slice.call(arguments, 1), expectedArgs.shift());
avaT.pass();
}
Expand All @@ -754,6 +753,10 @@ test('macros: Customize test names attaching a `title` function', t => {

const runner = new Runner();

runner.on('test', props => {
t.is(props.title, expectedTitles.shift());
});

runner.chain.test(macroFn, 'A');
runner.chain.test('supplied', macroFn, 'B');
runner.chain.test(macroFn, 'C');
Expand All @@ -770,7 +773,6 @@ test('match applies to macros', t => {
t.plan(3);

function macroFn(avaT) {
t.is(avaT.title, 'foobar');
avaT.pass();
}

Expand All @@ -780,6 +782,10 @@ test('match applies to macros', t => {
match: ['foobar']
});

runner.on('test', props => {
t.is(props.title, 'foobar');
});

runner.chain.test(macroFn, 'foo');
runner.chain.test(macroFn, 'bar');

Expand Down Expand Up @@ -842,7 +848,6 @@ test('match applies to arrays of macros', t => {
fooMacro.title = (title, firstArg) => `${firstArg}foo`;

function barMacro(avaT) {
t.is(avaT.title, 'foobar');
avaT.pass();
}
barMacro.title = (title, firstArg) => `${firstArg}bar`;
Expand All @@ -857,6 +862,10 @@ test('match applies to arrays of macros', t => {
match: ['foobar']
});

runner.on('test', props => {
t.is(props.title, 'foobar');
});

runner.chain.test([fooMacro, barMacro, bazMacro], 'foo');
runner.chain.test([fooMacro, barMacro, bazMacro], 'bar');

Expand Down
62 changes: 0 additions & 62 deletions test/test-collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,68 +239,6 @@ test('adding a bunch of different types', t => {
t.end();
});

test('foo', t => {
const collection = new TestCollection({});
const log = [];

function logger(a) {
log.push(a.title);
a.pass();
}

function add(title, opts) {
collection.add({
title,
metadata: metadata(opts),
fn: logger
});
}

add('after1', {type: 'after'});
add('after.always', {
type: 'after',
always: true
});
add('beforeEach1', {type: 'beforeEach'});
add('before1', {type: 'before'});
add('beforeEach2', {type: 'beforeEach'});
add('afterEach1', {type: 'afterEach'});
add('afterEach.always', {
type: 'afterEach',
always: true
});
add('test1', {});
add('afterEach2', {type: 'afterEach'});
add('test2', {});
add('after2', {type: 'after'});
add('before2', {type: 'before'});

const passed = collection.build().run();
t.is(passed, true);

t.strictDeepEqual(log, [
'before1',
'before2',
'beforeEach1 for test1',
'beforeEach2 for test1',
'test1',
'afterEach1 for test1',
'afterEach2 for test1',
'afterEach.always for test1',
'beforeEach1 for test2',
'beforeEach2 for test2',
'test2',
'afterEach1 for test2',
'afterEach2 for test2',
'afterEach.always for test2',
'after1',
'after2',
'after.always'
]);

t.end();
});

test('foo', t => {
const collection = new TestCollection({});
const log = [];
Expand Down
13 changes: 13 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,19 @@ test('end can be used as callback with a non-error as its error argument', t =>
t.end();
});

test('title returns the test title', t => {
t.plan(1);
new Test({
fn(a) {
t.is(a.title, 'foo');
a.pass();
},
metadata: {type: 'test', callback: false},
onResult: noop,
title: 'foo'
}).run();
});

test('handle non-assertion errors even when planned', t => {
const err = new Error('bar');
let result;
Expand Down

0 comments on commit 549e99b

Please sign in to comment.