Reputation: 2177
I have a file: src/app/shared/rxjs-imports.ts
that adds my rxjs operators. For example:
import 'rxjs/add/observable/from';
import 'rxjs/add/operator/map';
The rxjs-imports.ts
file gets imported in my app.module.ts
file so that my application has a single import of necessary rxjs operators during run time. But how do I import that same file at the right time before my jasmine unit tests start so that I don't get the various missing operator errors during testing that complain that map
and from
(etc) can't be found?
Upvotes: 1
Views: 610
Reputation: 2177
If you look at src/test.ts
:
import 'zone.js/dist/zone-testing';
import {getTestBed} from '@angular/core/testing';
import {
BrowserDynamicTestingModule,
platformBrowserDynamicTesting
} from '@angular/platform-browser-dynamic/testing';
declare const require: any;
// First, initialize the Angular testing environment.
getTestBed().initTestEnvironment(
BrowserDynamicTestingModule,
platformBrowserDynamicTesting()
);
// Then we find all the tests.
const context = require.context('./', true, /\.spec\.ts$/);
// And load the modules.
context.keys().map(context);
You'll notice the line that imports all spec files: require.context('./', true, /\.spec\.ts$/);
.
If you create an adjacent test.spec.ts
file, it will be loaded last, but its imports and definitions will take place before any of your tests run. Problem solved.
As a bonus, it's also a convenient location to place hooks you want to load before starting your first test:
// Put anything you wanted loaded before your tests start here
import './app/shared/rxjs-imports';
beforeAll(() => {
});
afterAll(() => {
});
beforeEach(() => {
});
afterEach(() => {
});
Because all spec
files are loaded, even if you tell jasmine to focus on a single test, and because you are not scoping anything in this file within a jasmine describe
function, any before/after hooks you place here will be applied to all your tests.
Upvotes: 2