Skip to content

Commit

Permalink
Allow awaited statements to be next to regular ones (#100)
Browse files Browse the repository at this point in the history
fixes #98
  • Loading branch information
tmercswims committed May 4, 2021
1 parent 615a24e commit 5e6fee4
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 9 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## v3.0.0 - May 4, 2021

### Breaking Changes

- Padding is no longer enforced between statements and `await`ed statements of the same kind.

## v1.1.0 - August 14, 2019

### Changed
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "eslint-plugin-jest-formatting",
"version": "2.0.1",
"version": "3.0.0",
"description": "ESLint rules for formatting jest tests",
"keywords": [
"eslint",
Expand Down
19 changes: 13 additions & 6 deletions src/rules/padding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,20 @@ interface PaddingContext {
// Creates a StatementTester to test an ExpressionStatement's first token name
const createTokenTester = (tokenName: string): StatementTester => {
return (node: Node, sourceCode: SourceCode): boolean => {
const token = sourceCode.getFirstToken(node);
let activeNode = node;

return (
node.type === 'ExpressionStatement' &&
token.type === 'Identifier' &&
token.value === tokenName
);
if (activeNode.type === 'ExpressionStatement') {
// In the case of `await`, we actually care about its argument
if (activeNode.expression.type === 'AwaitExpression') {
activeNode = activeNode.expression.argument;
}

const token = sourceCode.getFirstToken(activeNode);

return token.type === 'Identifier' && token.value === tokenName;
}

return false;
};
};

Expand Down
66 changes: 64 additions & 2 deletions tests/lib/rules/padding-around-expect-groups.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const rule = require('../../../lib').rules['padding-around-expect-groups'];

const ruleTester = new RuleTester({
parserOptions: {
ecmaVersion: 6,
ecmaVersion: 2017,
},
});

Expand Down Expand Up @@ -63,6 +63,29 @@ describe('someText', () => {
});
});
});
test('awaited expect', async () => {
const abc = 123;
const hasAPromise = () => Promise.resolve('foo');
await expect(hasAPromise()).resolves.toEqual('foo');
expect(abc).toEqual(123);
const efg = 456;
expect(123).toEqual(abc);
await expect(hasAPromise()).resolves.toEqual('foo');
const hij = 789;
await expect(hasAPromise()).resolves.toEqual('foo');
await expect(hasAPromise()).resolves.toEqual('foo');
const somethingElseAsync = () => Promise.resolve('bar');
await somethingElseAsync();
await expect(hasAPromise()).resolves.toEqual('foo');
});
`;

const invalid = `
Expand Down Expand Up @@ -103,6 +126,25 @@ describe('someText', () => {
});
});
});
test('awaited expect', async () => {
const abc = 123;
const hasAPromise = () => Promise.resolve('foo');
await expect(hasAPromise()).resolves.toEqual('foo');
expect(abc).toEqual(123);
const efg = 456;
expect(123).toEqual(abc);
await expect(hasAPromise()).resolves.toEqual('foo');
const hij = 789;
await expect(hasAPromise()).resolves.toEqual('foo');
await expect(hasAPromise()).resolves.toEqual('foo');
const somethingElseAsync = () => Promise.resolve('bar');
await somethingElseAsync();
await expect(hasAPromise()).resolves.toEqual('foo');
});
`;

ruleTester.run('padding-around-expect-groups', rule, {
Expand All @@ -111,7 +153,7 @@ ruleTester.run('padding-around-expect-groups', rule, {
{
code: invalid,
filename: 'src/component.test.jsx',
errors: 6,
errors: 10,
output: valid,
},
{
Expand Down Expand Up @@ -148,6 +190,26 @@ ruleTester.run('padding-around-expect-groups', rule, {
line: 32,
column: 7
},
{
message: 'Expected blank line before this statement.',
line: 43,
column: 3
},
{
message: 'Expected blank line before this statement.',
line: 47,
column: 3
},
{
message: 'Expected blank line before this statement.',
line: 51,
column: 3
},
{
message: 'Expected blank line before this statement.',
line: 56,
column: 3
},
],
output: valid,
},
Expand Down

0 comments on commit 5e6fee4

Please sign in to comment.