Skip to content

Commit d918e19

Browse files
author
James Salas
committed
fix: testbed issues caused by module resolution of different angular versions
1 parent da407f0 commit d918e19

File tree

15 files changed

+262
-300
lines changed

15 files changed

+262
-300
lines changed

jest/tests-setup.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ import 'zone.js/dist/sync-test';
1111
// tslint:disable-next-line:ordered-imports
1212
import 'jest-zone-patch';
1313

14-
import { TestBed } from '@angular/core/testing';
14+
import { getTestBed } from '@angular/core/testing';
1515
import {
1616
BrowserDynamicTestingModule,
1717
platformBrowserDynamicTesting,
1818
} from '@angular/platform-browser-dynamic/testing';
1919
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
2020

21-
TestBed.initTestEnvironment(
21+
getTestBed().initTestEnvironment(
2222
[BrowserDynamicTestingModule, NoopAnimationsModule],
2323
platformBrowserDynamicTesting(),
2424
);

package.json

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@
3434
]
3535
},
3636
"devDependencies": {
37-
"@angular/animations": "6.0.7",
38-
"@angular/common": "6.0.7",
39-
"@angular/compiler": "6.0.7",
40-
"@angular/compiler-cli": "6.0.7",
41-
"@angular/core": "6.0.7",
42-
"@angular/forms": "6.0.7",
43-
"@angular/platform-browser": "6.0.7",
44-
"@angular/platform-browser-dynamic": "6.0.7",
45-
"@angular/router": "6.0.7",
37+
"@angular/animations": "7.1.4",
38+
"@angular/common": "7.1.4",
39+
"@angular/compiler": "7.1.4",
40+
"@angular/compiler-cli": "7.1.4",
41+
"@angular/core": "7.1.4",
42+
"@angular/forms": "7.1.4",
43+
"@angular/platform-browser": "7.1.4",
44+
"@angular/platform-browser-dynamic": "7.1.4",
45+
"@angular/router": "7.1.4",
4646
"@commitlint/cli": "7.0.0",
4747
"@commitlint/config-conventional": "7.0.1",
4848
"@commitlint/prompt-cli": "7.0.0",

packages/example-app/angular.json

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,13 @@
2222
"main": "src/main.ts",
2323
"polyfills": "src/polyfills.ts",
2424
"tsConfig": "src/tsconfig.app.json",
25-
"assets": ["src/favicon.ico", "src/assets"],
26-
"styles": ["src/styles.css"],
25+
"assets": [
26+
"src/favicon.ico",
27+
"src/assets"
28+
],
29+
"styles": [
30+
"src/styles.css"
31+
],
2732
"scripts": []
2833
},
2934
"configurations": {
@@ -84,8 +89,13 @@
8489
"lint": {
8590
"builder": "@angular-devkit/build-angular:tslint",
8691
"options": {
87-
"tsConfig": ["src/tsconfig.app.json", "src/tsconfig.spec.json"],
88-
"exclude": ["**/node_modules/**"]
92+
"tsConfig": [
93+
"src/tsconfig.app.json",
94+
"src/tsconfig.spec.json"
95+
],
96+
"exclude": [
97+
"**/node_modules/**"
98+
]
8999
}
90100
}
91101
}
@@ -111,11 +121,16 @@
111121
"builder": "@angular-devkit/build-angular:tslint",
112122
"options": {
113123
"tsConfig": "e2e/tsconfig.e2e.json",
114-
"exclude": ["**/node_modules/**"]
124+
"exclude": [
125+
"**/node_modules/**"
126+
]
115127
}
116128
}
117129
}
118130
}
119131
},
120-
"defaultProject": "example-app"
121-
}
132+
"defaultProject": "example-app",
133+
"cli": {
134+
"packageManager": "yarn"
135+
}
136+
}

packages/example-app/jest.config.js

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,33 @@
1+
'use strict';
2+
3+
// Jest JUnit Reporter config
4+
process.env.JEST_JUNIT_OUTPUT = './coverage/junit.xml';
5+
16
module.exports = {
2-
preset: 'jest-preset-angular',
3-
setupTestFrameworkScriptFile: '<rootDir>/src/setupJest.ts',
4-
testMatch: ['**/*.spec.ts'],
7+
globals: {
8+
__TRANSFORM_HTML__: true,
9+
},
10+
transform: {
11+
'^.+\\.(ts|js|html)$':
12+
'<rootDir>/node_modules/jest-preset-angular/preprocessor.js',
13+
},
14+
testMatch: ['**/*.spec.{ts,js}'],
15+
moduleFileExtensions: ['ts', 'js', 'html', 'json'],
16+
setupTestFrameworkScriptFile: '<rootDir>/src/test.ts',
17+
snapshotSerializers: [
18+
'<rootDir>/node_modules/jest-preset-angular/AngularSnapshotSerializer.js',
19+
'<rootDir>/node_modules/jest-preset-angular/HTMLCommentSerializer.js',
20+
],
21+
moduleNameMapper: {
22+
'\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$':
23+
'<rootDir>/src/testing/file.mock.ts',
24+
},
25+
modulePathIgnorePatterns: ['dist'],
26+
reporters: ['default', 'jest-junit'],
27+
collectCoverageFrom: [
28+
'**/packages/**/*.{ts,js}',
29+
'!**/node_modules/**',
30+
'!**/dist/**',
31+
],
32+
coverageReporters: ['lcov', 'text-summary'],
533
};

packages/example-app/package.json

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@
1818
"@angular-redux/form": "latest",
1919
"@angular-redux/router": "latest",
2020
"@angular-redux/store": "latest",
21-
"@angular/common": "^7.1.0",
22-
"@angular/compiler": "^7.1.0",
23-
"@angular/core": "^7.1.0",
24-
"@angular/forms": "^7.1.0",
25-
"@angular/http": "^7.1.0",
26-
"@angular/platform-browser": "^7.1.0",
27-
"@angular/platform-browser-dynamic": "^7.1.0",
28-
"@angular/router": "^7.1.0",
21+
"@angular/animations": "7.1.4",
22+
"@angular/common": "^7.1.4",
23+
"@angular/compiler": "^7.1.4",
24+
"@angular/core": "^7.1.4",
25+
"@angular/forms": "^7.1.4",
26+
"@angular/http": "^7.1.4",
27+
"@angular/platform-browser": "^7.1.4",
28+
"@angular/platform-browser-dynamic": "^7.1.4",
29+
"@angular/router": "^7.1.4",
2930
"core-js": "^2.4.1",
3031
"flux-standard-action": "^2.0.3",
3132
"ramda": "^0.26.0",
@@ -45,6 +46,7 @@
4546
"@types/ramda": "^0.24.18",
4647
"@types/redux-logger": "^3.0.0",
4748
"jest": "^23.6.0",
49+
"jest-junit": "5.2.0",
4850
"jest-preset-angular": "6.0.2",
4951
"protractor": "^5.4.1",
5052
"ts-node": "^7.0.1",

packages/example-app/src/app/animals/animal-list/component.spec.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@ import { By } from '@angular/platform-browser';
44
import { CoreModule } from '../../core/module';
55
import { AnimalType } from '../model';
66
import { AnimalListComponent } from './component';
7-
import {
8-
BrowserDynamicTestingModule,
9-
platformBrowserDynamicTesting,
10-
} from '@angular/platform-browser-dynamic/testing';
117

128
@Component({ selector: 'zoo-animal', template: '' })
139
class MockAnimalComponent {
@@ -17,12 +13,6 @@ class MockAnimalComponent {
1713

1814
describe('AnimalListComponent', () => {
1915
beforeEach(async(() => {
20-
TestBed.resetTestEnvironment();
21-
TestBed.initTestEnvironment(
22-
BrowserDynamicTestingModule,
23-
platformBrowserDynamicTesting(),
24-
);
25-
2616
TestBed.configureTestingModule({
2717
declarations: [AnimalListComponent, MockAnimalComponent],
2818
imports: [CoreModule],

packages/example-app/src/app/animals/animal/component.spec.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import { async, TestBed } from '@angular/core/testing';
77
import { AnyAction, Reducer } from 'redux';
88
import { CoreModule } from '../../core/module';
99
import { AnimalComponent } from './component';
10-
import { BrowserDynamicTestingModule, platformBrowserDynamicTesting } from '@angular/platform-browser-dynamic/testing';
1110

1211
type ConfigureSubStoreFn = (
1312
basePath: (string | number)[],
@@ -20,12 +19,6 @@ describe('AnimalComponent', () => {
2019
let spyConfigureSubStore: ConfigureSubStoreFn;
2120

2221
beforeEach(async(() => {
23-
TestBed.resetTestEnvironment();
24-
TestBed.initTestEnvironment(
25-
BrowserDynamicTestingModule,
26-
platformBrowserDynamicTesting(),
27-
);
28-
2922
spyConfigureSubStore = spyOn(
3023
MockNgRedux.mockInstance!,
3124
'configureSubStore',

packages/example-app/src/app/component.spec.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,6 @@ import {
88

99
describe('AppComponent', () => {
1010
beforeEach(async(() => {
11-
TestBed.resetTestEnvironment();
12-
TestBed.initTestEnvironment(
13-
BrowserDynamicTestingModule,
14-
platformBrowserDynamicTesting(),
15-
);
16-
1711
TestBed.configureTestingModule({
1812
declarations: [AppComponent],
1913
imports: [RouterTestingModule],

packages/example-app/src/app/elephants/page.spec.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import { toArray } from 'rxjs/operators';
1212
import { AnimalAPIActions } from '../animals/api/actions';
1313
import { ANIMAL_TYPES } from '../animals/model';
1414
import { ElephantPageComponent } from './page';
15-
import { BrowserDynamicTestingModule, platformBrowserDynamicTesting } from '@angular/platform-browser-dynamic/testing';
1615

1716
@Component({
1817
selector: 'zoo-animal-list',
@@ -27,12 +26,6 @@ class MockAnimalListComponent {
2726

2827
describe('Elephant Page Container', () => {
2928
beforeEach(() => {
30-
TestBed.resetTestEnvironment();
31-
TestBed.initTestEnvironment(
32-
BrowserDynamicTestingModule,
33-
platformBrowserDynamicTesting(),
34-
);
35-
3629
TestBed.configureTestingModule({
3730
declarations: [ElephantPageComponent, MockAnimalListComponent],
3831
imports: [NgReduxTestingModule],

packages/example-app/src/app/feedback/page.spec.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,10 @@ import { TestBed } from '@angular/core/testing';
66

77
import { toArray } from 'rxjs/operators';
88
import { FeedbackFormComponent } from './page';
9-
import { BrowserDynamicTestingModule, platformBrowserDynamicTesting } from '@angular/platform-browser-dynamic/testing';
9+
1010

1111
describe('Feedback Form Component', () => {
1212
beforeEach(() => {
13-
TestBed.resetTestEnvironment();
14-
TestBed.initTestEnvironment(
15-
BrowserDynamicTestingModule,
16-
platformBrowserDynamicTesting(),
17-
);
18-
1913
TestBed.configureTestingModule({
2014
declarations: [FeedbackFormComponent],
2115
imports: [NgReduxTestingModule],

packages/example-app/src/app/store/module.spec.ts

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,39 +6,27 @@ import {
66
import { async, getTestBed, TestBed } from '@angular/core/testing';
77
import { RootEpics } from './epics';
88
import { StoreModule } from './module';
9-
import { BrowserDynamicTestingModule, platformBrowserDynamicTesting } from '@angular/platform-browser-dynamic/testing';
109

1110
describe('Store Module', () => {
1211
let mockNgRedux: NgRedux<any>;
1312
let devTools: DevToolsExtension;
1413
let mockEpics: Partial<RootEpics>;
1514

1615
beforeEach(async(() => {
17-
TestBed.resetTestEnvironment();
18-
TestBed.initTestEnvironment(
19-
BrowserDynamicTestingModule,
20-
platformBrowserDynamicTesting(),
21-
);
22-
2316
TestBed.configureTestingModule({
2417
imports: [NgReduxTestingModule],
2518
})
26-
.compileComponents()
27-
.then(() => {
28-
const testbed = getTestBed();
29-
30-
mockEpics = {
31-
createEpics() {
32-
return [];
33-
},
34-
};
35-
36-
devTools = testbed.get(DevToolsExtension);
37-
mockNgRedux = MockNgRedux.getInstance();
38-
});
3919
}));
4020

4121
it('should configure the store when the module is loaded', async(() => {
22+
mockEpics = {
23+
createEpics() {
24+
return [];
25+
},
26+
};
27+
devTools = TestBed.get(DevToolsExtension);
28+
mockNgRedux = MockNgRedux.getInstance();
29+
4230
const configureSpy = spyOn(MockNgRedux.getInstance(), 'configureStore');
4331
const module = new StoreModule(
4432
mockNgRedux,

packages/example-app/src/setupJest.ts

Lines changed: 0 additions & 2 deletions
This file was deleted.

packages/example-app/src/test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// tslint:disable:no-implicit-dependencies
2+
import 'core-js/es6/reflect';
3+
import 'core-js/es7/reflect';
4+
import 'zone.js';
5+
import 'zone.js/dist/async-test';
6+
import 'zone.js/dist/fake-async-test';
7+
import 'zone.js/dist/proxy';
8+
import 'zone.js/dist/sync-test';
9+
// This must be loaded in after ZoneJS
10+
// tslint:disable-next-line:ordered-imports
11+
import 'jest-zone-patch';
12+
13+
import { getTestBed } from '@angular/core/testing';
14+
import {
15+
BrowserDynamicTestingModule,
16+
platformBrowserDynamicTesting,
17+
} from '@angular/platform-browser-dynamic/testing';
18+
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
19+
20+
getTestBed().initTestEnvironment(
21+
[BrowserDynamicTestingModule, NoopAnimationsModule],
22+
platformBrowserDynamicTesting(),
23+
);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = 'test-file-stub';

0 commit comments

Comments
 (0)