Skip to content

Commit

Permalink
feat(valid-expect): supporting automatically fixing adding async in s…
Browse files Browse the repository at this point in the history
…ome cases (#1579)

* feat: add async

* test: tests for adding async

* feat: add await

* fix: valid-expect test

* Revert "fix: valid-expect test"

This reverts commit e652a25.

* fix: refactor to return an array

* fix: valid-expect logic

* Revert "fix: valid-expect logic"

This reverts commit ae8ecac.

* fix: valid-expect fixer logic

* refactor: fix import

* fix: write format
  • Loading branch information
y-hsgw committed Jun 6, 2024
1 parent 0a14446 commit 5b9b47e
Show file tree
Hide file tree
Showing 2 changed files with 188 additions and 26 deletions.
119 changes: 116 additions & 3 deletions src/rules/__tests__/valid-expect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ ruleTester.run('valid-expect', rule, {
},
],
},

{
code: 'expect().toBe(true);',
errors: [
Expand Down Expand Up @@ -417,7 +416,6 @@ ruleTester.run('valid-expect', rule, {
},
],
},

{
code: dedent`
expect.extend({
Expand All @@ -428,6 +426,15 @@ ruleTester.run('valid-expect', rule, {
}
});
`,
output: dedent`
expect.extend({
async toResolve(obj) {
this.isNot
? expect(obj).toBe(true)
: await expect(obj).resolves.not.toThrow();
}
});
`,
errors: [
{
column: 9,
Expand All @@ -446,6 +453,15 @@ ruleTester.run('valid-expect', rule, {
}
});
`,
output: dedent`
expect.extend({
async toResolve(obj) {
this.isNot
? await expect(obj).resolves.not.toThrow()
: expect(obj).toBe(true);
}
});
`,
errors: [
{
column: 9,
Expand All @@ -466,6 +482,17 @@ ruleTester.run('valid-expect', rule, {
}
});
`,
output: dedent`
expect.extend({
async toResolve(obj) {
this.isNot
? expect(obj).toBe(true)
: anotherCondition
? await expect(obj).resolves.not.toThrow()
: expect(obj).toBe(false)
}
});
`,
errors: [
{
column: 9,
Expand All @@ -478,6 +505,8 @@ ruleTester.run('valid-expect', rule, {
// expect().resolves
{
code: 'test("valid-expect", () => { expect(Promise.resolve(2)).resolves.toBeDefined(); });',
output:
'test("valid-expect", async () => { await expect(Promise.resolve(2)).resolves.toBeDefined(); });',
errors: [
{
column: 30,
Expand All @@ -489,6 +518,8 @@ ruleTester.run('valid-expect', rule, {
},
{
code: 'test("valid-expect", () => { expect(Promise.resolve(2)).toResolve(); });',
output:
'test("valid-expect", async () => { await expect(Promise.resolve(2)).toResolve(); });',
errors: [
{
messageId: 'asyncMustBeAwaited',
Expand All @@ -500,6 +531,8 @@ ruleTester.run('valid-expect', rule, {
},
{
code: 'test("valid-expect", () => { expect(Promise.resolve(2)).toResolve(); });',
output:
'test("valid-expect", async () => { await expect(Promise.resolve(2)).toResolve(); });',
options: [{ asyncMatchers: undefined }],
errors: [
{
Expand All @@ -512,6 +545,8 @@ ruleTester.run('valid-expect', rule, {
},
{
code: 'test("valid-expect", () => { expect(Promise.resolve(2)).toReject(); });',
output:
'test("valid-expect", async () => { await expect(Promise.resolve(2)).toReject(); });',
errors: [
{
messageId: 'asyncMustBeAwaited',
Expand All @@ -523,6 +558,8 @@ ruleTester.run('valid-expect', rule, {
},
{
code: 'test("valid-expect", () => { expect(Promise.resolve(2)).not.toReject(); });',
output:
'test("valid-expect", async () => { await expect(Promise.resolve(2)).not.toReject(); });',
errors: [
{
messageId: 'asyncMustBeAwaited',
Expand All @@ -535,6 +572,8 @@ ruleTester.run('valid-expect', rule, {
// expect().resolves.not
{
code: 'test("valid-expect", () => { expect(Promise.resolve(2)).resolves.not.toBeDefined(); });',
output:
'test("valid-expect", async () => { await expect(Promise.resolve(2)).resolves.not.toBeDefined(); });',
errors: [
{
column: 30,
Expand All @@ -547,6 +586,8 @@ ruleTester.run('valid-expect', rule, {
// expect().rejects
{
code: 'test("valid-expect", () => { expect(Promise.resolve(2)).rejects.toBeDefined(); });',
output:
'test("valid-expect", async () => { await expect(Promise.resolve(2)).rejects.toBeDefined(); });',
errors: [
{
column: 30,
Expand All @@ -559,6 +600,8 @@ ruleTester.run('valid-expect', rule, {
// expect().rejects.not
{
code: 'test("valid-expect", () => { expect(Promise.resolve(2)).rejects.not.toBeDefined(); });',
output:
'test("valid-expect", async () => { await expect(Promise.resolve(2)).rejects.not.toBeDefined(); });',
errors: [
{
column: 30,
Expand Down Expand Up @@ -597,6 +640,8 @@ ruleTester.run('valid-expect', rule, {
},
{
code: 'test("valid-expect", () => { expect(Promise.reject(2)).toRejectWith(2); });',
output:
'test("valid-expect", async () => { await expect(Promise.reject(2)).toRejectWith(2); });',
options: [{ asyncMatchers: ['toRejectWith'] }],
errors: [
{
Expand All @@ -608,6 +653,8 @@ ruleTester.run('valid-expect', rule, {
},
{
code: 'test("valid-expect", () => { expect(Promise.reject(2)).rejects.toBe(2); });',
output:
'test("valid-expect", async () => { await expect(Promise.reject(2)).rejects.toBe(2); });',
options: [{ asyncMatchers: ['toRejectWith'] }],
errors: [
{
Expand Down Expand Up @@ -785,6 +832,11 @@ ruleTester.run('valid-expect', rule, {
Promise.resolve(expect(Promise.resolve(2)).resolves.not.toBeDefined());
});
`,
output: dedent`
test("valid-expect", async () => {
await Promise.resolve(expect(Promise.resolve(2)).resolves.not.toBeDefined());
});
`,
errors: [
{
line: 2,
Expand All @@ -801,6 +853,11 @@ ruleTester.run('valid-expect', rule, {
Promise.reject(expect(Promise.resolve(2)).resolves.not.toBeDefined());
});
`,
output: dedent`
test("valid-expect", async () => {
await Promise.reject(expect(Promise.resolve(2)).resolves.not.toBeDefined());
});
`,
errors: [
{
line: 2,
Expand Down Expand Up @@ -838,6 +895,11 @@ ruleTester.run('valid-expect', rule, {
Promise.x(expect(Promise.resolve(2)).resolves.not.toBeDefined());
});
`,
output: dedent`
test("valid-expect", async () => {
await Promise.x(expect(Promise.resolve(2)).resolves.not.toBeDefined());
});
`,
errors: [
{
line: 2,
Expand All @@ -855,6 +917,11 @@ ruleTester.run('valid-expect', rule, {
Promise.resolve(expect(Promise.resolve(2)).resolves.not.toBeDefined());
});
`,
output: dedent`
test("valid-expect", async () => {
await Promise.resolve(expect(Promise.resolve(2)).resolves.not.toBeDefined());
});
`,
options: [{ alwaysAwait: true }],
errors: [
{
Expand All @@ -875,6 +942,14 @@ ruleTester.run('valid-expect', rule, {
]);
});
`,
output: dedent`
test("valid-expect", async () => {
await Promise.all([
expect(Promise.resolve(2)).resolves.not.toBeDefined(),
expect(Promise.resolve(3)).resolves.not.toBeDefined(),
]);
});
`,
errors: [
{
line: 2,
Expand All @@ -896,6 +971,14 @@ ruleTester.run('valid-expect', rule, {
]);
});
`,
output: dedent`
test("valid-expect", async () => {
await Promise.x([
expect(Promise.resolve(2)).resolves.not.toBeDefined(),
expect(Promise.resolve(3)).resolves.not.toBeDefined(),
]);
});
`,
errors: [
{
line: 2,
Expand All @@ -907,7 +990,6 @@ ruleTester.run('valid-expect', rule, {
},
],
},
//
{
code: dedent`
test("valid-expect", () => {
Expand All @@ -917,6 +999,14 @@ ruleTester.run('valid-expect', rule, {
]
});
`,
output: dedent`
test("valid-expect", async () => {
const assertions = [
await expect(Promise.resolve(2)).resolves.not.toBeDefined(),
await expect(Promise.resolve(3)).resolves.not.toBeDefined(),
]
});
`,
errors: [
{
line: 3,
Expand Down Expand Up @@ -945,6 +1035,14 @@ ruleTester.run('valid-expect', rule, {
]
});
`,
output: dedent`
test("valid-expect", async () => {
const assertions = [
await expect(Promise.resolve(2)).toResolve(),
await expect(Promise.resolve(3)).toReject(),
]
});
`,
errors: [
{
messageId: 'asyncMustBeAwaited',
Expand All @@ -969,6 +1067,14 @@ ruleTester.run('valid-expect', rule, {
]
});
`,
output: dedent`
test("valid-expect", async () => {
const assertions = [
await expect(Promise.resolve(2)).not.toResolve(),
await expect(Promise.resolve(3)).resolves.toReject(),
]
});
`,
errors: [
{
messageId: 'asyncMustBeAwaited',
Expand Down Expand Up @@ -1002,6 +1108,13 @@ ruleTester.run('valid-expect', rule, {
});
});
`,
output: dedent`
test("valid-expect", () => {
return expect(functionReturningAPromise()).resolves.toEqual(1).then(async () => {
await expect(Promise.resolve(2)).resolves.toBe(1);
});
});
`,
errors: [
{
line: 3,
Expand Down
Loading

0 comments on commit 5b9b47e

Please sign in to comment.