-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup-jest.ts
60 lines (54 loc) · 2.36 KB
/
setup-jest.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import {ngMocks} from 'ng-mocks'; // eslint-disable-line import/order
// in case of jest
ngMocks.autoSpy('jest');
// In case, if you use @angular/router and Angular 14+.
// You might want to set a mock of DefaultTitleStrategy as TitleStrategy.
// A14 fix: making DefaultTitleStrategy to be a default mock for TitleStrategy
import { DefaultTitleStrategy, TitleStrategy } from '@angular/router'; // eslint-disable-line import/order
import { MockService } from 'ng-mocks'; // eslint-disable-line import/order
ngMocks.defaultMock(TitleStrategy, () => MockService(DefaultTitleStrategy));
// Usually, *ngIf and other declarations from CommonModule aren't expected to be mocked.
// The code below keeps them.
import { CommonModule } from '@angular/common'; // eslint-disable-line import/order
import { ApplicationModule } from '@angular/core'; // eslint-disable-line import/order
import { BrowserModule } from '@angular/platform-browser'; // eslint-disable-line import/order
ngMocks.globalKeep(ApplicationModule, true);
ngMocks.globalKeep(CommonModule, true);
ngMocks.globalKeep(BrowserModule, true);
// auto restore for jasmine and jest <27
// declare const jasmine: any;
// import { MockInstance } from 'ng-mocks'; // eslint-disable-line import/order
// jest.getEnv().addReporter({
// specDone: MockInstance.restore,
// specStarted: MockInstance.remember,
// suiteDone: MockInstance.restore,
// suiteStarted: MockInstance.remember,
// });
// // If you use jest v27+, please add to its config testRunner=jest-jasmine2 for now.
// // If you don't want to rely on jasmine at all, then, please,
// // upvote the issue on github: https://github.com/facebook/jest/issues/11483.
// // Once it has been merged you can use the code below.
// // Also, please consider usage of MockInstance.scope instead.
// import { addEventHandler } from 'jest-circus';
// addEventHandler((event: { name: string }) => {
// switch (event.name) {
// case 'run_describe_start':
// case 'test_start':
// MockInstance.remember();
// break;
// case 'run_describe_finish':
// case 'run_finish':
// MockInstance.restore();
// break;
// default:
// }
// });
// // in case of mocha
// mocha.setup({
// rootHooks: {
// afterAll: MockInstance.restore,
// afterEach: MockInstance.restore,
// beforeAll: MockInstance.remember,
// beforeEach: MockInstance.remember,
// },
// });