Skip to content

Commit

Permalink
feat(ng test): serve assets files from ng test
Browse files Browse the repository at this point in the history
  • Loading branch information
Meligy committed Dec 19, 2016
1 parent 874cec6 commit f8148bf
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 3 deletions.
21 changes: 21 additions & 0 deletions packages/angular-cli/plugins/karma.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const path = require('path');
const fs = require('fs');

const getWebpackTestConfig = require('../models/webpack-build-test').getWebpackTestConfig;
const CliConfig = require('../models/config').CliConfig;

Expand All @@ -17,6 +19,25 @@ const init = (config) => {
progress: config.angularCli.progress
}

// add assets
if (appConfig.assets) {
const assets = typeof appConfig.assets === 'string' ? [appConfig.assets] : appConfig.assets;
config.proxies = config.proxies || {};
assets.forEach(asset => {
const fullAssetPath = path.join(config.basePath, appConfig.root, asset);
const isDirectory = fs.lstatSync(fullAssetPath).isDirectory();
const filePattern = isDirectory ? fullAssetPath + '/**' : fullAssetPath;
const proxyPath = isDirectory ? asset + '/' : asset;
config.files.push({
pattern: filePattern,
included: false,
served: true,
watched: true
});
config.proxies['/' + proxyPath] = '/base/' + appConfig.root + '/' + proxyPath;
});
}

// add webpack config
const webpackConfig = getWebpackTestConfig(config.basePath, environment, appConfig, testConfig);
const webpackMiddlewareConfig = {
Expand Down
63 changes: 60 additions & 3 deletions tests/e2e/tests/test/test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,65 @@
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() {
// make sure both --watch=false and --single-run work
export default function () {
// Each test function returns PromiseLike<Something_Different>,
// which would throw normally
// but resolve() normalizes this to <any> from the start
return Promise.resolve()
.then(() => testWatchFalseAndSingleRun())
.then(() => testAssetsAreServed());
}

// Make sure both --watch=false and --single-run work
function testWatchFalseAndSingleRun() {
return ng('test', '--single-run')
.then(() => ng('test', '--watch=false'));
}

// Make sure asset files are served
function testAssetsAreServed() {
return Promise.resolve()
.then(() => writeMultipleFiles({
'src/assets/file.txt': 'assets-folder-content',
'src/file.txt': 'file-content',
// Not using `async()` in tests as it seemed to swallow `fetch()` errors
'src/app/app.component.spec.ts': stripIndent`
describe('Test Runner', () => {
const fetch = global['fetch'];
it('should serve files in assets folder', (done) => {
fetch('/assets/file.txt')
.then(response => response.text())
.then(fileText => {
expect(fileText).toMatch('assets-folder-content');
done();
});
});
it('should serve files explicitly added to assets array', (done) => {
fetch('/file.txt')
.then(response => response.text())
.then(fileText => {
expect(fileText).toMatch('file-content');
done();
});
});
});
`
}))
// Test failure condition (no assets in angular-cli.json)
.then(() => updateJsonFile('angular-cli.json', configJson => {
const app = configJson['apps'][0];
app['assets'] = [];
}))
.then(() => expectToFail(() => ng('test', '--single-run'),
'Should fail because the assets to serve were not in the angular-cli config'))
// Test passing condition (assets are included)
.then(() => updateJsonFile('angular-cli.json', configJson => {
const app = configJson['apps'][0];
app['assets'] = ['assets', 'file.txt'];
}))
.then(() => ng('test', '--single-run'));
}

0 comments on commit f8148bf

Please sign in to comment.