Skip to content

Commit 2f598c2

Browse files
committed
feat(ng test): serve assets files from ng test
closes angular#2803
1 parent 874cec6 commit 2f598c2

File tree

2 files changed

+78
-3
lines changed

2 files changed

+78
-3
lines changed

packages/angular-cli/plugins/karma.js

+20
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,26 @@ const init = (config) => {
1717
progress: config.angularCli.progress
1818
}
1919

20+
// add assets
21+
if(!config.proxies) {
22+
config.proxies = {};
23+
}
24+
var assets = appConfig.assets;
25+
if(typeof assets === 'string') {
26+
assets = [assets];
27+
}
28+
for (var i = 0; i < assets.length; i++) {
29+
var asset = assets[i];
30+
// files are added under `/base` by default, but we can rewrite the URLs
31+
config.proxies['/' + asset] = '/base/' + appConfig.root + '/' + asset;
32+
config.files.push({
33+
pattern: './' + appConfig.root + '/' + asset,
34+
included: false,
35+
served: true,
36+
watched: true
37+
});
38+
}
39+
2040
// add webpack config
2141
const webpackConfig = getWebpackTestConfig(config.basePath, environment, appConfig, testConfig);
2242
const webpackMiddlewareConfig = {

tests/e2e/tests/test/test.ts

+58-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,63 @@
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

7+
export default function () {
8+
// Each test function returns PromiseLike<Something_Different>,
9+
// which would throw normally
10+
// but resolve() normalizes this to <any> from the start
11+
return Promise.resolve()
12+
.then(() => testWatchFalseAndSingleRun())
13+
.then(() => testAssetsAreServed());
14+
}
315

4-
export default function() {
5-
// make sure both --watch=false and --single-run work
16+
// Make sure both --watch=false and --single-run work
17+
function testWatchFalseAndSingleRun() {
618
return ng('test', '--single-run')
719
.then(() => ng('test', '--watch=false'));
820
}
21+
22+
// Make sure asset files are served
23+
function testAssetsAreServed() {
24+
return Promise.resolve()
25+
.then(() => writeMultipleFiles({
26+
'src/assets/file.txt': `assets-folder-content`,
27+
'src/file.txt': 'file-content',
28+
'src/app/app.component.spec.ts': stripIndent`
29+
import { async } from '@angular/core/testing';
30+
describe('Test Runner', () => {
31+
const fetch = global['fetch'];
32+
it('should serve files in assets folder', async(() => {
33+
// throw new Error('Should fail');
34+
fetch('/assets/file.txt')
35+
.then(response => {
36+
expect(response.ok()).toBe(true);
37+
expect(response.text()).toBe('assets-folder-content');
38+
});
39+
}));
40+
it('should serve files explicitly added to assets array', async(() => {
41+
fetch('/file.txt')
42+
.then(response => {
43+
expect(response.ok()).toBe(true);
44+
expect(response.text()).toBe('file-content');
45+
});
46+
}));
47+
});
48+
`
49+
}))
50+
// Test failure condition (no assets in angular-cli.json)
51+
.then(() => updateJsonFile('angular-cli.json', configJson => {
52+
const app = configJson['apps'][0];
53+
app['assets'] = [];
54+
}))
55+
.then(() => expectToFail(() => ng('test', '--single-run'),
56+
'Should fail because the assets to serve were not in the angular-cli config'))
57+
// Test passing condition (assets are included)
58+
.then(() => updateJsonFile('angular-cli.json', configJson => {
59+
const app = configJson['apps'][0];
60+
app['assets'] = ['assets', 'file.txt'];
61+
}))
62+
.then(() => ng('test', '--single-run'));
63+
}

0 commit comments

Comments
 (0)