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

Allow awaited expects to be next to regular ones #100

Merged
merged 1 commit into from
May 4, 2021
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
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