Skip to content

Commit 3459300

Browse files
Meligyhansl
authored andcommitted
fix(tests): serve assets files from ng test (angular#3628)
1 parent d929adc commit 3459300

File tree

3 files changed

+89
-6
lines changed

3 files changed

+89
-6
lines changed

packages/angular-cli/plugins/karma.js

+23
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
const path = require('path');
2+
const fs = require('fs');
3+
24
const getWebpackTestConfig = require('../models/webpack-build-test').getWebpackTestConfig;
35
const CliConfig = require('../models/config').CliConfig;
46

@@ -17,6 +19,27 @@ const init = (config) => {
1719
progress: config.angularCli.progress
1820
}
1921

22+
// add assets
23+
if (appConfig.assets) {
24+
const assets = typeof appConfig.assets === 'string' ? [appConfig.assets] : appConfig.assets;
25+
config.proxies = config.proxies || {};
26+
assets.forEach(asset => {
27+
const fullAssetPath = path.join(config.basePath, appConfig.root, asset);
28+
const isDirectory = fs.lstatSync(fullAssetPath).isDirectory();
29+
const filePattern = isDirectory ? fullAssetPath + '/**' : fullAssetPath;
30+
const proxyPath = isDirectory ? asset + '/' : asset;
31+
config.files.push({
32+
pattern: filePattern,
33+
included: false,
34+
served: true,
35+
watched: true
36+
});
37+
// The `files` entry serves the file from `/base/{appConfig.root}/{asset}`
38+
// so, we need to add a URL rewrite that exposes the asset as `/{asset}` only
39+
config.proxies['/' + proxyPath] = '/base/' + appConfig.root + '/' + proxyPath;
40+
});
41+
}
42+
2043
// add webpack config
2144
const webpackConfig = getWebpackTestConfig(config.basePath, environment, appConfig, testConfig);
2245
const webpackMiddlewareConfig = {

tests/e2e/tests/test/test.ts

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

37

4-
export default function() {
5-
// make sure both --watch=false and --single-run work
8+
export default function () {
9+
// Each test function returns PromiseLike<Something_Different>,
10+
// which would throw normally
11+
// but resolve() normalizes this to <any> from the start
12+
return Promise.resolve()
13+
.then(() => testWatchFalseAndSingleRun())
14+
.then(() => testAssetsAreServed());
15+
}
16+
17+
// Make sure both --watch=false and --single-run work
18+
function testWatchFalseAndSingleRun() {
619
return ng('test', '--single-run')
720
.then(() => ng('test', '--watch=false'));
821
}
22+
23+
// Make sure asset files are served
24+
function testAssetsAreServed() {
25+
return Promise.resolve()
26+
.then(() => writeMultipleFiles({
27+
'src/assets/file.txt': 'assets-folder-content',
28+
'src/file.txt': 'file-content',
29+
// Not using `async()` in tests as it seemed to swallow `fetch()` errors
30+
'src/app/app.component.spec.ts': stripIndent`
31+
describe('Test Runner', () => {
32+
const fetch = global['fetch'];
33+
it('should serve files in assets folder', (done) => {
34+
fetch('/assets/file.txt')
35+
.then(response => response.text())
36+
.then(fileText => {
37+
expect(fileText).toMatch('assets-folder-content');
38+
done();
39+
});
40+
});
41+
it('should serve files explicitly added to assets array', (done) => {
42+
fetch('/file.txt')
43+
.then(response => response.text())
44+
.then(fileText => {
45+
expect(fileText).toMatch('file-content');
46+
done();
47+
});
48+
});
49+
});
50+
`
51+
}))
52+
// Test failure condition (no assets in angular-cli.json)
53+
.then(() => updateJsonFile('angular-cli.json', configJson => {
54+
const app = configJson['apps'][0];
55+
app['assets'] = [];
56+
}))
57+
.then(() => expectToFail(() => ng('test', '--single-run'),
58+
'Should fail because the assets to serve were not in the angular-cli config'))
59+
// Test passing condition (assets are included)
60+
.then(() => updateJsonFile('angular-cli.json', configJson => {
61+
const app = configJson['apps'][0];
62+
app['assets'] = ['assets', 'file.txt'];
63+
}))
64+
.then(() => ng('test', '--single-run'));
65+
}

tests/e2e/utils/utils.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11

2-
export function expectToFail(fn: () => Promise<any>): Promise<void> {
2+
export function expectToFail(fn: () => Promise<any>, errorMessage?: string): Promise<void> {
33
return fn()
44
.then(() => {
5-
throw new Error(`Function ${fn.source} was expected to fail, but succeeded.`);
6-
}, () => {});
5+
const functionSource = fn.name || (<any>fn).source || fn.toString();
6+
const errorDetails = errorMessage ? `\n\tDetails:\n\t${errorMessage}` : '';
7+
throw new Error(
8+
`Function ${functionSource} was expected to fail, but succeeded.${errorDetails}`);
9+
}, () => { });
710
}
811

912
export function isMobileTest() {

0 commit comments

Comments
 (0)