Skip to content

Require unique test titles #1669

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 2 commits into from
Jan 29, 2018
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
10 changes: 5 additions & 5 deletions docs/common-pitfalls.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Use the `concurrency` flag to limit the number of processes ran. For example, if
You may be running an asynchronous operation inside a test and wondering why it's not finishing. If your asynchronous operation uses promises, you should return the promise:

```js
test(t => {
test('fetches foo', t => {
return fetch().then(data => {
t.is(data, 'foo');
});
Expand All @@ -35,7 +35,7 @@ test(t => {
Better yet, use `async` / `await`:

```js
test(async t => {
test('fetches foo', async t => {
const data = await fetch();
t.is(data, 'foo');
});
Expand All @@ -44,7 +44,7 @@ test(async t => {
If you're using callbacks, use [`test.cb`](https://github.com/avajs/ava#callback-support):

```js
test.cb(t => {
test.cb('fetches foo', t => {
fetch((err, data) => {
t.is(data, 'foo');
t.end();
Expand All @@ -55,7 +55,7 @@ test.cb(t => {
Alternatively, promisify the callback function using something like [`pify`](https://github.com/sindresorhus/pify):

```js
test(async t => {
test('fetches foo', async t => {
const data = await pify(fetch)();
t.is(data, 'foo');
});
Expand All @@ -70,7 +70,7 @@ AVA [can't trace uncaught exceptions](https://github.com/avajs/ava/issues/214) b
Ensure that the first parameter passed into your test is named `t`. This is a requirement of [`power-assert`](https://github.com/power-assert-js/power-assert), the library that provides the enhanced messages.

```js
test(t => {
test('one is one', t => {
t.is(1, 1);
});
```
Expand Down
16 changes: 8 additions & 8 deletions docs/recipes/when-to-use-plan.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Many users transitioning from `tap`/`tape` are accustomed to using `t.plan()` pr
`t.plan()` is unnecessary in most sync tests.

```js
test(t => {
test('simple sums', t => {
// BAD: there is no branching here - t.plan() is pointless
t.plan(2);

Expand All @@ -27,7 +27,7 @@ test(t => {
### Promises that are expected to resolve

```js
test(t => {
test('gives foo', t => {
t.plan(1);

return somePromise().then(result => {
Expand All @@ -43,15 +43,15 @@ At a glance, this tests appears to make good use of `t.plan()` since an async pr
2. It would be better to take advantage of `async`/`await`:

```js
test(async t => {
test('gives foo', async t => {
t.is(await somePromise(), 'foo');
});
```

### Promises with a `.catch()` block

```js
test(t => {
test('rejects with foo', t => {
t.plan(2);

return shouldRejectWithFoo().catch(reason => {
Expand All @@ -65,7 +65,7 @@ Here, the use of `t.plan()` seeks to ensure that the code inside the `catch` blo
Instead, you should take advantage of `t.throws` and `async`/`await`, as this leads to flatter code that is easier to reason about:

```js
test(async t => {
test('rejects with foo', async t => {
const reason = await t.throws(shouldRejectWithFoo());
t.is(reason.message, 'Hello');
t.is(reason.foo, 'bar');
Expand All @@ -75,7 +75,7 @@ test(async t => {
### Ensuring a catch statement happens

```js
test(t => {
test('throws', t => {
t.plan(2);

try {
Expand All @@ -96,7 +96,7 @@ As stated in the previous example, using the `t.throws()` assertion with `async`
### Ensuring multiple callbacks are actually called

```js
test.cb(t => {
test.cb('invokes callbacks', t => {
t.plan(2);

const callbackA = () => {
Expand All @@ -120,7 +120,7 @@ In most cases, it's a bad idea to use any complex branching inside your tests. A
const testData = require('./fixtures/test-definitions.json');

testData.forEach(testDefinition => {
test(t => {
test('foo or bar', t => {
const result = functionUnderTest(testDefinition.input);

// testDefinition should have an expectation for `foo` or `bar` but not both
Expand Down
12 changes: 6 additions & 6 deletions index.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ type ContextualCallbackTest = TestImplementation<ContextualCallbackTestContext,
*/

type ContextualTestMethod = {
( implementation: ContextualTest): void;
(name: string, implementation: ContextualTest): void;
( implementation: ContextualTest): void;
(title: string, implementation: ContextualTest): void;

serial : ContextualTestMethod;
before : ContextualTestMethod;
Expand All @@ -128,8 +128,8 @@ type ContextualTestMethod = {
};

type ContextualCallbackTestMethod = {
( implementation: ContextualCallbackTest): void;
(name: string, implementation: ContextualCallbackTest): void;
( implementation: ContextualCallbackTest): void;
(title: string, implementation: ContextualCallbackTest): void;

serial : ContextualCallbackTestMethod;
before : ContextualCallbackTestMethod;
Expand All @@ -149,8 +149,8 @@ type ContextualCallbackTestMethod = {
*/

declare module.exports: {
( run: ContextualTest, ...args: any): void;
(name: string, run: ContextualTest, ...args: any): void;
( run: ContextualTest, ...args: any): void;
(title: string, run: ContextualTest, ...args: any): void;

beforeEach : ContextualTestMethod;
afterEach : ContextualTestMethod;
Expand Down
23 changes: 11 additions & 12 deletions lib/test-collection.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';
const EventEmitter = require('events');
const fnName = require('fn-name');
const Concurrent = require('./concurrent');
const Sequence = require('./sequence');
const Test = require('./test');
Expand Down Expand Up @@ -30,6 +29,7 @@ class TestCollection extends EventEmitter {
};

this.pendingTestInstances = new Set();
this.uniqueTestTitles = new Set();

this._emitTestResult = this._emitTestResult.bind(this);
}
Expand All @@ -42,20 +42,19 @@ class TestCollection extends EventEmitter {
throw new Error('Test type must be specified');
}

if (!test.title && test.fn) {
test.title = fnName(test.fn);
}

// Workaround for Babel giving anonymous functions a name
if (test.title === 'callee$0$0') {
test.title = null;
if (test.title === '' || typeof test.title !== 'string') {
if (type === 'test') {
throw new TypeError('Tests must have a title');
} else {
test.title = `${type} hook`;
}
}

if (!test.title) {
if (type === 'test') {
test.title = '[anonymous]';
if (type === 'test') {
if (this.uniqueTestTitles.has(test.title)) {
throw new Error(`Duplicate test title: ${test.title}`);
} else {
test.title = type;
this.uniqueTestTitles.add(test.title);
}
}

Expand Down
5 changes: 0 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@
"equal-length": "^1.0.0",
"figures": "^2.0.0",
"find-cache-dir": "^1.0.0",
"fn-name": "^2.0.0",
"get-port": "^3.2.0",
"globby": "^7.1.1",
"has-flag": "^3.0.0",
Expand Down
Loading