|
9 | 9 | // this spec will not be integrated with Travis CI, because I don't |
10 | 10 | // want to add bluebird into devDependencies, you can run this spec |
11 | 11 | // on your local environment |
| 12 | +process.on('unhandledRejection',(reason,p)=>{ |
| 13 | +console.log('Unhandled Rejection at:',p,'reason:',reason); |
| 14 | +// application specific logging, throwing an error, or other logic here |
| 15 | +}); |
12 | 16 |
|
13 | 17 | describe('bluebird promise',()=>{ |
14 | 18 | letBluebirdPromise: any; |
@@ -636,4 +640,156 @@ describe('bluebird promise', () => { |
636 | 640 | }); |
637 | 641 | }); |
638 | 642 | }); |
| 643 | + |
| 644 | +it('should be able to chain promise',(done: DoneFn)=>{ |
| 645 | +Zone.current.fork({name: 'zone_A'}).run(()=>{ |
| 646 | +newBluebirdPromise((resolve: any,reject: any)=>{ |
| 647 | +expect(Zone.current.name).toEqual('zone_A'); |
| 648 | +resolve(1); |
| 649 | +}) |
| 650 | +.then((r: any)=>{ |
| 651 | +expect(r).toBe(1); |
| 652 | +expect(Zone.current.name).toEqual('zone_A'); |
| 653 | +returnPromise.resolve(2); |
| 654 | +}) |
| 655 | +.then((r: any)=>{ |
| 656 | +expect(r).toBe(2); |
| 657 | +expect(Zone.current.name).toEqual('zone_A'); |
| 658 | +}); |
| 659 | +}); |
| 660 | +Zone.current.fork({name: 'zone_B'}).run(()=>{ |
| 661 | +newBluebirdPromise((resolve: any,reject: any)=>{ |
| 662 | +expect(Zone.current.name).toEqual('zone_B'); |
| 663 | +reject(1); |
| 664 | +}) |
| 665 | +.then( |
| 666 | +()=>{ |
| 667 | +fail('should not be here.'); |
| 668 | +}, |
| 669 | +(r: any)=>{ |
| 670 | +expect(r).toBe(1); |
| 671 | +expect(Zone.current.name).toEqual('zone_B'); |
| 672 | +returnPromise.resolve(2); |
| 673 | +}) |
| 674 | +.then((r: any)=>{ |
| 675 | +expect(r).toBe(2); |
| 676 | +expect(Zone.current.name).toEqual('zone_B'); |
| 677 | +done(); |
| 678 | +}); |
| 679 | +}); |
| 680 | +}); |
| 681 | + |
| 682 | +it('should catch rejected chained bluebird promise',(done: DoneFn)=>{ |
| 683 | +constlogs: string[]=[]; |
| 684 | +constzone=Zone.current.fork({ |
| 685 | +name: 'testErrorHandling', |
| 686 | +onHandleError: function(){ |
| 687 | +// should not get here |
| 688 | +logs.push('onHandleError'); |
| 689 | +returntrue; |
| 690 | +} |
| 691 | +}); |
| 692 | + |
| 693 | +zone.runGuarded(()=>{ |
| 694 | +returnBluebirdPromise.resolve() |
| 695 | +.then(()=>{ |
| 696 | +thrownewError('test error'); |
| 697 | +}) |
| 698 | +.catch(()=>{ |
| 699 | +expect(logs).toEqual([]); |
| 700 | +done(); |
| 701 | +}); |
| 702 | +}); |
| 703 | +}); |
| 704 | + |
| 705 | +it('should catch rejected chained global promise',(done: DoneFn)=>{ |
| 706 | +constlogs: string[]=[]; |
| 707 | +constzone=Zone.current.fork({ |
| 708 | +name: 'testErrorHandling', |
| 709 | +onHandleError: function(){ |
| 710 | +// should not get here |
| 711 | +logs.push('onHandleError'); |
| 712 | +returntrue; |
| 713 | +} |
| 714 | +}); |
| 715 | + |
| 716 | +zone.runGuarded(()=>{ |
| 717 | +returnPromise.resolve() |
| 718 | +.then(()=>{ |
| 719 | +thrownewError('test error'); |
| 720 | +}) |
| 721 | +.catch(()=>{ |
| 722 | +expect(logs).toEqual([]); |
| 723 | +done(); |
| 724 | +}); |
| 725 | +}); |
| 726 | +}); |
| 727 | + |
| 728 | +it('should catch rejected bluebird promise',(done: DoneFn)=>{ |
| 729 | +constlogs: string[]=[]; |
| 730 | +constzone=Zone.current.fork({ |
| 731 | +name: 'testErrorHandling', |
| 732 | +onHandleError: function(){ |
| 733 | +// should not get here |
| 734 | +logs.push('onHandleError'); |
| 735 | +returntrue; |
| 736 | +} |
| 737 | +}); |
| 738 | + |
| 739 | +zone.runGuarded(()=>{ |
| 740 | +returnBluebirdPromise.reject().catch(()=>{ |
| 741 | +expect(logs).toEqual([]); |
| 742 | +done(); |
| 743 | +}); |
| 744 | +}); |
| 745 | +}); |
| 746 | + |
| 747 | +it('should catch rejected global promise',(done: DoneFn)=>{ |
| 748 | +constlogs: string[]=[]; |
| 749 | +constzone=Zone.current.fork({ |
| 750 | +name: 'testErrorHandling', |
| 751 | +onHandleError: function(){ |
| 752 | +// should not get here |
| 753 | +logs.push('onHandleError'); |
| 754 | +returntrue; |
| 755 | +} |
| 756 | +}); |
| 757 | + |
| 758 | +zone.runGuarded(()=>{ |
| 759 | +returnPromise.reject(newError('reject')).catch(()=>{ |
| 760 | +expect(logs).toEqual([]); |
| 761 | +done(); |
| 762 | +}); |
| 763 | +}); |
| 764 | +}); |
| 765 | + |
| 766 | +it('should trigger onHandleError when unhandledRejection',(done: DoneFn)=>{ |
| 767 | +constzone=Zone.current.fork({ |
| 768 | +name: 'testErrorHandling', |
| 769 | +onHandleError: function(){ |
| 770 | +setTimeout(done,100); |
| 771 | +returntrue; |
| 772 | +} |
| 773 | +}); |
| 774 | + |
| 775 | +zone.runGuarded(()=>{ |
| 776 | +returnPromise.reject(newError('reject')); |
| 777 | +}); |
| 778 | +}); |
| 779 | + |
| 780 | +it('should trigger onHandleError when unhandledRejection in chained Promise',(done: DoneFn)=>{ |
| 781 | +constzone=Zone.current.fork({ |
| 782 | +name: 'testErrorHandling', |
| 783 | +onHandleError: function(){ |
| 784 | +setTimeout(done,100); |
| 785 | +returntrue; |
| 786 | +} |
| 787 | +}); |
| 788 | + |
| 789 | +zone.runGuarded(()=>{ |
| 790 | +returnPromise.resolve().then(()=>{ |
| 791 | +thrownewError('test'); |
| 792 | +}); |
| 793 | +}); |
| 794 | +}); |
639 | 795 | }); |
0 commit comments