From 1290c8a48087e6aa9c9df1280616a6f3ede03c1b Mon Sep 17 00:00:00 2001 From: MG Date: Sat, 5 Jun 2021 10:26:51 +0200 Subject: [PATCH] fix(jest): a fix in advance to listen to jest hooks #610 --- .../ng-mocks/src/lib/common/ng-mocks-stack.ts | 54 +++++++++++-------- 1 file changed, 32 insertions(+), 22 deletions(-) diff --git a/libs/ng-mocks/src/lib/common/ng-mocks-stack.ts b/libs/ng-mocks/src/lib/common/ng-mocks-stack.ts index 1218f5f775..464b4274b8 100644 --- a/libs/ng-mocks/src/lib/common/ng-mocks-stack.ts +++ b/libs/ng-mocks/src/lib/common/ng-mocks-stack.ts @@ -1,11 +1,16 @@ import ngMocksUniverse from './ng-mocks-universe'; +// TODO remove the check once Jest has a solution https://github.com/facebook/jest/issues/11483 +let checkJestCircusEventHandler = true; let addJestCircusEventHandler: undefined | ((event: { name: string }) => void); // istanbul ignore next try { // tslint:disable-next-line no-require-imports no-var-requires no-implicit-dependencies const jestCircus: any = require('jest-circus/build/state'); addJestCircusEventHandler = jestCircus.addEventHandler; + if (jestCircus.removeEventHandler) { + checkJestCircusEventHandler = false; + } } catch { // nothing to do } @@ -96,34 +101,39 @@ const installJasmineReporter = () => { } }; +// istanbul ignore next +const jestCircusHandler = (event: { name: string }) => { + switch (event.name) { + case 'run_start': + stackPush('root'); + break; + case 'run_describe_start': + stackPush('suite'); + break; + case 'test_start': + stackPush('test'); + break; + case 'test_done': + case 'run_describe_finish': + case 'run_finish': + stackPop(); + break; + default: + // nothing to do + } +}; + // istanbul ignore next const installJestCircus = () => { if (!addJestCircusEventHandler) { return false; } - afterEach(messageCoreChecker); // TODO remove once Jest has a solution - - addJestCircusEventHandler((event: { name: string }) => { - switch (event.name) { - case 'run_start': - stackPush('root'); - break; - case 'run_describe_start': - stackPush('suite'); - break; - case 'test_start': - stackPush('test'); - break; - case 'test_done': - case 'run_describe_finish': - case 'run_finish': - stackPop(); - break; - default: - // nothing to do - } - }); + if (checkJestCircusEventHandler) { + afterEach(messageCoreChecker); + } + + addJestCircusEventHandler(jestCircusHandler); return true; };