@@ -35,39 +35,55 @@ task('test:single-run', [':test:build'], (done: () => void) => {
35
35
36
36
/**
37
37
* [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.
44
39
*
45
40
* This task should be used when running unit tests locally.
46
41
*/
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 ( ) ) ;
51
43
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 : [ ] } ) ) ;
58
50
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' ) ;
66
66
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 } ) ;
70
73
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