From fbe1f8b4cb7cfca5aa6a6a1cd36600e77f52cf68 Mon Sep 17 00:00:00 2001 From: Winnie Hellmann Date: Tue, 26 Mar 2019 20:49:05 +0100 Subject: Provide custom Jest environment with mocked console --- spec/frontend/environment.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 spec/frontend/environment.js (limited to 'spec') diff --git a/spec/frontend/environment.js b/spec/frontend/environment.js new file mode 100644 index 00000000000..cb128c7d880 --- /dev/null +++ b/spec/frontend/environment.js @@ -0,0 +1,27 @@ +/* eslint-disable import/no-commonjs */ + +const { ErrorWithStack } = require('jest-util'); +const JSDOMEnvironment = require('jest-environment-jsdom'); + +class CustomEnvironment extends JSDOMEnvironment { + constructor(config, context) { + super(config, context); + Object.assign(context.console, { + error(...args) { + throw new ErrorWithStack( + `Unexpected call of console.error() with:\n\n${args.join(', ')}`, + this.error, + ); + }, + + warn(...args) { + throw new ErrorWithStack( + `Unexpected call of console.warn() with:\n\n${args.join(', ')}`, + this.warn, + ); + }, + }); + } +} + +module.exports = CustomEnvironment; -- cgit v1.2.3