From 9add62bfd6934bc27160fa36f85ec7c085ffabed Mon Sep 17 00:00:00 2001 From: David Sanders Date: Wed, 9 Aug 2023 02:01:52 -0700 Subject: [PATCH] test: helper to expect deprecation warnings (#39405) --- spec/lib/deprecate-helpers.ts | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 spec/lib/deprecate-helpers.ts diff --git a/spec/lib/deprecate-helpers.ts b/spec/lib/deprecate-helpers.ts new file mode 100644 index 0000000000000..ed59c921844d6 --- /dev/null +++ b/spec/lib/deprecate-helpers.ts @@ -0,0 +1,26 @@ +import { expect } from 'chai'; + +export async function expectDeprecationMessages (func: () => any, ...expected: string[]) { + const messages: string[] = []; + + const originalWarn = console.warn; + console.warn = (message) => { + messages.push(message); + }; + + const warningListener = (error: Error) => { + messages.push(error.message); + }; + + process.on('warning', warningListener); + + try { + return await func(); + } finally { + // process.emitWarning seems to need us to wait a tick + await new Promise(process.nextTick); + console.warn = originalWarn; + process.off('warning', warningListener); + expect(messages).to.deep.equal(expected); + } +}