Skip to content

Commit

Permalink
Merge pull request #667 from satanTime/issues/610-jest
Browse files Browse the repository at this point in the history
fix(jest): a fix in advance to listen to jest hooks #610
  • Loading branch information
satanTime authored Jun 5, 2021
2 parents 4758dd7 + 1290c8a commit e350d81
Showing 1 changed file with 32 additions and 22 deletions.
54 changes: 32 additions & 22 deletions libs/ng-mocks/src/lib/common/ng-mocks-stack.ts
Original file line number Diff line number Diff line change
@@ -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
}
Expand Down Expand Up @@ -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;
};
Expand Down

0 comments on commit e350d81

Please # to comment.