Skip to content

Commit 24aed4d

Browse files
committed
fix(tests): add global scripts in karma plugin
Note: renamed and lazy global scripts are not supported. Fix angular#2897
1 parent 1352545 commit 24aed4d

File tree

2 files changed

+87
-4
lines changed

2 files changed

+87
-4
lines changed

packages/angular-cli/plugins/karma.js

+20
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const init = (config) => {
99
}
1010
const angularCliConfig = require(path.join(config.basePath, config.angularCli.config));
1111
const appConfig = angularCliConfig.apps[0];
12+
const appRoot = path.join(config.basePath, appConfig.root);
1213
const environment = config.angularCli.environment || 'dev';
1314
const testConfig = {
1415
codeCoverage: config.angularCli.codeCoverage || false,
@@ -43,6 +44,25 @@ const init = (config) => {
4344
.filter((file) => config.preprocessors[file].indexOf('angular-cli') !== -1)
4445
.map((file) => config.preprocessors[file])
4546
.map((arr) => arr.splice(arr.indexOf('angular-cli'), 1, 'webpack', 'sourcemap'));
47+
48+
// Add global scripts
49+
if (appConfig.scripts && appConfig.scripts.length > 0) {
50+
const globalScriptPatterns = appConfig.scripts
51+
.map(script => typeof script === 'string' ? { input: script } : script)
52+
// Neither renamed nor lazy scripts are currently supported
53+
.filter(script => !(script.output || script.lazy))
54+
.map(script => ({
55+
pattern: path.resolve(appRoot, script.input),
56+
included: true,
57+
served: true,
58+
watched: true
59+
}));
60+
61+
// Unshift elements onto the beginning of the files array.
62+
// It's important to not replace the array, because
63+
// karma already has a reference to the existing array.
64+
Array.prototype.unshift.apply(config.files, globalScriptPatterns);
65+
}
4666
}
4767

4868
init.$inject = ['config'];

tests/e2e/tests/test/test.ts

+67-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,71 @@
1-
import {ng} from '../../utils/process';
1+
import { writeMultipleFiles } from '../../utils/fs';
2+
import { ng } from '../../utils/process';
3+
import { updateJsonFile } from '../../utils/project';
4+
import { expectToFail } from '../../utils/utils';
5+
import { stripIndent } from 'common-tags';
26

3-
4-
export default function() {
7+
export default function () {
58
// make sure both --watch=false and --single-run work
69
return ng('test', '--single-run')
7-
.then(() => ng('test', '--watch=false'));
10+
.then(() => ng('test', '--watch=false'))
11+
// prepare global scripts test files
12+
.then(() => writeMultipleFiles({
13+
'src/string-script.js': `stringScriptGlobal = 'string-scripts.js';`,
14+
'src/input-script.js': `inputScriptGlobal = 'input-scripts.js';`,
15+
'src/typings.d.ts': stripIndent`
16+
declare var stringScriptGlobal: any;
17+
declare var inputScriptGlobal: any;
18+
`,
19+
'src/app/app.component.ts': stripIndent`
20+
import { Component } from '@angular/core';
21+
22+
@Component({ selector: 'app-root', template: '' })
23+
export class AppComponent {
24+
stringScriptGlobalProp = stringScriptGlobal;
25+
inputScriptGlobalProp = inputScriptGlobal;
26+
}
27+
`,
28+
'src/app/app.component.spec.ts': stripIndent`
29+
import { TestBed, async } from '@angular/core/testing';
30+
import { AppComponent } from './app.component';
31+
32+
describe('AppComponent', () => {
33+
beforeEach(() => {
34+
TestBed.configureTestingModule({ declarations: [ AppComponent ] });
35+
TestBed.compileComponents();
36+
});
37+
38+
it('should have access to string-script.js', async(() => {
39+
let app = TestBed.createComponent(AppComponent).debugElement.componentInstance;
40+
expect(app.stringScriptGlobalProp).toEqual('string-scripts.js');
41+
}));
42+
43+
it('should have access to input-script.js', async(() => {
44+
let app = TestBed.createComponent(AppComponent).debugElement.componentInstance;
45+
expect(app.inputScriptGlobalProp).toEqual('input-scripts.js');
46+
}));
47+
});
48+
49+
describe('Spec', () => {
50+
it('should have access to string-script.js', async(() => {
51+
expect(stringScriptGlobal).toBe('string-scripts.js');
52+
}));
53+
54+
it('should have access to input-script.js', async(() => {
55+
expect(inputScriptGlobal).toBe('input-scripts.js');
56+
}));
57+
});
58+
`
59+
}))
60+
// should fail because the global scripts were not added to scripts array
61+
.then(() => expectToFail(() => ng('test', '--single-run')))
62+
.then(() => updateJsonFile('angular-cli.json', configJson => {
63+
const app = configJson['apps'][0];
64+
app['scripts'] = [
65+
'string-script.js',
66+
{ input: 'input-script.js' }
67+
];
68+
}))
69+
// should pass now
70+
.then(() => ng('test', '--single-run'));
871
}

0 commit comments

Comments
 (0)