|
| 1 | +/** |
| 2 | + * Copyright (c) Microsoft Corporation. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { spawnSync } from 'child_process'; |
| 18 | +import vm from 'vm'; |
| 19 | +import { test, expect } from '@playwright/test'; |
| 20 | +import { EventEmitter } from './utils'; |
| 21 | + |
| 22 | +test('EE-057 should report synchronous listener errors and continue dispatch', { |
| 23 | + annotation: { type: 'issue', description: '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/microsoft/playwright/issues/43770' }, |
| 24 | +}, () => { |
| 25 | + const ee = new EventEmitter(); |
| 26 | + const error = new Error('listener failed'); |
| 27 | + ee.on('throw', () => { throw error; }); |
| 28 | + expect(() => ee.emit('throw')).toThrow(error); |
| 29 | + |
| 30 | + const errors = []; |
| 31 | + ee.on('error', error => errors.push(error)); |
| 32 | + ee.on('throw', () => errors.push('next listener')); |
| 33 | + expect(ee.emit('throw')).toBe(true); |
| 34 | + expect(errors).toEqual([error, 'next listener']); |
| 35 | + |
| 36 | + ee.removeAllListeners('error'); |
| 37 | + expect(() => ee.emit('throw')).toThrow(error); |
| 38 | + ee.on('error', () => { throw new Error('error listener failed'); }); |
| 39 | + expect(() => ee.emit('throw')).toThrow('error listener failed'); |
| 40 | +}); |
| 41 | + |
| 42 | +test('EE-058 should capture rejections from promises and thenables', async () => { |
| 43 | + const ee = new EventEmitter(); |
| 44 | + const error = new Error('listener failed'); |
| 45 | + const event = Symbol('event'); |
| 46 | + const errors = []; |
| 47 | + ee.on('error', function(this: EventEmitter, value) { |
| 48 | + expect(this).toBe(ee); |
| 49 | + errors.push(value); |
| 50 | + }); |
| 51 | + ee.once(event, async function(this: EventEmitter, ...args) { |
| 52 | + expect(this).toBe(ee); |
| 53 | + expect(args).toEqual([1, 2]); |
| 54 | + throw error; |
| 55 | + }); |
| 56 | + ee.on(event, () => errors.push('next listener')); |
| 57 | + expect(ee.emit(event, 1, 2)).toBe(true); |
| 58 | + expect(errors).toEqual(['next listener']); |
| 59 | + await expect.poll(() => errors).toEqual(['next listener', error]); |
| 60 | + ee.emit(event); |
| 61 | + expect(errors).toEqual(['next listener', error, 'next listener']); |
| 62 | + |
| 63 | + ee.on('vm', () => vm.runInNewContext('Promise.reject("vm rejection")')); |
| 64 | + ee.emit('vm'); |
| 65 | + await expect.poll(() => errors.at(-1)).toBe('vm rejection'); |
| 66 | + |
| 67 | + const thenable = { |
| 68 | + then(resolve, reject) { |
| 69 | + expect(this).toBe(thenable); |
| 70 | + reject(error); |
| 71 | + }, |
| 72 | + }; |
| 73 | + ee.on('thenable', () => thenable); |
| 74 | + ee.emit('thenable'); |
| 75 | + await expect.poll(() => errors.at(-1)).toBe(error); |
| 76 | +}); |
| 77 | + |
| 78 | +test('EE-059 should read then only once and handle errors while inspecting thenables', { |
| 79 | + annotation: { type: 'issue', description: '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/microsoft/playwright/issues/43770' }, |
| 80 | +}, async () => { |
| 81 | + const ee = new EventEmitter(); |
| 82 | + const errors = []; |
| 83 | + ee.on('error', error => errors.push(error)); |
| 84 | + const error = new Error('then getter failed'); |
| 85 | + ee.on('getter', () => ({ |
| 86 | + get then() { |
| 87 | + throw error; |
| 88 | + }, |
| 89 | + })); |
| 90 | + expect(ee.emit('getter')).toBe(true); |
| 91 | + expect(errors).toEqual([error]); |
| 92 | + |
| 93 | + let reads = 0; |
| 94 | + ee.on('thenable', () => ({ |
| 95 | + get then() { |
| 96 | + expect(++reads).toBe(1); |
| 97 | + return () => { throw new Error('then call failed'); }; |
| 98 | + }, |
| 99 | + })); |
| 100 | + ee.emit('thenable'); |
| 101 | + await expect.poll(() => errors.length).toBe(2); |
| 102 | + expect(errors[1].message).toBe('then call failed'); |
| 103 | + expect(reads).toBe(1); |
| 104 | +}); |
| 105 | + |
| 106 | +test('EE-060 should not swallow unhandled rejections or recurse on error listener failures', () => { |
| 107 | + for (const errorListener of [ |
| 108 | + '', |
| 109 | + `ee.on('error', () => { throw new Error('error listener failed'); });`, |
| 110 | + `ee.on('error', async () => { throw new Error('error listener failed'); });`, |
| 111 | + ]) { |
| 112 | + const result = spawnSync(process.execPath, ['--unhandled-rejections=strict', '-e', ` |
| 113 | + const { clientEventEmitter: EventEmitter } = require(${JSON.stringify(require.resolve('../../../packages/playwright-core/lib/coreBundle'))}); |
| 114 | + const ee = new EventEmitter(); |
| 115 | + ${errorListener} |
| 116 | + ee.on('event', async () => { throw new Error('listener failed'); }); |
| 117 | + ee.emit('event'); |
| 118 | + `], { encoding: 'utf-8', timeout: 10000 }); |
| 119 | + expect(result.error).toBeUndefined(); |
| 120 | + expect(result.status).toBe(1); |
| 121 | + expect(result.stderr).toContain(errorListener ? 'Error: error listener failed' : 'Error: listener failed'); |
| 122 | + } |
| 123 | +}); |
0 commit comments