Skip to content

Commit

Permalink
fix(build): improvments in unit tests
Browse files Browse the repository at this point in the history
* separate karma shim & System config
  • Loading branch information
pimenovoleg committed Jul 11, 2018
1 parent 523cbc7 commit 38c1098
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 101 deletions.
72 changes: 72 additions & 0 deletions tests/karma-system-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Configure the base path and map the different node packages.
System.config({
baseURL: '/base',
paths: {
'node:*': 'node_modules/*'
},
map: {
'rxjs': 'node:rxjs',
'main': 'main.js',
'tslib': 'node:tslib/tslib.js',

// Angular specific mappings.
'@angular/core': 'node:@angular/core/bundles/core.umd.js',
'@angular/core/testing': 'node:@angular/core/bundles/core-testing.umd.js',
'@angular/common': 'node:@angular/common/bundles/common.umd.js',
'@angular/common/testing': 'node:@angular/common/bundles/common-testing.umd.js',
'@angular/common/http': 'node:@angular/common/bundles/common-http.umd.js',
'@angular/common/http/testing': 'node:@angular/common/bundles/common-http-testing.umd.js',
'@angular/compiler': 'node:@angular/compiler/bundles/compiler.umd.js',
'@angular/compiler/testing': 'node:@angular/compiler/bundles/compiler-testing.umd.js',
'@angular/forms': 'node:@angular/forms/bundles/forms.umd.js',
'@angular/forms/testing': 'node:@angular/forms/bundles/forms-testing.umd.js',
'@angular/animations': 'node:@angular/animations/bundles/animations.umd.js',
'@angular/animations/browser': 'node:@angular/animations/bundles/animations-browser.umd.js',
'@angular/platform-browser/animations':
'node:@angular/platform-browser/bundles/platform-browser-animations.umd',
'@angular/platform-browser':
'node:@angular/platform-browser/bundles/platform-browser.umd.js',
'@angular/platform-browser/testing':
'node:@angular/platform-browser/bundles/platform-browser-testing.umd.js',
'@angular/platform-browser-dynamic':
'node:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
'@angular/platform-browser-dynamic/testing':
'node:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic-testing.umd.js',

// Path for local packages. Can be imported inside of tests.
'@ptsecurity/mosaic': 'dist/packages/mosaic/index.js',

'@ptsecurity/cdk': 'dist/packages/cdk/index.js',
'@ptsecurity/cdk/a11y': 'dist/packages/cdk/a11y/index.js',
'@ptsecurity/cdk/bidi': 'dist/packages/cdk/bidi/index.js',
'@ptsecurity/cdk/coercion': 'dist/packages/cdk/coercion/index.js',
'@ptsecurity/cdk/collections': 'dist/packages/cdk/collections/index.js',
'@ptsecurity/cdk/keycodes': 'dist/packages/cdk/keycodes/index.js',
'@ptsecurity/cdk/layout': 'dist/packages/cdk/layout/index.js',
'@ptsecurity/cdk/overlay': 'dist/packages/cdk/overlay/index.js',
'@ptsecurity/cdk/platform': 'dist/packages/cdk/platform/index.js',
'@ptsecurity/cdk/portal': 'dist/packages/cdk/portal/index.js',
'@ptsecurity/cdk/scrolling': 'dist/packages/cdk/scrolling/index.js',
'@ptsecurity/cdk/testing': 'dist/packages/cdk/testing/index.js',

'@ptsecurity/mosaic/button': 'dist/packages/mosaic/button/index.js',
'@ptsecurity/mosaic/core': 'dist/packages/mosaic/core/index.js',
'@ptsecurity/mosaic/divider': 'dist/packages/mosaic/divider/index.js',
'@ptsecurity/mosaic/list': 'dist/packages/mosaic/list/index.js',
'@ptsecurity/mosaic/progress-bar': 'dist/packages/mosaic/progress-bar/index.js',
'@ptsecurity/mosaic/icon': 'dist/packages/mosaic/icon/index.js',
'@ptsecurity/mosaic/radio': 'dist/packages/mosaic/radio/index.js',
'@ptsecurity/mosaic/checkbox': 'dist/packages/mosaic/checkbox/index.js'
},
packages: {
// Thirdparty barrels.
'rxjs': {main: 'index'},
'rxjs/operators': {main: 'index'},

// Set the default extension for the root package, because otherwise the demo-app can't
// be built within the production mode. Due to missing file extensions.
'.': {
defaultExtension: 'js'
}
}
});
119 changes: 23 additions & 96 deletions tests/karma-test-shim.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict';

/*global jasmine, __karma__, window*/
Error.stackTraceLimit = Infinity;

Expand All @@ -8,101 +6,32 @@ Error.stackTraceLimit = Infinity;
// take longer in some situations (e.g Saucelabs and Browserstack tunnels)
jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000;

__karma__.loaded = function () {
};

const baseDir = '/base';
const specFiles = Object.keys(window.__karma__.files).filter(isMosaicSpecFile);

// Configure the base path and map the different node packages.
System.config({
baseURL: baseDir,
paths: {
'node:*': 'node_modules/*'
},
map: {
'rxjs': 'node:rxjs',
'main': 'main.js',
'tslib': 'node:tslib/tslib.js',

// Angular specific mappings.
'@angular/core': 'node:@angular/core/bundles/core.umd.js',
'@angular/core/testing': 'node:@angular/core/bundles/core-testing.umd.js',
'@angular/common': 'node:@angular/common/bundles/common.umd.js',
'@angular/common/testing': 'node:@angular/common/bundles/common-testing.umd.js',
'@angular/common/http': 'node:@angular/common/bundles/common-http.umd.js',
'@angular/common/http/testing': 'node:@angular/common/bundles/common-http-testing.umd.js',
'@angular/compiler': 'node:@angular/compiler/bundles/compiler.umd.js',
'@angular/compiler/testing': 'node:@angular/compiler/bundles/compiler-testing.umd.js',
'@angular/forms': 'node:@angular/forms/bundles/forms.umd.js',
'@angular/forms/testing': 'node:@angular/forms/bundles/forms-testing.umd.js',
'@angular/animations': 'node:@angular/animations/bundles/animations.umd.js',
'@angular/animations/browser': 'node:@angular/animations/bundles/animations-browser.umd.js',
'@angular/platform-browser/animations':
'node:@angular/platform-browser/bundles/platform-browser-animations.umd',
'@angular/platform-browser':
'node:@angular/platform-browser/bundles/platform-browser.umd.js',
'@angular/platform-browser/testing':
'node:@angular/platform-browser/bundles/platform-browser-testing.umd.js',
'@angular/platform-browser-dynamic':
'node:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
'@angular/platform-browser-dynamic/testing':
'node:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic-testing.umd.js',

// Path for local packages. Can be imported inside of tests.
'@ptsecurity/mosaic': 'dist/packages/mosaic/index.js',
__karma__.loaded = function () {};

'@ptsecurity/cdk': 'dist/packages/cdk/index.js',
'@ptsecurity/cdk/a11y': 'dist/packages/cdk/a11y/index.js',
'@ptsecurity/cdk/bidi': 'dist/packages/cdk/bidi/index.js',
'@ptsecurity/cdk/coercion': 'dist/packages/cdk/coercion/index.js',
'@ptsecurity/cdk/collections': 'dist/packages/cdk/collections/index.js',
'@ptsecurity/cdk/keycodes': 'dist/packages/cdk/keycodes/index.js',
'@ptsecurity/cdk/layout': 'dist/packages/cdk/layout/index.js',
'@ptsecurity/cdk/overlay': 'dist/packages/cdk/overlay/index.js',
'@ptsecurity/cdk/platform': 'dist/packages/cdk/platform/index.js',
'@ptsecurity/cdk/portal': 'dist/packages/cdk/portal/index.js',
'@ptsecurity/cdk/scrolling': 'dist/packages/cdk/scrolling/index.js',
'@ptsecurity/cdk/testing': 'dist/packages/cdk/testing/index.js',

'@ptsecurity/mosaic/button': 'dist/packages/mosaic/button/index.js',
'@ptsecurity/mosaic/core': 'dist/packages/mosaic/core/index.js',
'@ptsecurity/mosaic/divider': 'dist/packages/mosaic/divider/index.js',
'@ptsecurity/mosaic/list': 'dist/packages/mosaic/list/index.js',
'@ptsecurity/mosaic/progress-bar': 'dist/packages/mosaic/progress-bar/index.js',
'@ptsecurity/mosaic/icon': 'dist/packages/mosaic/icon/index.js',
'@ptsecurity/mosaic/radio': 'dist/packages/mosaic/radio/index.js',
'@ptsecurity/mosaic/checkbox': 'dist/packages/mosaic/checkbox/index.js'
},
packages: {
// Thirdparty barrels.
'rxjs': {main: 'index'},
'rxjs/operators': {main: 'index'},

// Set the default extension for the root package, because otherwise the demo-app can't
// be built within the production mode. Due to missing file extensions.
'.': {
defaultExtension: 'js'
}
}
});
var specFiles = Object.keys(window.__karma__.files).filter(isMaterialSpecFile);

// Configure the Angular test bed and run all specs once configured.
configureTestBed()
.then(runMosaicSpecs)
.then(__karma__.start, __karma__.error);
.then(runMaterialSpecs)
.then(__karma__.start, function(error) {
// Passing in the error object directly to Karma won't log out the stack trace and
// passing the `originalErr` doesn't work correctly either. We have to log out the
// stack trace so we can actually debug errors before the tests have started.
console.error(error.originalErr.stack);
__karma__.error(error);
});


/** Runs the library specs in Karma. */
function runMosaicSpecs() {
/** Runs the Angular Material specs in Karma. */
function runMaterialSpecs() {
// By importing all spec files, Karma will run the tests directly.
return Promise.all(specFiles.map(function (fileName) {
return Promise.all(specFiles.map(function(fileName) {
return System.import(fileName);
}));
}

/** Whether the specified file is part of library. */
function isMosaicSpecFile(path) {
/** Whether the specified file is part of Angular Material. */
function isMaterialSpecFile(path) {
return path.slice(-8) === '.spec.js' && path.indexOf('node_modules') === -1;
}

Expand All @@ -112,10 +41,10 @@ function configureTestBed() {
System.import('@angular/core/testing'),
System.import('@angular/platform-browser-dynamic/testing')
]).then(function (providers) {
const testing = providers[0];
const testingBrowser = providers[1];
var testing = providers[0];
var testingBrowser = providers[1];

const testBed = testing.TestBed.initTestEnvironment(
var testBed = testing.TestBed.initTestEnvironment(
testingBrowser.BrowserDynamicTestingModule,
testingBrowser.platformBrowserDynamicTesting()
);
Expand All @@ -129,7 +58,7 @@ function configureTestBed() {
* destruction are thrown instead of silently logged. Also runs TestBed.resetTestingModule after
* each unit test.
*
* Without this patch, the combination of two behaviors is problematic for library:
* Without this patch, the combination of two behaviors is problematic for Angular Material:
* - TestBed.resetTestingModule catches errors thrown on fixture destruction and logs them without
* the errors ever being thrown. This means that any component errors that occur in ngOnDestroy
* can encounter errors silently and still pass unit tests.
Expand All @@ -139,16 +68,14 @@ function configureTestBed() {
*/
function patchTestBedToDestroyFixturesAfterEveryTest(testBed) {
// Original resetTestingModule function of the TestBed.
const _resetTestingModule = testBed.resetTestingModule;
var _resetTestingModule = testBed.resetTestingModule;

// Monkey-patch the resetTestingModule to destroy fixtures outside of a try/catch block.
// With https://github.com/angular/angular/commit/2c5a67134198a090a24f6671dcdb7b102fea6eba
// errors when destroying components are no longer causing Jasmine to fail.
testBed.resetTestingModule = function () {
testBed.resetTestingModule = function() {
try {
this._activeFixtures.forEach(function (fixture) {
fixture.destroy();
});
this._activeFixtures.forEach(function (fixture) { fixture.destroy(); });
} finally {
this._activeFixtures = [];
// Regardless of errors or not, run the original reset testing module function.
Expand All @@ -160,7 +87,7 @@ function patchTestBedToDestroyFixturesAfterEveryTest(testBed) {
// for us because it doesn't allow developers to see what test actually failed.
// Fixing this by resetting the testing module after each test.
// https://github.com/angular/angular/blob/master/packages/core/testing/src/before_each.ts#L25
afterEach(function () {
afterEach(function() {
testBed.resetTestingModule();
});
}
13 changes: 8 additions & 5 deletions tests/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,21 @@ module.exports = (config) => {
require('karma-junit-reporter')
],
files: [
{pattern: 'node_modules/core-js/client/core.js', included: true, watched: false},
{pattern: 'node_modules/core-js/client/core.min.js', included: true, watched: false},
{pattern: 'node_modules/tslib/tslib.js', included: true, watched: false},
{pattern: 'node_modules/systemjs/dist/system.src.js', included: true, watched: false},
{pattern: 'node_modules/zone.js/dist/zone.js', included: true, watched: false},
{pattern: 'node_modules/zone.js/dist/proxy.js', included: true, watched: false},
{pattern: 'node_modules/systemjs/dist/system.js', included: true, watched: false},
{pattern: 'node_modules/zone.js/dist/zone.min.js', included: true, watched: false},
{pattern: 'node_modules/zone.js/dist/proxy.min.js', included: true, watched: false},
{pattern: 'node_modules/zone.js/dist/sync-test.js', included: true, watched: false},
{pattern: 'node_modules/zone.js/dist/jasmine-patch.js', included: true, watched: false},
{pattern: 'node_modules/zone.js/dist/jasmine-patch.min.js', included: true, watched: false},
{pattern: 'node_modules/zone.js/dist/async-test.js', included: true, watched: false},
{pattern: 'node_modules/zone.js/dist/fake-async-test.js', included: true, watched: false},

// Include all Angular dependencies
{pattern: 'node_modules/@angular/**/*', included: false, watched: false},
{pattern: 'node_modules/rxjs/**/*', included: false, watched: false},

{pattern: 'tests/karma-system-config.js', included: true, watched: false},
{pattern: 'tests/karma-test-shim.js', included: true, watched: false},

// Paths to support debugging with source maps in dev tools
Expand Down Expand Up @@ -69,6 +70,8 @@ module.exports = (config) => {

singleRun: false,

transports: ['websocket', 'polling'],

browserConsoleLogOptions: {
terminal: true,
level: 'log'
Expand Down

0 comments on commit 38c1098

Please sign in to comment.