Skip to content

Commit

Permalink
fix(tests): add global scripts in karma plugin
Browse files Browse the repository at this point in the history
Note: renamed and lazy global scripts are not supported.

Fix angular#2897
  • Loading branch information
filipesilva committed Dec 13, 2016
1 parent 1352545 commit 20c5cb6
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 4 deletions.
20 changes: 20 additions & 0 deletions packages/angular-cli/plugins/karma.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const init = (config) => {
}
const angularCliConfig = require(path.join(config.basePath, config.angularCli.config));
const appConfig = angularCliConfig.apps[0];
const appRoot = path.join(config.basePath, appConfig.root);
const environment = config.angularCli.environment || 'dev';
const testConfig = {
codeCoverage: config.angularCli.codeCoverage || false,
Expand Down Expand Up @@ -43,6 +44,25 @@ const init = (config) => {
.filter((file) => config.preprocessors[file].indexOf('angular-cli') !== -1)
.map((file) => config.preprocessors[file])
.map((arr) => arr.splice(arr.indexOf('angular-cli'), 1, 'webpack', 'sourcemap'));

// Add global scripts
if (appConfig.scripts || appConfig.scripts.length > 0) {
const globalScriptPatterns = appConfig.scripts
.map(script => typeof script === 'string' ? { input: script } : script)
// Neither renamed nor lazy scripts are currently supported
.filter(script => !(script.output || script.lazy))
.map(script => ({
pattern: path.resolve(appRoot, script.input),
included: true,
served: true,
watched: true
}));

// Unshift elements onto the beginning of the files array.
// It's important to not replace the array, because
// karma already has a reference to the existing array.
Array.prototype.unshift.apply(config.files, globalScriptPatterns);
}
}

init.$inject = ['config'];
Expand Down
71 changes: 67 additions & 4 deletions tests/e2e/tests/test/test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,71 @@
import {ng} from '../../utils/process';
import { writeMultipleFiles } from '../../utils/fs';
import { ng } from '../../utils/process';
import { updateJsonFile } from '../../utils/project';
import { expectToFail } from '../../utils/utils';
import { stripIndent } from 'common-tags';


export default function() {
export default function () {
// make sure both --watch=false and --single-run work
return ng('test', '--single-run')
.then(() => ng('test', '--watch=false'));
.then(() => ng('test', '--watch=false'))
// prepare global scripts test files
.then(() => writeMultipleFiles({
'src/string-script.js': `stringScriptGlobal = 'string-scripts.js';`,
'src/input-script.js': `inputScriptGlobal = 'input-scripts.js';`,
'src/typings.d.ts': stripIndent`
declare var stringScriptGlobal: any;
declare var inputScriptGlobal: any;
`,
'src/app/app.component.ts': stripIndent`
import { Component } from '@angular/core';
@Component({ selector: 'app-root', template: '' })
export class AppComponent {
stringScriptGlobalProp = stringScriptGlobal;
inputScriptGlobalProp = inputScriptGlobal;
}
`,
'src/app/app.component.spec.ts': stripIndent`
import { TestBed, async } from '@angular/core/testing';
import { AppComponent } from './app.component';
describe('AppComponent', () => {
beforeEach(() => {
TestBed.configureTestingModule({ declarations: [ AppComponent ] });
TestBed.compileComponents();
});
it('should have access to string-script.js', async(() => {
let app = TestBed.createComponent(AppComponent).debugElement.componentInstance;
expect(app.stringScriptGlobalProp).toEqual('string-scripts.js');
}));
it('should have access to input-script.js', async(() => {
let app = TestBed.createComponent(AppComponent).debugElement.componentInstance;
expect(app.inputScriptGlobalProp).toEqual('input-scripts.js');
}));
});
describe('Spec', () => {
it('should have access to string-script.js', async(() => {
expect(stringScriptGlobal).toBe('string-scripts.js');
}));
it('should have access to input-script.js', async(() => {
expect(inputScriptGlobal).toBe('input-scripts.js');
}));
});
`
}))
// should fail because the global scripts were not added to scripts array
.then(() => expectToFail(() => ng('test', '--single-run')))
.then(() => updateJsonFile('angular-cli.json', configJson => {
const app = configJson['apps'][0];
app['scripts'] = [
'string-script.js',
{ input: 'input-script.js' }
];
}))
// should pass now
.then(() => ng('test', '--single-run'));
}

0 comments on commit 20c5cb6

Please sign in to comment.