Skip to content

Commit 1124da4

Browse files
committed
build: add karma task for easier cross-browser debugging
Adds the `gulp test:static` task that is identical to `gulp test`, however it doesn't launch Chrome automatically, which is convenient for debugging any browser that isn't Chrome. Currently the debugging flow for other browsers is running the Gulp task which launches Chrome, waiting for it to finish running the tests, connect the other browser, wait for the new browser to finish and then start debugging. With the new task you can just run the Gulp task and connect and disconnect only the relevant browsers.
1 parent 9673f63 commit 1124da4

File tree

1 file changed

+45
-29
lines changed

1 file changed

+45
-29
lines changed

tools/gulp/tasks/unit-test.ts

Lines changed: 45 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -35,39 +35,55 @@ task('test:single-run', [':test:build'], (done: () => void) => {
3535

3636
/**
3737
* [Watch task] Runs the unit tests, rebuilding and re-testing when sources change.
38-
* Does not inline resources. Note that this doesn't use Karma's built-in file
39-
* watching. Due to the way our build process is set up, Karma ends up firing
40-
* it's change detection for every file that is written to disk, which causes
41-
* it to run tests multiple time and makes it hard to follow the console output.
42-
* This approach runs the Karma server and then depends on the Gulp API to tell
43-
* Karma when to run the tests.
38+
* Does not inline resources.
4439
*
4540
* This task should be used when running unit tests locally.
4641
*/
47-
task('test', [':test:build'], () => {
48-
const patternRoot = join(packagesDir, '**/*');
49-
// Load karma not outside. Karma pollutes Promise with a different implementation.
50-
const karma = require('karma');
42+
task('test', [':test:build'], karmaWatchTask());
5143

52-
// Configure the Karma server and override the autoWatch and singleRun just in case.
53-
const server = new karma.Server({
54-
configFile: join(projectDir, 'test/karma.conf.js'),
55-
autoWatch: false,
56-
singleRun: false
57-
});
44+
/**
45+
* Runs a Karma server which will run the unit tests against any browser that connects to it.
46+
* This is identical to `gulp test`, however it won't launch and manage Chrome automatically,
47+
* which makes it convenient debugging unit tests against multiple different browsers.
48+
*/
49+
task('test:static', [':test:build'], karmaWatchTask({browsers: []}));
5850

59-
// Refreshes Karma's file list and schedules a test run.
60-
// Tests will only run if TypeScript compilation was successful.
61-
const runTests = (err?: Error) => {
62-
if (!err) {
63-
server.refreshFiles().then(() => server._injector.get('executor').schedule());
64-
}
65-
};
51+
/**
52+
* Returns a Gulp task that spawns a Karma server and reloads whenever the files change.
53+
* Note that this doesn't use Karma's built-in file watching. Due to the way our build
54+
* process is set up, Karma ends up firing it's change detection for every file that is
55+
* written to disk, which causes it to run tests multiple time and makes it hard to follow
56+
* the console output. This approach runs the Karma server and then depends on the Gulp API
57+
* to tell Karma when to run the tests.
58+
* @param overrides Optional Karma config overrides.
59+
*/
60+
function karmaWatchTask(overrides?: any) {
61+
return () => {
62+
const patternRoot = join(packagesDir, '**/*');
63+
// Note: Karma shouldn't be required from the outside, because it
64+
// pollutes the global Promise with a custom implementation.
65+
const karma = require('karma');
6666

67-
// Boot up the test server and run the tests whenever a new browser connects.
68-
server.start();
69-
server.on('browser_register', () => runTests());
67+
// Configure the Karma server and override the autoWatch and singleRun just in case.
68+
const server = new karma.Server({...({
69+
configFile: join(projectDir, 'test/karma.conf.js'),
70+
autoWatch: false,
71+
singleRun: false
72+
}), ...overrides});
7073

71-
// Whenever a file change has been recognized, rebuild and re-run the tests.
72-
watch(patternRoot + '.+(ts|scss|html)', () => runSequence(':test:build', runTests));
73-
});
74+
// Refreshes Karma's file list and schedules a test run.
75+
// Tests will only run if TypeScript compilation was successful.
76+
const runTests = (error?: Error) => {
77+
if (!error) {
78+
server.refreshFiles().then(() => server._injector.get('executor').schedule());
79+
}
80+
};
81+
82+
// Boot up the test server and run the tests whenever a new browser connects.
83+
server.start();
84+
server.on('browser_register', () => runTests());
85+
86+
// Whenever a file change has been recognized, rebuild and re-run the tests.
87+
watch(patternRoot + '.+(ts|scss|html)', () => runSequence(':test:build', runTests));
88+
};
89+
}

0 commit comments

Comments
 (0)