Skip to content
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

Next #579

Merged
merged 17 commits into from Sep 4, 2020
Merged

Next #579

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions .github/workflows/lint.yml
Expand Up @@ -19,6 +19,15 @@ jobs:
- uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Get yarn cache
id: yarn-cache
run: echo "::set-output name=dir::$(yarn config get cacheFolder)"
- uses: actions/cache@v2
with:
path: ${{ steps.yarn-cache.outputs.dir }}
key: ubuntu-latest-node-12.x-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
ubuntu-latest-node-12.x-yarn-
- uses: actions/setup-node@v2.1.1
with:
node-version: 12.x
Expand Down
26 changes: 21 additions & 5 deletions .github/workflows/nodejs.yml
Expand Up @@ -18,16 +18,21 @@ jobs:
strategy:
fail-fast: false
matrix:
node-version: [8.x, 10.x, 12.x, 13.x, 14.x]
node-version: [10.x, 12.x, 14.x]
eslint-version: [5, 6, 7]
exclude:
# eslint@7 doesn't support node@8
- node-version: 8.x
eslint-version: 7
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Get yarn cache
id: yarn-cache
run: echo "::set-output name=dir::$(yarn config get cacheFolder)"
- uses: actions/cache@v2
with:
path: ${{ steps.yarn-cache.outputs.dir }}
key: ubuntu-latest-node-12.x-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
ubuntu-latest-node-12.x-yarn-
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2.1.1
with:
Expand Down Expand Up @@ -57,6 +62,17 @@ jobs:

steps:
- uses: actions/checkout@v2
- name: Get yarn cache
id: yarn-cache
run: echo "::set-output name=dir::$(yarn config get cacheFolder)"
- uses: actions/cache@v2
with:
path: ${{ steps.yarn-cache.outputs.dir }}
key:
${{ runner.os }}-node-${{ matrix.node-version }}-yarn-${{
hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-node-${{ matrix.node-version }}-yarn-
- uses: actions/setup-node@v2.1.1
with:
node-version: 12.x
Expand Down
11 changes: 10 additions & 1 deletion .github/workflows/release.yml
Expand Up @@ -10,9 +10,18 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Get yarn cache
id: yarn-cache
run: echo "::set-output name=dir::$(yarn config get cacheFolder)"
- uses: actions/cache@v2
with:
path: ${{ steps.yarn-cache.outputs.dir }}
key: ubuntu-latest-node-12.x-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
ubuntu-latest-node-12.x-yarn-
- uses: actions/setup-node@v2.1.1
with:
node-version: '12.x'
node-version: 12.x
- name: install
run: yarn
- name: run prettier
Expand Down
6 changes: 6 additions & 0 deletions .gitignore
Expand Up @@ -3,3 +3,9 @@ node_modules/
coverage/
lib/
*.log
.yarn/*
!.yarn/releases
!.yarn/plugins
!.yarn/sdks
!.yarn/versions
.pnp.*
38 changes: 38 additions & 0 deletions .yarn/plugins/@yarnpkg/plugin-interactive-tools.cjs

Large diffs are not rendered by default.

55 changes: 55 additions & 0 deletions .yarn/releases/yarn-sources.cjs

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions .yarnrc
@@ -0,0 +1,6 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


lastUpdateCheck 1576854265612
yarn-path ".yarn/releases/yarn-sources.cjs"
9 changes: 9 additions & 0 deletions .yarnrc.yml
@@ -0,0 +1,9 @@
enableGlobalCache: true

nodeLinker: node-modules

plugins:
- path: .yarn/plugins/@yarnpkg/plugin-interactive-tools.cjs
spec: '@yarnpkg/plugin-interactive-tools'

yarnPath: .yarn/releases/yarn-sources.cjs
10 changes: 5 additions & 5 deletions README.md
Expand Up @@ -135,23 +135,23 @@ installations requiring long-term consistency.
| [lowercase-name](docs/rules/lowercase-name.md) | Enforce lowercase test names | | ![fixable][] |
| [no-alias-methods](docs/rules/no-alias-methods.md) | Disallow alias methods | ![style][] | ![fixable][] |
| [no-commented-out-tests](docs/rules/no-commented-out-tests.md) | Disallow commented out tests | ![recommended][] | |
| [no-conditional-expect](docs/rules/no-conditional-expect.md) | Prevent calling `expect` conditionally | | |
| [no-deprecated-functions](docs/rules/no-deprecated-functions.md) | Disallow use of deprecated functions | | ![fixable][] |
| [no-conditional-expect](docs/rules/no-conditional-expect.md) | Prevent calling `expect` conditionally | ![recommended][] | |
| [no-deprecated-functions](docs/rules/no-deprecated-functions.md) | Disallow use of deprecated functions | ![recommended][] | ![fixable][] |
| [no-disabled-tests](docs/rules/no-disabled-tests.md) | Disallow disabled tests | ![recommended][] | |
| [no-done-callback](docs/rules/no-done-callback.md) | Avoid using a callback in asynchronous tests and hooks | ![recommended][] | ![suggest][] |
| [no-duplicate-hooks](docs/rules/no-duplicate-hooks.md) | Disallow duplicate setup and teardown hooks | | |
| [no-export](docs/rules/no-export.md) | Disallow using `exports` in files containing tests | ![recommended][] | |
| [no-focused-tests](docs/rules/no-focused-tests.md) | Disallow focused tests | ![recommended][] | ![fixable][] |
| [no-hooks](docs/rules/no-hooks.md) | Disallow setup and teardown hooks | | |
| [no-identical-title](docs/rules/no-identical-title.md) | Disallow identical titles | ![recommended][] | |
| [no-if](docs/rules/no-if.md) | Disallow conditional logic | | |
| [no-interpolation-in-snapshots](docs/rules/no-interpolation-in-snapshots.md) | Disallow string interpolation inside snapshots | | |
| [no-interpolation-in-snapshots](docs/rules/no-interpolation-in-snapshots.md) | Disallow string interpolation inside snapshots | ![recommended][] | |
| [no-jasmine-globals](docs/rules/no-jasmine-globals.md) | Disallow Jasmine globals | ![recommended][] | ![fixable][] |
| [no-jest-import](docs/rules/no-jest-import.md) | Disallow importing Jest | ![recommended][] | |
| [no-large-snapshots](docs/rules/no-large-snapshots.md) | disallow large snapshots | | |
| [no-mocks-import](docs/rules/no-mocks-import.md) | Disallow manually importing from `__mocks__` | ![recommended][] | |
| [no-restricted-matchers](docs/rules/no-restricted-matchers.md) | Disallow specific matchers & modifiers | | |
| [no-standalone-expect](docs/rules/no-standalone-expect.md) | Disallow using `expect` outside of `it` or `test` blocks | ![recommended][] | |
| [no-test-callback](docs/rules/no-test-callback.md) | Avoid using a callback in asynchronous tests | ![recommended][] | ![suggest][] |
| [no-test-prefixes](docs/rules/no-test-prefixes.md) | Use `.only` and `.skip` over `f` and `x` | ![recommended][] | ![fixable][] |
| [no-test-return-statement](docs/rules/no-test-return-statement.md) | Disallow explicitly returning from tests | | |
| [prefer-called-with](docs/rules/prefer-called-with.md) | Suggest using `toBeCalledWith()` or `toHaveBeenCalledWith()` | | |
Expand All @@ -169,7 +169,7 @@ installations requiring long-term consistency.
| [valid-describe](docs/rules/valid-describe.md) | Enforce valid `describe()` callback | ![recommended][] | |
| [valid-expect](docs/rules/valid-expect.md) | Enforce valid `expect()` usage | ![recommended][] | |
| [valid-expect-in-promise](docs/rules/valid-expect-in-promise.md) | Enforce having return statement when testing with promises | ![recommended][] | |
| [valid-title](docs/rules/valid-title.md) | Enforce valid titles | | ![fixable][] |
| [valid-title](docs/rules/valid-title.md) | Enforce valid titles | ![recommended][] | ![fixable][] |

<!-- end rules list -->

Expand Down
2 changes: 1 addition & 1 deletion babel.config.js
Expand Up @@ -5,6 +5,6 @@ module.exports = {
plugins: ['replace-ts-export-assignment'],
presets: [
'@babel/preset-typescript',
['@babel/preset-env', { targets: { node: 8 } }],
['@babel/preset-env', { targets: { node: 10 } }],
],
};
8 changes: 4 additions & 4 deletions docs/rules/consistent-test-it.md
Expand Up @@ -59,12 +59,12 @@ test.only('foo'); // invalid
/*eslint jest/consistent-test-it: ["error", {"fn": "it", "withinDescribe": "test"}]*/

it('foo'); // valid
describe('foo', function() {
describe('foo', function () {
test('bar'); // valid
});

test('foo'); // invalid
describe('foo', function() {
describe('foo', function () {
it('bar'); // invalid
});
```
Expand All @@ -78,12 +78,12 @@ nested within `describe` to use `it`.
/*eslint jest/consistent-test-it: ["error"]*/

test('foo'); // valid
describe('foo', function() {
describe('foo', function () {
it('bar'); // valid
});

it('foo'); // invalid
describe('foo', function() {
describe('foo', function () {
test('bar'); // invalid
});
```
17 changes: 5 additions & 12 deletions docs/rules/expect-expect.md
Expand Up @@ -56,9 +56,7 @@ import { expectSaga } from 'redux-saga-test-plan';
import { addSaga } from '../src/sagas';

test('returns sum', () => {
expectSaga(addSaga, 1, 1)
.returns(2)
.run();
expectSaga(addSaga, 1, 1).returns(2).run();
});
```

Expand All @@ -72,9 +70,7 @@ import { expectSaga } from 'redux-saga-test-plan';
import { addSaga } from '../src/sagas';

test('returns sum', () => {
expectSaga(addSaga, 1, 1)
.returns(2)
.run();
expectSaga(addSaga, 1, 1).returns(2).run();
});
```

Expand All @@ -100,12 +96,9 @@ const express = require('express');

const app = express();

describe('GET /user', function() {
it('responds with json', function(done) {
request(app)
.get('/user')
.expect('Content-Type', /json/)
.expect(200, done);
describe('GET /user', function () {
it('responds with json', function (done) {
request(app).get('/user').expect('Content-Type', /json/).expect(200, done);
});
});
```
90 changes: 90 additions & 0 deletions docs/rules/no-done-callback.md
@@ -0,0 +1,90 @@
# Avoid using a callback in asynchronous tests and hooks (`no-done-callback`)

When calling asynchronous code in hooks and tests, `jest` needs to know when the
asynchronous work is complete to progress the current run.

Originally the most common pattern to archive this was to use callbacks:

```js
test('the data is peanut butter', done => {
function callback(data) {
try {
expect(data).toBe('peanut butter');
done();
} catch (error) {
done(error);
}
}

fetchData(callback);
});
```

This can be very error prone however, as it requires careful understanding of
how assertions work in tests or otherwise tests won't behave as expected.

For example, if the `try/catch` was left out of the above code, the test would
timeout rather than fail. Even with the `try/catch`, forgetting to pass the
caught error to `done` will result in `jest` believing the test has passed.

A more straightforward way to handle asynchronous code is to use Promises:

```js
test('the data is peanut butter', () => {
return fetchData().then(data => {
expect(data).toBe('peanut butter');
});
});
```

When a test or hook returns a promise, `jest` waits for that promise to resolve,
as well as automatically failing should the promise reject.

If your environment supports `async/await`, this becomes even simpler:

```js
test('the data is peanut butter', async () => {
const data = await fetchData();
expect(data).toBe('peanut butter');
});
```

## Rule details

This rule checks the function parameter of hooks & tests for use of the `done`
argument, suggesting you return a promise instead.

The following patterns are considered warnings:

```js
beforeEach(done => {
// ...
});

test('myFunction()', done => {
// ...
});

test('myFunction()', function (done) {
// ...
});
```

The following patterns are not considered warnings:

```js
beforeEach(async () => {
await setupUsTheBomb();
});

test('myFunction()', () => {
expect(myFunction()).toBeTruthy();
});

test('myFunction()', () => {
return new Promise(done => {
expect(myFunction()).toBeTruthy();
done();
});
});
```
2 changes: 1 addition & 1 deletion docs/rules/no-export.md
Expand Up @@ -15,7 +15,7 @@ Examples of **incorrect** code for this rule:
```js
export function myHelper() {}

module.exports = function() {};
module.exports = function () {};

module.exports = {
something: 'that should be moved to a non-test file',
Expand Down
76 changes: 0 additions & 76 deletions docs/rules/no-test-callback.md

This file was deleted.