Skip to content

Commit ce86d4a

Browse files
G-RathSimenB
authored andcommitted
chore(valid-expect): convert to typescript (#387)
1 parent b39aad5 commit ce86d4a

File tree

4 files changed

+393
-397
lines changed

4 files changed

+393
-397
lines changed

src/rules/__tests__/valid-expect.test.js renamed to src/rules/__tests__/valid-expect.test.ts

Lines changed: 101 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { RuleTester } from 'eslint';
1+
import { TSESLint } from '@typescript-eslint/experimental-utils';
22
import rule from '../valid-expect';
33

4-
const ruleTester = new RuleTester({
4+
const ruleTester = new TSESLint.RuleTester({
55
parserOptions: {
66
ecmaVersion: 8,
77
},
@@ -15,23 +15,65 @@ ruleTester.run('valid-expect', rule, {
1515
'expect(undefined).not.toBeDefined();',
1616
'test("valid-expect", () => { return expect(Promise.resolve(2)).resolves.toBeDefined(); });',
1717
'test("valid-expect", () => { return expect(Promise.reject(2)).rejects.toBeDefined(); });',
18-
'test("valid-expect", () => { return expect(Promise.resolve(2)).not.resolves.toBeDefined(); });',
19-
'test("valid-expect", () => { return expect(Promise.resolve(2)).not.rejects.toBeDefined(); });',
20-
'test("valid-expect", function () { return expect(Promise.resolve(2)).not.resolves.toBeDefined(); });',
21-
'test("valid-expect", function () { return expect(Promise.resolve(2)).not.rejects.toBeDefined(); });',
22-
'test("valid-expect", function () { return Promise.resolve(expect(Promise.resolve(2)).not.resolves.toBeDefined()); });',
23-
'test("valid-expect", function () { return Promise.resolve(expect(Promise.resolve(2)).not.rejects.toBeDefined()); });',
18+
'test("valid-expect", () => { return expect(Promise.resolve(2)).resolves.not.toBeDefined(); });',
19+
'test("valid-expect", () => { return expect(Promise.resolve(2)).rejects.not.toBeDefined(); });',
20+
'test("valid-expect", function () { return expect(Promise.resolve(2)).resolves.not.toBeDefined(); });',
21+
'test("valid-expect", function () { return expect(Promise.resolve(2)).rejects.not.toBeDefined(); });',
22+
'test("valid-expect", function () { return Promise.resolve(expect(Promise.resolve(2)).resolves.not.toBeDefined()); });',
23+
'test("valid-expect", function () { return Promise.resolve(expect(Promise.resolve(2)).rejects.not.toBeDefined()); });',
2424
{
2525
code:
2626
'test("valid-expect", () => expect(Promise.resolve(2)).resolves.toBeDefined());',
2727
options: [{ alwaysAwait: true }],
2828
},
2929
'test("valid-expect", () => expect(Promise.resolve(2)).resolves.toBeDefined());',
3030
'test("valid-expect", () => expect(Promise.reject(2)).rejects.toBeDefined());',
31-
'test("valid-expect", () => expect(Promise.reject(2)).not.resolves.toBeDefined());',
32-
'test("valid-expect", () => expect(Promise.reject(2)).not.rejects.toBeDefined());',
3331
'test("valid-expect", () => expect(Promise.reject(2)).resolves.not.toBeDefined());',
3432
'test("valid-expect", () => expect(Promise.reject(2)).rejects.not.toBeDefined());',
33+
// 'test("valid-expect", () => expect(Promise.reject(2)).not.resolves.toBeDefined());',
34+
// 'test("valid-expect", () => expect(Promise.reject(2)).not.rejects.toBeDefined());',
35+
'test("valid-expect", async () => { await expect(Promise.reject(2)).resolves.not.toBeDefined(); });',
36+
'test("valid-expect", async () => { await expect(Promise.reject(2)).rejects.not.toBeDefined(); });',
37+
'test("valid-expect", async function () { await expect(Promise.reject(2)).resolves.not.toBeDefined(); });',
38+
'test("valid-expect", async function () { await expect(Promise.reject(2)).rejects.not.toBeDefined(); });',
39+
'test("valid-expect", async () => { await Promise.resolve(expect(Promise.reject(2)).rejects.not.toBeDefined()); });',
40+
'test("valid-expect", async () => { await Promise.reject(expect(Promise.reject(2)).rejects.not.toBeDefined()); });',
41+
'test("valid-expect", async () => { await Promise.all([expect(Promise.reject(2)).rejects.not.toBeDefined(), expect(Promise.reject(2)).rejects.not.toBeDefined()]); });',
42+
'test("valid-expect", async () => { await Promise.race([expect(Promise.reject(2)).rejects.not.toBeDefined(), expect(Promise.reject(2)).rejects.not.toBeDefined()]); });',
43+
'test("valid-expect", async () => { await Promise.allSettled([expect(Promise.reject(2)).rejects.not.toBeDefined(), expect(Promise.reject(2)).rejects.not.toBeDefined()]); });',
44+
'test("valid-expect", async () => { await Promise.any([expect(Promise.reject(2)).rejects.not.toBeDefined(), expect(Promise.reject(2)).rejects.not.toBeDefined()]); });',
45+
'test("valid-expect", async () => { return expect(Promise.reject(2)).resolves.not.toBeDefined().then(() => console.log("valid-case")); });',
46+
'test("valid-expect", async () => { return expect(Promise.reject(2)).resolves.not.toBeDefined().then(() => console.log("valid-case")).then(() => console.log("another valid case")); });',
47+
'test("valid-expect", async () => { return expect(Promise.reject(2)).resolves.not.toBeDefined().catch(() => console.log("valid-case")); });',
48+
'test("valid-expect", async () => { return expect(Promise.reject(2)).resolves.not.toBeDefined().then(() => console.log("valid-case")).catch(() => console.log("another valid case")); });',
49+
'test("valid-expect", async () => { return expect(Promise.reject(2)).resolves.not.toBeDefined().then(() => { expect(someMock).toHaveBeenCalledTimes(1); }); });',
50+
'test("valid-expect", async () => { await expect(Promise.reject(2)).resolves.not.toBeDefined().then(() => console.log("valid-case")); });',
51+
'test("valid-expect", async () => { await expect(Promise.reject(2)).resolves.not.toBeDefined().then(() => console.log("valid-case")).then(() => console.log("another valid case")); });',
52+
'test("valid-expect", async () => { await expect(Promise.reject(2)).resolves.not.toBeDefined().catch(() => console.log("valid-case")); });',
53+
'test("valid-expect", async () => { await expect(Promise.reject(2)).resolves.not.toBeDefined().then(() => console.log("valid-case")).catch(() => console.log("another valid case")); });',
54+
'test("valid-expect", async () => { await expect(Promise.reject(2)).resolves.not.toBeDefined().then(() => { expect(someMock).toHaveBeenCalledTimes(1); }); });',
55+
{
56+
code: `test("valid-expect", () => {
57+
return expect(functionReturningAPromise()).resolves.toEqual(1).then(() => {
58+
return expect(Promise.resolve(2)).resolves.toBe(1);
59+
});
60+
});`,
61+
},
62+
{
63+
code: `test("valid-expect", () => {
64+
return expect(functionReturningAPromise()).resolves.toEqual(1).then(async () => {
65+
await expect(Promise.resolve(2)).resolves.toBe(1);
66+
});
67+
});`,
68+
},
69+
{
70+
code: `test("valid-expect", () => {
71+
return expect(functionReturningAPromise()).resolves.toEqual(1).then(() => expect(Promise.resolve(2)).resolves.toBe(1));
72+
});`,
73+
},
74+
],
75+
invalid: [
76+
/*
3577
'test("valid-expect", async () => { await expect(Promise.reject(2)).not.resolves.toBeDefined(); });',
3678
'test("valid-expect", async () => { await expect(Promise.reject(2)).not.rejects.toBeDefined(); });',
3779
'test("valid-expect", async function () { await expect(Promise.reject(2)).not.resolves.toBeDefined(); });',
@@ -52,28 +94,7 @@ ruleTester.run('valid-expect', rule, {
5294
'test("valid-expect", async () => { await expect(Promise.reject(2)).not.resolves.toBeDefined().catch(() => console.log("valid-case")); });',
5395
'test("valid-expect", async () => { await expect(Promise.reject(2)).not.resolves.toBeDefined().then(() => console.log("valid-case")).catch(() => console.log("another valid case")); });',
5496
'test("valid-expect", async () => { await expect(Promise.reject(2)).not.resolves.toBeDefined().then(() => { expect(someMock).toHaveBeenCalledTimes(1); }); });',
55-
{
56-
code: `test("valid-expect", () => {
57-
return expect(functionReturningAPromise()).resolves.toEqual(1).then(() => {
58-
return expect(Promise.resolve(2)).resolves.toBe(1);
59-
});
60-
});`,
61-
},
62-
{
63-
code: `test("valid-expect", () => {
64-
return expect(functionReturningAPromise()).resolves.toEqual(1).then(async () => {
65-
await expect(Promise.resolve(2)).resolves.toBe(1);
66-
});
67-
});`,
68-
},
69-
{
70-
code: `test("valid-expect", () => {
71-
return expect(functionReturningAPromise()).resolves.toEqual(1).then(() => expect(Promise.resolve(2)).resolves.toBe(1));
72-
});`,
73-
},
74-
],
75-
76-
invalid: [
97+
*/
7798
{
7899
code: 'expect().toBe(true);',
79100
errors: [{ endColumn: 8, column: 7, messageId: 'noArgs' }],
@@ -215,10 +236,10 @@ ruleTester.run('valid-expect', rule, {
215236
},
216237
],
217238
},
218-
// expect().not.resolves
239+
// expect().resolves.not
219240
{
220241
code:
221-
'test("valid-expect", () => { expect(Promise.resolve(2)).not.resolves.toBeDefined(); });',
242+
'test("valid-expect", () => { expect(Promise.resolve(2)).resolves.not.toBeDefined(); });',
222243
errors: [
223244
{
224245
column: 30,
@@ -241,10 +262,10 @@ ruleTester.run('valid-expect', rule, {
241262
},
242263
],
243264
},
244-
// expect().not.rejects
265+
// expect().rejects.not
245266
{
246267
code:
247-
'test("valid-expect", () => { expect(Promise.resolve(2)).not.rejects.toBeDefined(); });',
268+
'test("valid-expect", () => { expect(Promise.resolve(2)).rejects.not.toBeDefined(); });',
248269
errors: [
249270
{
250271
column: 30,
@@ -269,7 +290,7 @@ ruleTester.run('valid-expect', rule, {
269290
},
270291
{
271292
code:
272-
'test("valid-expect", async () => { expect(Promise.resolve(2)).not.resolves.toBeDefined(); });',
293+
'test("valid-expect", async () => { expect(Promise.resolve(2)).resolves.not.toBeDefined(); });',
273294
errors: [
274295
{
275296
column: 36,
@@ -281,9 +302,9 @@ ruleTester.run('valid-expect', rule, {
281302
},
282303
// alwaysAwait:false, one not awaited
283304
{
284-
code: `test("valid-expect", async () => {
285-
expect(Promise.resolve(2)).not.resolves.toBeDefined();
286-
expect(Promise.resolve(1)).rejects.toBeDefined();
305+
code: `test("valid-expect", async () => {
306+
expect(Promise.resolve(2)).resolves.not.toBeDefined();
307+
expect(Promise.resolve(1)).rejects.toBeDefined();
287308
});`,
288309
errors: [
289310
{
@@ -304,9 +325,9 @@ ruleTester.run('valid-expect', rule, {
304325
},
305326
// alwaysAwait: true, one returned
306327
{
307-
code: `test("valid-expect", async () => {
308-
await expect(Promise.resolve(2)).not.resolves.toBeDefined();
309-
expect(Promise.resolve(1)).rejects.toBeDefined();
328+
code: `test("valid-expect", async () => {
329+
await expect(Promise.resolve(2)).resolves.not.toBeDefined();
330+
expect(Promise.resolve(1)).rejects.toBeDefined();
310331
});`,
311332
errors: [
312333
{
@@ -323,9 +344,9 @@ ruleTester.run('valid-expect', rule, {
323344
*/
324345
// both not awaited
325346
{
326-
code: `test("valid-expect", async () => {
327-
expect(Promise.resolve(2)).not.resolves.toBeDefined();
328-
return expect(Promise.resolve(1)).rejects.toBeDefined();
347+
code: `test("valid-expect", async () => {
348+
expect(Promise.resolve(2)).resolves.not.toBeDefined();
349+
return expect(Promise.resolve(1)).rejects.toBeDefined();
329350
});`,
330351
options: [{ alwaysAwait: true }],
331352
errors: [
@@ -345,9 +366,9 @@ ruleTester.run('valid-expect', rule, {
345366
},
346367
// alwaysAwait:true, one not awaited, one returned
347368
{
348-
code: `test("valid-expect", async () => {
349-
expect(Promise.resolve(2)).not.resolves.toBeDefined();
350-
return expect(Promise.resolve(1)).rejects.toBeDefined();
369+
code: `test("valid-expect", async () => {
370+
expect(Promise.resolve(2)).resolves.not.toBeDefined();
371+
return expect(Promise.resolve(1)).rejects.toBeDefined();
351372
});`,
352373
errors: [
353374
{
@@ -361,9 +382,9 @@ ruleTester.run('valid-expect', rule, {
361382
},
362383
// one not awaited
363384
{
364-
code: `test("valid-expect", async () => {
365-
await expect(Promise.resolve(2)).not.resolves.toBeDefined();
366-
return expect(Promise.resolve(1)).rejects.toBeDefined();
385+
code: `test("valid-expect", async () => {
386+
await expect(Promise.resolve(2)).resolves.not.toBeDefined();
387+
return expect(Promise.resolve(1)).rejects.toBeDefined();
367388
});`,
368389
options: [{ alwaysAwait: true }],
369390
errors: [
@@ -380,8 +401,8 @@ ruleTester.run('valid-expect', rule, {
380401
* Promise.x(expect()) usages
381402
*/
382403
{
383-
code: `test("valid-expect", () => {
384-
Promise.resolve(expect(Promise.resolve(2)).not.resolves.toBeDefined());
404+
code: `test("valid-expect", () => {
405+
Promise.resolve(expect(Promise.resolve(2)).resolves.not.toBeDefined());
385406
});`,
386407
errors: [
387408
{
@@ -394,8 +415,8 @@ ruleTester.run('valid-expect', rule, {
394415
],
395416
},
396417
{
397-
code: `test("valid-expect", () => {
398-
Promise.reject(expect(Promise.resolve(2)).not.resolves.toBeDefined());
418+
code: `test("valid-expect", () => {
419+
Promise.reject(expect(Promise.resolve(2)).resolves.not.toBeDefined());
399420
});`,
400421
errors: [
401422
{
@@ -408,8 +429,8 @@ ruleTester.run('valid-expect', rule, {
408429
],
409430
},
410431
{
411-
code: `test("valid-expect", () => {
412-
Promise.x(expect(Promise.resolve(2)).not.resolves.toBeDefined());
432+
code: `test("valid-expect", () => {
433+
Promise.x(expect(Promise.resolve(2)).resolves.not.toBeDefined());
413434
});`,
414435
errors: [
415436
{
@@ -423,8 +444,8 @@ ruleTester.run('valid-expect', rule, {
423444
},
424445
// alwaysAwait option changes error message
425446
{
426-
code: `test("valid-expect", () => {
427-
Promise.resolve(expect(Promise.resolve(2)).not.resolves.toBeDefined());
447+
code: `test("valid-expect", () => {
448+
Promise.resolve(expect(Promise.resolve(2)).resolves.not.toBeDefined());
428449
});`,
429450
options: [{ alwaysAwait: true }],
430451
errors: [
@@ -438,11 +459,11 @@ ruleTester.run('valid-expect', rule, {
438459
},
439460
// Promise method accepts arrays and returns 1 error
440461
{
441-
code: `test("valid-expect", () => {
462+
code: `test("valid-expect", () => {
442463
Promise.all([
443-
expect(Promise.resolve(2)).not.resolves.toBeDefined(),
444-
expect(Promise.resolve(3)).not.resolves.toBeDefined(),
445-
]);
464+
expect(Promise.resolve(2)).resolves.not.toBeDefined(),
465+
expect(Promise.resolve(3)).resolves.not.toBeDefined(),
466+
]);
446467
});`,
447468
errors: [
448469
{
@@ -457,11 +478,11 @@ ruleTester.run('valid-expect', rule, {
457478
},
458479
// Promise.any([expect1, expect2]) returns one error
459480
{
460-
code: `test("valid-expect", () => {
481+
code: `test("valid-expect", () => {
461482
Promise.x([
462-
expect(Promise.resolve(2)).not.resolves.toBeDefined(),
463-
expect(Promise.resolve(3)).not.resolves.toBeDefined(),
464-
]);
483+
expect(Promise.resolve(2)).resolves.not.toBeDefined(),
484+
expect(Promise.resolve(3)).resolves.not.toBeDefined(),
485+
]);
465486
});`,
466487
errors: [
467488
{
@@ -476,10 +497,10 @@ ruleTester.run('valid-expect', rule, {
476497
},
477498
//
478499
{
479-
code: `test("valid-expect", () => {
500+
code: `test("valid-expect", () => {
480501
const assertions = [
481-
expect(Promise.resolve(2)).not.resolves.toBeDefined(),
482-
expect(Promise.resolve(3)).not.resolves.toBeDefined(),
502+
expect(Promise.resolve(2)).resolves.not.toBeDefined(),
503+
expect(Promise.resolve(3)).resolves.not.toBeDefined(),
483504
]
484505
});`,
485506
errors: [
@@ -505,6 +526,13 @@ ruleTester.run('valid-expect', rule, {
505526
{
506527
code: 'expect(Promise.resolve(2)).resolves.toBe;',
507528
errors: [
529+
{
530+
line: 1,
531+
column: 1,
532+
endLine: 1,
533+
endColumn: 42,
534+
messageId: 'asyncMustBeAwaited',
535+
},
508536
{
509537
column: 37,
510538
endColumn: 41,
@@ -514,7 +542,7 @@ ruleTester.run('valid-expect', rule, {
514542
],
515543
},
516544
{
517-
code: `test("valid-expect", () => {
545+
code: `test("valid-expect", () => {
518546
return expect(functionReturningAPromise()).resolves.toEqual(1).then(() => {
519547
expect(Promise.resolve(2)).resolves.toBe(1);
520548
});
@@ -531,7 +559,7 @@ ruleTester.run('valid-expect', rule, {
531559
],
532560
},
533561
{
534-
code: `test("valid-expect", () => {
562+
code: `test("valid-expect", () => {
535563
return expect(functionReturningAPromise()).resolves.toEqual(1).then(async () => {
536564
await expect(Promise.resolve(2)).resolves.toBe(1);
537565
expect(Promise.resolve(4)).resolves.toBe(4);

src/rules/util.js

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,11 @@ export const expectResolvesCase = node =>
2222
node.parent.parent.type === 'MemberExpression' &&
2323
methodName(node) === 'resolves';
2424

25-
export const expectNotResolvesCase = node =>
26-
expectNotCase(node) &&
27-
node.parent.parent.type === 'MemberExpression' &&
28-
methodName(node.parent) === 'resolves';
29-
3025
export const expectRejectsCase = node =>
3126
expectCase(node) &&
3227
node.parent.parent.type === 'MemberExpression' &&
3328
methodName(node) === 'rejects';
3429

35-
export const expectNotRejectsCase = node =>
36-
expectNotCase(node) &&
37-
node.parent.parent.type === 'MemberExpression' &&
38-
methodName(node.parent) === 'rejects';
39-
4030
export const expectToBeCase = (node, arg) =>
4131
!(
4232
expectNotCase(node) ||

0 commit comments

Comments
 (0)