Skip to content

Commit 5b9b47e

Browse files
authored
feat(valid-expect): supporting automatically fixing adding async in some 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
1 parent 0a14446 commit 5b9b47e

File tree

2 files changed

+188
-26
lines changed

2 files changed

+188
-26
lines changed

src/rules/__tests__/valid-expect.test.ts

+116-3
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,6 @@ ruleTester.run('valid-expect', rule, {
144144
},
145145
],
146146
},
147-
148147
{
149148
code: 'expect().toBe(true);',
150149
errors: [
@@ -417,7 +416,6 @@ ruleTester.run('valid-expect', rule, {
417416
},
418417
],
419418
},
420-
421419
{
422420
code: dedent`
423421
expect.extend({
@@ -428,6 +426,15 @@ ruleTester.run('valid-expect', rule, {
428426
}
429427
});
430428
`,
429+
output: dedent`
430+
expect.extend({
431+
async toResolve(obj) {
432+
this.isNot
433+
? expect(obj).toBe(true)
434+
: await expect(obj).resolves.not.toThrow();
435+
}
436+
});
437+
`,
431438
errors: [
432439
{
433440
column: 9,
@@ -446,6 +453,15 @@ ruleTester.run('valid-expect', rule, {
446453
}
447454
});
448455
`,
456+
output: dedent`
457+
expect.extend({
458+
async toResolve(obj) {
459+
this.isNot
460+
? await expect(obj).resolves.not.toThrow()
461+
: expect(obj).toBe(true);
462+
}
463+
});
464+
`,
449465
errors: [
450466
{
451467
column: 9,
@@ -466,6 +482,17 @@ ruleTester.run('valid-expect', rule, {
466482
}
467483
});
468484
`,
485+
output: dedent`
486+
expect.extend({
487+
async toResolve(obj) {
488+
this.isNot
489+
? expect(obj).toBe(true)
490+
: anotherCondition
491+
? await expect(obj).resolves.not.toThrow()
492+
: expect(obj).toBe(false)
493+
}
494+
});
495+
`,
469496
errors: [
470497
{
471498
column: 9,
@@ -478,6 +505,8 @@ ruleTester.run('valid-expect', rule, {
478505
// expect().resolves
479506
{
480507
code: 'test("valid-expect", () => { expect(Promise.resolve(2)).resolves.toBeDefined(); });',
508+
output:
509+
'test("valid-expect", async () => { await expect(Promise.resolve(2)).resolves.toBeDefined(); });',
481510
errors: [
482511
{
483512
column: 30,
@@ -489,6 +518,8 @@ ruleTester.run('valid-expect', rule, {
489518
},
490519
{
491520
code: 'test("valid-expect", () => { expect(Promise.resolve(2)).toResolve(); });',
521+
output:
522+
'test("valid-expect", async () => { await expect(Promise.resolve(2)).toResolve(); });',
492523
errors: [
493524
{
494525
messageId: 'asyncMustBeAwaited',
@@ -500,6 +531,8 @@ ruleTester.run('valid-expect', rule, {
500531
},
501532
{
502533
code: 'test("valid-expect", () => { expect(Promise.resolve(2)).toResolve(); });',
534+
output:
535+
'test("valid-expect", async () => { await expect(Promise.resolve(2)).toResolve(); });',
503536
options: [{ asyncMatchers: undefined }],
504537
errors: [
505538
{
@@ -512,6 +545,8 @@ ruleTester.run('valid-expect', rule, {
512545
},
513546
{
514547
code: 'test("valid-expect", () => { expect(Promise.resolve(2)).toReject(); });',
548+
output:
549+
'test("valid-expect", async () => { await expect(Promise.resolve(2)).toReject(); });',
515550
errors: [
516551
{
517552
messageId: 'asyncMustBeAwaited',
@@ -523,6 +558,8 @@ ruleTester.run('valid-expect', rule, {
523558
},
524559
{
525560
code: 'test("valid-expect", () => { expect(Promise.resolve(2)).not.toReject(); });',
561+
output:
562+
'test("valid-expect", async () => { await expect(Promise.resolve(2)).not.toReject(); });',
526563
errors: [
527564
{
528565
messageId: 'asyncMustBeAwaited',
@@ -535,6 +572,8 @@ ruleTester.run('valid-expect', rule, {
535572
// expect().resolves.not
536573
{
537574
code: 'test("valid-expect", () => { expect(Promise.resolve(2)).resolves.not.toBeDefined(); });',
575+
output:
576+
'test("valid-expect", async () => { await expect(Promise.resolve(2)).resolves.not.toBeDefined(); });',
538577
errors: [
539578
{
540579
column: 30,
@@ -547,6 +586,8 @@ ruleTester.run('valid-expect', rule, {
547586
// expect().rejects
548587
{
549588
code: 'test("valid-expect", () => { expect(Promise.resolve(2)).rejects.toBeDefined(); });',
589+
output:
590+
'test("valid-expect", async () => { await expect(Promise.resolve(2)).rejects.toBeDefined(); });',
550591
errors: [
551592
{
552593
column: 30,
@@ -559,6 +600,8 @@ ruleTester.run('valid-expect', rule, {
559600
// expect().rejects.not
560601
{
561602
code: 'test("valid-expect", () => { expect(Promise.resolve(2)).rejects.not.toBeDefined(); });',
603+
output:
604+
'test("valid-expect", async () => { await expect(Promise.resolve(2)).rejects.not.toBeDefined(); });',
562605
errors: [
563606
{
564607
column: 30,
@@ -597,6 +640,8 @@ ruleTester.run('valid-expect', rule, {
597640
},
598641
{
599642
code: 'test("valid-expect", () => { expect(Promise.reject(2)).toRejectWith(2); });',
643+
output:
644+
'test("valid-expect", async () => { await expect(Promise.reject(2)).toRejectWith(2); });',
600645
options: [{ asyncMatchers: ['toRejectWith'] }],
601646
errors: [
602647
{
@@ -608,6 +653,8 @@ ruleTester.run('valid-expect', rule, {
608653
},
609654
{
610655
code: 'test("valid-expect", () => { expect(Promise.reject(2)).rejects.toBe(2); });',
656+
output:
657+
'test("valid-expect", async () => { await expect(Promise.reject(2)).rejects.toBe(2); });',
611658
options: [{ asyncMatchers: ['toRejectWith'] }],
612659
errors: [
613660
{
@@ -785,6 +832,11 @@ ruleTester.run('valid-expect', rule, {
785832
Promise.resolve(expect(Promise.resolve(2)).resolves.not.toBeDefined());
786833
});
787834
`,
835+
output: dedent`
836+
test("valid-expect", async () => {
837+
await Promise.resolve(expect(Promise.resolve(2)).resolves.not.toBeDefined());
838+
});
839+
`,
788840
errors: [
789841
{
790842
line: 2,
@@ -801,6 +853,11 @@ ruleTester.run('valid-expect', rule, {
801853
Promise.reject(expect(Promise.resolve(2)).resolves.not.toBeDefined());
802854
});
803855
`,
856+
output: dedent`
857+
test("valid-expect", async () => {
858+
await Promise.reject(expect(Promise.resolve(2)).resolves.not.toBeDefined());
859+
});
860+
`,
804861
errors: [
805862
{
806863
line: 2,
@@ -838,6 +895,11 @@ ruleTester.run('valid-expect', rule, {
838895
Promise.x(expect(Promise.resolve(2)).resolves.not.toBeDefined());
839896
});
840897
`,
898+
output: dedent`
899+
test("valid-expect", async () => {
900+
await Promise.x(expect(Promise.resolve(2)).resolves.not.toBeDefined());
901+
});
902+
`,
841903
errors: [
842904
{
843905
line: 2,
@@ -855,6 +917,11 @@ ruleTester.run('valid-expect', rule, {
855917
Promise.resolve(expect(Promise.resolve(2)).resolves.not.toBeDefined());
856918
});
857919
`,
920+
output: dedent`
921+
test("valid-expect", async () => {
922+
await Promise.resolve(expect(Promise.resolve(2)).resolves.not.toBeDefined());
923+
});
924+
`,
858925
options: [{ alwaysAwait: true }],
859926
errors: [
860927
{
@@ -875,6 +942,14 @@ ruleTester.run('valid-expect', rule, {
875942
]);
876943
});
877944
`,
945+
output: dedent`
946+
test("valid-expect", async () => {
947+
await Promise.all([
948+
expect(Promise.resolve(2)).resolves.not.toBeDefined(),
949+
expect(Promise.resolve(3)).resolves.not.toBeDefined(),
950+
]);
951+
});
952+
`,
878953
errors: [
879954
{
880955
line: 2,
@@ -896,6 +971,14 @@ ruleTester.run('valid-expect', rule, {
896971
]);
897972
});
898973
`,
974+
output: dedent`
975+
test("valid-expect", async () => {
976+
await Promise.x([
977+
expect(Promise.resolve(2)).resolves.not.toBeDefined(),
978+
expect(Promise.resolve(3)).resolves.not.toBeDefined(),
979+
]);
980+
});
981+
`,
899982
errors: [
900983
{
901984
line: 2,
@@ -907,7 +990,6 @@ ruleTester.run('valid-expect', rule, {
907990
},
908991
],
909992
},
910-
//
911993
{
912994
code: dedent`
913995
test("valid-expect", () => {
@@ -917,6 +999,14 @@ ruleTester.run('valid-expect', rule, {
917999
]
9181000
});
9191001
`,
1002+
output: dedent`
1003+
test("valid-expect", async () => {
1004+
const assertions = [
1005+
await expect(Promise.resolve(2)).resolves.not.toBeDefined(),
1006+
await expect(Promise.resolve(3)).resolves.not.toBeDefined(),
1007+
]
1008+
});
1009+
`,
9201010
errors: [
9211011
{
9221012
line: 3,
@@ -945,6 +1035,14 @@ ruleTester.run('valid-expect', rule, {
9451035
]
9461036
});
9471037
`,
1038+
output: dedent`
1039+
test("valid-expect", async () => {
1040+
const assertions = [
1041+
await expect(Promise.resolve(2)).toResolve(),
1042+
await expect(Promise.resolve(3)).toReject(),
1043+
]
1044+
});
1045+
`,
9481046
errors: [
9491047
{
9501048
messageId: 'asyncMustBeAwaited',
@@ -969,6 +1067,14 @@ ruleTester.run('valid-expect', rule, {
9691067
]
9701068
});
9711069
`,
1070+
output: dedent`
1071+
test("valid-expect", async () => {
1072+
const assertions = [
1073+
await expect(Promise.resolve(2)).not.toResolve(),
1074+
await expect(Promise.resolve(3)).resolves.toReject(),
1075+
]
1076+
});
1077+
`,
9721078
errors: [
9731079
{
9741080
messageId: 'asyncMustBeAwaited',
@@ -1002,6 +1108,13 @@ ruleTester.run('valid-expect', rule, {
10021108
});
10031109
});
10041110
`,
1111+
output: dedent`
1112+
test("valid-expect", () => {
1113+
return expect(functionReturningAPromise()).resolves.toEqual(1).then(async () => {
1114+
await expect(Promise.resolve(2)).resolves.toBe(1);
1115+
});
1116+
});
1117+
`,
10051118
errors: [
10061119
{
10071120
line: 3,

0 commit comments

Comments
 (0)