From fbe1f8b4cb7cfca5aa6a6a1cd36600e77f52cf68 Mon Sep 17 00:00:00 2001 From: Winnie Hellmann <winnie@gitlab.com> Date: Tue, 26 Mar 2019 20:49:05 +0100 Subject: [PATCH] Provide custom Jest environment with mocked console --- jest.config.js | 1 + spec/frontend/environment.js | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 spec/frontend/environment.js diff --git a/jest.config.js b/jest.config.js index 1f6e04390ae30..cd0d311779db5 100644 --- a/jest.config.js +++ b/jest.config.js @@ -37,4 +37,5 @@ module.exports = { }, transformIgnorePatterns: ['node_modules/(?!(@gitlab/ui)/)'], timers: 'fake', + testEnvironment: '<rootDir>/spec/frontend/environment.js', }; diff --git a/spec/frontend/environment.js b/spec/frontend/environment.js new file mode 100644 index 0000000000000..cb128c7d88098 --- /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; -- GitLab