|
1 | 1 | import { TSESLint } from '@typescript-eslint/experimental-utils';
|
| 2 | +import dedent from 'dedent'; |
2 | 3 | import resolveFrom from 'resolve-from';
|
3 | 4 | import rule from '../no-conditional-expect';
|
4 | 5 |
|
@@ -584,3 +585,108 @@ ruleTester.run('catch conditions', rule, {
|
584 | 585 | },
|
585 | 586 | ],
|
586 | 587 | });
|
| 588 | + |
| 589 | +ruleTester.run('promises', rule, { |
| 590 | + valid: [ |
| 591 | + ` |
| 592 | + it('works', async () => { |
| 593 | + try { |
| 594 | + await Promise.resolve().then(() => { |
| 595 | + throw new Error('oh noes!'); |
| 596 | + }); |
| 597 | + } catch { |
| 598 | + // ignore errors |
| 599 | + } finally { |
| 600 | + expect(something).toHaveBeenCalled(); |
| 601 | + } |
| 602 | + }); |
| 603 | + `, |
| 604 | + ` |
| 605 | + it('works', async () => { |
| 606 | + await doSomething().catch(error => error); |
| 607 | +
|
| 608 | + expect(error).toBeInstanceOf(Error); |
| 609 | + }); |
| 610 | + `, |
| 611 | + ` |
| 612 | + it('works', async () => { |
| 613 | + try { |
| 614 | + await Promise.resolve().then(() => { |
| 615 | + throw new Error('oh noes!'); |
| 616 | + }); |
| 617 | + } catch { |
| 618 | + // ignore errors |
| 619 | + } |
| 620 | +
|
| 621 | + expect(something).toHaveBeenCalled(); |
| 622 | + }); |
| 623 | + `, |
| 624 | + ], |
| 625 | + invalid: [ |
| 626 | + { |
| 627 | + code: dedent` |
| 628 | + it('works', async () => { |
| 629 | + await Promise.resolve() |
| 630 | + .then(() => { throw new Error('oh noes!'); }) |
| 631 | + .catch(error => expect(error).toBeInstanceOf(Error)); |
| 632 | + }); |
| 633 | + `, |
| 634 | + errors: [{ messageId: 'conditionalExpect' }], |
| 635 | + }, |
| 636 | + { |
| 637 | + code: dedent` |
| 638 | + it('works', async () => { |
| 639 | + await Promise.resolve() |
| 640 | + .then(() => { throw new Error('oh noes!'); }) |
| 641 | + .catch(error => expect(error).toBeInstanceOf(Error)) |
| 642 | + .then(() => { throw new Error('oh noes!'); }) |
| 643 | + .catch(error => expect(error).toBeInstanceOf(Error)) |
| 644 | + .then(() => { throw new Error('oh noes!'); }) |
| 645 | + .catch(error => expect(error).toBeInstanceOf(Error)); |
| 646 | + }); |
| 647 | + `, |
| 648 | + errors: [{ messageId: 'conditionalExpect' }], |
| 649 | + }, |
| 650 | + { |
| 651 | + code: dedent` |
| 652 | + it('works', async () => { |
| 653 | + await Promise.resolve() |
| 654 | + .catch(error => expect(error).toBeInstanceOf(Error)) |
| 655 | + .catch(error => expect(error).toBeInstanceOf(Error)) |
| 656 | + .catch(error => expect(error).toBeInstanceOf(Error)); |
| 657 | + }); |
| 658 | + `, |
| 659 | + errors: [{ messageId: 'conditionalExpect' }], |
| 660 | + }, |
| 661 | + { |
| 662 | + code: dedent` |
| 663 | + it('works', async () => { |
| 664 | + await Promise.resolve() |
| 665 | + .catch(error => expect(error).toBeInstanceOf(Error)) |
| 666 | + .then(() => { throw new Error('oh noes!'); }) |
| 667 | + .then(() => { throw new Error('oh noes!'); }) |
| 668 | + .then(() => { throw new Error('oh noes!'); }); |
| 669 | + }); |
| 670 | + `, |
| 671 | + errors: [{ messageId: 'conditionalExpect' }], |
| 672 | + }, |
| 673 | + { |
| 674 | + code: dedent` |
| 675 | + it('works', async () => { |
| 676 | + await somePromise |
| 677 | + .then(() => { throw new Error('oh noes!'); }) |
| 678 | + .catch(error => expect(error).toBeInstanceOf(Error)); |
| 679 | + }); |
| 680 | + `, |
| 681 | + errors: [{ messageId: 'conditionalExpect' }], |
| 682 | + }, |
| 683 | + { |
| 684 | + code: dedent` |
| 685 | + it('works', async () => { |
| 686 | + await somePromise.catch(error => expect(error).toBeInstanceOf(Error)); |
| 687 | + }); |
| 688 | + `, |
| 689 | + errors: [{ messageId: 'conditionalExpect' }], |
| 690 | + }, |
| 691 | + ], |
| 692 | +}); |
0 commit comments