From c01056322dadfa3df99aa82079ed70f256f3e4fe Mon Sep 17 00:00:00 2001 From: Olivier Combe Date: Fri, 1 Jan 2016 19:36:08 +0100 Subject: [PATCH] use karma shim to avoid adding BrowserDomAdapter to spec files --- karma-shim.js | 26 ++++++++++++++++++++++++++ karma.conf.js | 7 +++---- src/app/app.spec.ts | 3 --- src/app/components/home/home.spec.ts | 3 --- src/app/services/api/api.spec.ts | 3 --- 5 files changed, 29 insertions(+), 13 deletions(-) create mode 100644 karma-shim.js diff --git a/karma-shim.js b/karma-shim.js new file mode 100644 index 00000000..e2baeed4 --- /dev/null +++ b/karma-shim.js @@ -0,0 +1,26 @@ +Error.stackTraceLimit = Infinity; +require('es6-shim'); +require('angular2/bundles/angular2-polyfills.js'); +require('angular2/testing'); + +/* + Ok, this is kinda crazy. We can use the the context method on + require that webpack created in order to tell webpack + what files we actually want to require or import. + Below, context will be an function/object with file names as keys. + using that regex we are saying look in client/app and find + any file that ends with spec.js and get its path. By passing in true + we say do this recursively + */ +var appContext = require.context('./src', true, /\.spec\.ts/); + +// get all the files, for each file, call the context function +// that will require the file and load it up here. Context will +// loop and require those spec files here +appContext.keys().forEach(appContext); + + +// Select BrowserDomAdapter. +// see https://github.com/AngularClass/angular2-webpack-starter/issues/124 +var domAdapter = require('angular2/src/platform/browser/browser_adapter'); +domAdapter.BrowserDomAdapter.makeCurrent(); diff --git a/karma.conf.js b/karma.conf.js index 008a6fb9..4b0a6031 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -13,9 +13,8 @@ module.exports = function(config) { // list of files / patterns to load in the browser files: [ - {pattern: 'node_modules/es6-shim/es6-shim.js', watched: false}, - {pattern: 'node_modules/angular2/bundles/angular2-polyfills.js', watched: false}, - {pattern: './src/app/**/*.spec.ts', watched: true, included: true} + {pattern: './karma-shim.js', watched: false}, + {pattern: './src/app/**/*.spec.ts', watched: true, included: false} ], // list of files to exclude @@ -24,7 +23,7 @@ module.exports = function(config) { // preprocess matching files before serving them to the browser // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor preprocessors: { - './src/app/**/*.spec.ts': ['webpack', 'sourcemap'] + './karma-shim.js': ['webpack', 'sourcemap'] }, webpack: webpackConfig, diff --git a/src/app/app.spec.ts b/src/app/app.spec.ts index 2c7e72a6..03895609 100644 --- a/src/app/app.spec.ts +++ b/src/app/app.spec.ts @@ -10,9 +10,6 @@ import { import {Api} from './services/api/api'; import {App} from './app'; -import {BrowserDomAdapter} from 'angular2/src/platform/browser/browser_adapter'; -BrowserDomAdapter.makeCurrent(); // in the test code - describe('App', () => { // provide our implementations or mocks to the dependency injector beforeEachProviders(() => [ diff --git a/src/app/components/home/home.spec.ts b/src/app/components/home/home.spec.ts index fe2ef2b2..afcaca23 100644 --- a/src/app/components/home/home.spec.ts +++ b/src/app/components/home/home.spec.ts @@ -11,9 +11,6 @@ import {Component, provide} from 'angular2/core'; import {BaseRequestOptions, Http} from 'angular2/http'; import {MockBackend} from 'angular2/http/testing'; -import {BrowserDomAdapter} from 'angular2/src/platform/browser/browser_adapter'; -BrowserDomAdapter.makeCurrent(); // in the test code - // Load the implementations that should be tested import {Home} from './home'; diff --git a/src/app/services/api/api.spec.ts b/src/app/services/api/api.spec.ts index fd770db1..d5b3df9e 100644 --- a/src/app/services/api/api.spec.ts +++ b/src/app/services/api/api.spec.ts @@ -12,9 +12,6 @@ import { import {provide} from 'angular2/core'; import {Api} from './api'; -import {BrowserDomAdapter} from 'angular2/src/platform/browser/browser_adapter'; -BrowserDomAdapter.makeCurrent(); // in the test code - describe('Api Service', () => {