Skip to content

Commit

Permalink
build: add test:watch task
Browse files Browse the repository at this point in the history
  • Loading branch information
hsuanxyz committed Jun 17, 2019
1 parent 5be56c0 commit c31f315
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 10 deletions.
2 changes: 2 additions & 0 deletions components/karma.conf.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Karma configuration file, see link for more information
// https://karma-runner.github.io/1.0/config/configuration-file.html

const tags = process.env && process.env['NG_TEST_TAGS'];
module.exports = function(config) {
config.set({
basePath: '',
Expand All @@ -15,6 +16,7 @@ module.exports = function(config) {
require('karma-viewport')
],
client: {
args: [tags],
clearContext: true // leave Jasmine Spec Runner output visible in browser
},
coverageIstanbulReporter: {
Expand Down
23 changes: 20 additions & 3 deletions components/test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,37 @@
// This file is required by karma.conf.js and loads recursively all the .spec and framework files

// tslint:disable-next-line:no-import-side-effect
import 'zone.js/dist/zone-testing';

import { getTestBed } from '@angular/core/testing';
import {
BrowserDynamicTestingModule,
platformBrowserDynamicTesting
platformBrowserDynamicTesting,
BrowserDynamicTestingModule
} from '@angular/platform-browser-dynamic/testing';

// tslint:disable-next-line:no-any
declare const __karma__: any;
// tslint:disable-next-line:no-any
declare const require: any;

const tags = __karma__.config.args[0];
// First, initialize the Angular testing environment.
getTestBed().initTestEnvironment(
BrowserDynamicTestingModule,
platformBrowserDynamicTesting()
);

let filterRegExp: RegExp;

if (tags) {
filterRegExp = new RegExp(`(${tags})\\.spec\\.ts$`);
} else {
filterRegExp = /\.spec\.ts$/;
}

// Then we find all the tests.
const context = require.context('./', true, /\.spec\.ts$/);
// Filter specify file
const specFiles = context.keys().filter((path: string) => filterRegExp.test(path));
// And load the modules.
context.keys().map(context);
specFiles.map(context);
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"scripts": {
"start": "gulp start:dev",
"test": "ng test --watch=false --code-coverage",
"test:watch": "gulp test:watch --tags $tags",
"build": "gulp build:release",
"build:lib": "gulp build:library",
"doc": "gulp build:preview",
Expand Down Expand Up @@ -43,8 +44,8 @@
"@angular/http": "~7.2.0",
"@angular/language-service": "~8.0.0",
"@angular/platform-browser": "~8.0.0",
"@angular/platform-server": "^8.0.0",
"@angular/platform-browser-dynamic": "~8.0.0",
"@angular/platform-server": "^8.0.0",
"@angular/pwa": "^0.800.1",
"@angular/router": "~8.0.0",
"@angular/service-worker": "~8.0.0",
Expand Down Expand Up @@ -80,6 +81,7 @@
"less-plugin-clean-css": "^1.5.1",
"lint-staged": "^8.1.5",
"marked": "^0.5.1",
"minimist": "^1.2.0",
"ng-packagr": "^5.2.0",
"ngx-color": "^2.0.5",
"node-prismjs": "^0.1.1",
Expand All @@ -88,9 +90,9 @@
"protractor": "~5.4.0",
"readline-sync": "^1.4.9",
"remark": "^8.0.0",
"rxjs": "~6.5.2",
"resolve-bin": "^0.4.0",
"run-sequence": "^2.2.1",
"rxjs": "~6.5.2",
"sitemap": "^2.1.0",
"ts-node": "^7.0.1",
"tsickle": ">=0.34.0",
Expand Down
1 change: 1 addition & 0 deletions scripts/gulp/gulpfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { series, task } from 'gulp';
import './tasks/clean';
import './tasks/default';
import './tasks/schematic';
import './tasks/unit-test';
import './tasks/universal';

import './tasks/library';
Expand Down
13 changes: 13 additions & 0 deletions scripts/gulp/tasks/unit-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { task } from 'gulp';
import { execTask } from '../util/task-helpers';

task('test:watch', (done) => {
const argv = require('minimist')(process.argv.slice(2));
return execTask(
'ng',
['test', '--watch=true', '--code-coverage'],
{
'NG_TEST_TAGS': argv.tags || ''
}
)(done);
});
9 changes: 4 additions & 5 deletions scripts/gulp/util/task-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@ export function cleanTask(glob: string | string[]): gulp.TaskFunction {
return () => gulp.src(glob, { read: false, allowEmpty: true }).pipe(gulpClean(null));
}

export function execTask(binPath: string, args: string[]): gulp.TaskFunction {
export function execTask(binPath: string, args: string[], env: {} = {}): gulp.TaskFunction {
return (done: (err?: string) => void) => {
const env = {...process.env};
// https://github.com/angular/angular-cli/issues/10922
// tslint:disable-next-line:no-any
(process.stdout as any)._handle.setBlocking(true);
// tslint:disable-next-line:no-any
(process.stdout as any)._handle.setBlocking(true);
const childProcess = child_process.spawn(binPath, args, {
env,
env: {...process.env, ...env},
cwd: process.cwd(),
stdio: "inherit"
});
Expand All @@ -28,7 +27,7 @@ export function execTask(binPath: string, args: string[]): gulp.TaskFunction {
};
}

export function execNodeTask(packageName: string, executable: string | string[], args?: string[]): gulp.TaskFunction {
export function execNodeTask(packageName: string, executable: string | string[], args?: string[], env: {} = {}): gulp.TaskFunction {
if (!args) {
// tslint:disable-next-line:no-parameter-reassignment
args = executable as string[];
Expand All @@ -43,7 +42,7 @@ export function execNodeTask(packageName: string, executable: string | string[],
if (err) {
done(err);
} else {
execTask('node', ['--max_old_space_size=5120', binPath].concat(args!))(done);
execTask('node', ['--max_old_space_size=5120', binPath].concat(args!), env)(done);
}
});
};
Expand Down

0 comments on commit c31f315

Please sign in to comment.