Skip to content

Commit

Permalink
use karma shim to avoid adding BrowserDomAdapter to spec files
Browse files Browse the repository at this point in the history
  • Loading branch information
ocombe committed Jan 1, 2016
1 parent 784250d commit c010563
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 13 deletions.
26 changes: 26 additions & 0 deletions karma-shim.js
Original file line number Diff line number Diff line change
@@ -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();
7 changes: 3 additions & 4 deletions karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down
3 changes: 0 additions & 3 deletions src/app/app.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => [
Expand Down
3 changes: 0 additions & 3 deletions src/app/components/home/home.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
3 changes: 0 additions & 3 deletions src/app/services/api/api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {

Expand Down

0 comments on commit c010563

Please sign in to comment.