Skip to content

Commit

Permalink
feat(@angular-devkit/build-angular): stop karma gracefully
Browse files Browse the repository at this point in the history
  • Loading branch information
filipesilva committed Nov 1, 2018
1 parent 27d9882 commit 7dd7955
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 62 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -103,6 +103,7 @@
"istanbul": "^0.4.5",
"jasmine": "^2.6.0",
"jasmine-spec-reporter": "^3.2.0",
"karma": "~3.1.1",
"karma-jasmine-html-reporter": "^0.2.2",
"license-checker": "^20.1.0",
"minimatch": "^3.0.4",
Expand Down
2 changes: 1 addition & 1 deletion packages/angular_devkit/build_angular/package.json
Expand Up @@ -77,7 +77,7 @@
"jquery": "^3.3.1",
"jasmine-core": "~2.8.0",
"jasmine-spec-reporter": "~4.2.1",
"karma": "~3.0.0",
"karma": "~3.1.1",
"karma-chrome-launcher": "~2.2.0",
"karma-coverage-istanbul-reporter": "~2.0.0",
"karma-jasmine": "~1.1.0",
Expand Down
11 changes: 5 additions & 6 deletions packages/angular_devkit/build_angular/src/karma/index.ts
Expand Up @@ -108,15 +108,14 @@ export class KarmaBuilder implements Builder<KarmaBuilderSchema> {

// Complete the observable once the Karma server returns.
const karmaServer = new karma.Server(karmaOptions, () => obs.complete());
karmaServer.start();
const karmaStartPromise = karmaServer.start();

// Cleanup, signal Karma to exit.
return () => {
// Karma does not seem to have a way to exit the server gracefully.
// See https://github.com/karma-runner/karma/issues/2867#issuecomment-369912167
// TODO: make a PR for karma to add `karmaServer.close(code)`, that
// calls `disconnectBrowsers(code);`
// karmaServer.close();
// Karma only has the `stop` method start with 3.1.1, so we must defensively check.
if (karmaServer.stop && typeof karmaServer.stop === 'function') {
return karmaStartPromise.then(() => karmaServer.stop());
}
};
})),
);
Expand Down
Expand Up @@ -10,64 +10,18 @@ import { runTargetSpec } from '@angular-devkit/architect/testing';
import { debounceTime, take, tap } from 'rxjs/operators';
import { host, karmaTargetSpec } from '../utils';


// Karma watch mode is currently bugged:
// - errors print a huge stack trace
// - karma does not have a way to close the server
// gracefully (https://github.com/karma-runner/karma/issues/3149)
// TODO: fix these before 6.0 final.
xdescribe('Karma Builder watch mode', () => {
describe('Karma Builder watch mode', () => {
beforeEach(done => host.initialize().toPromise().then(done, done.fail));
afterEach(done => host.restore().toPromise().then(done, done.fail));

it('works', (done) => {
it('works', async () => {
const overrides = { watch: true };
runTargetSpec(host, karmaTargetSpec, overrides).pipe(
const res = await runTargetSpec(host, karmaTargetSpec, overrides).pipe(
debounceTime(500),
tap((buildEvent) => expect(buildEvent.success).toBe(true)),
take(1),
).toPromise().then(done, done.fail);
}, 30000);

it('recovers from compilation failures in watch mode', (done) => {
const overrides = { watch: true };
let buildNumber = 0;

runTargetSpec(host, karmaTargetSpec, overrides).pipe(
debounceTime(500),
tap((buildEvent) => {
buildNumber += 1;
switch (buildNumber) {
case 1:
// Karma run should succeed.
// Add a compilation error.
expect(buildEvent.success).toBe(true);
host.writeMultipleFiles({
'src/app/app.component.spec.ts': '<p> definitely not typescript </p>',
});
break;

case 2:
// Karma run should fail due to compilation error. Fix it.
expect(buildEvent.success).toBe(false);
host.writeMultipleFiles({ 'src/foo.spec.ts': '' });
break;

case 3:
// Karma run should succeed again.
expect(buildEvent.success).toBe(true);
break;

default:
break;
}
}),
take(3),
).toPromise().then(done, done.fail);
}, 30000);
).toPromise();

it('does not rebuild when nothing changed', (done) => {
// Start the server in watch mode, wait for the first build to finish, touch
// test.js without changing it, wait 5s then exit unsuscribe, verify only one event was emitted.
expect(res).toEqual({ success: true });
}, 30000);
});
8 changes: 4 additions & 4 deletions yarn.lock
Expand Up @@ -5542,10 +5542,10 @@ karma@alexeagle/karma#fa1a84ac881485b5657cb669e9b4e5da77b79f0a:
tmp "0.0.33"
useragent "^2.1.12"

karma@~3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/karma/-/karma-3.0.0.tgz#6da83461a8a28d8224575c3b5b874e271b4730c3"
integrity sha512-ZTjyuDXVXhXsvJ1E4CnZzbCjSxD6sEdzEsFYogLuZM0yqvg/mgz+O+R1jb0J7uAQeuzdY8kJgx6hSNXLwFuHIQ==
karma@~3.1.1:
version "3.1.1"
resolved "https://registry.yarnpkg.com/karma/-/karma-3.1.1.tgz#94c8edd20fb9597ccde343326da009737fb0423a"
integrity sha512-NetT3wPCQMNB36uiL9LLyhrOt8SQwrEKt0xD3+KpTCfm0VxVyUJdPL5oTq2Ic5ouemgL/Iz4wqXEbF3zea9kQQ==
dependencies:
bluebird "^3.3.0"
body-parser "^1.16.1"
Expand Down

0 comments on commit 7dd7955

Please sign in to comment.