11import { join } from 'path' ;
2- import { task , watch } from 'gulp' ;
2+ import { task } from 'gulp' ;
33import { buildConfig , sequenceTask } from '../../package-tools' ;
44
5- // There are no type definitions available for these imports.
6- const runSequence = require ( 'run-sequence' ) ;
7-
8- // Default Karma options.
9- const defaultOptions = {
10- configFile : join ( buildConfig . projectDir , 'test/karma.conf.js' ) ,
11- autoWatch : false ,
12- singleRun : false
13- } ;
14-
155/** Builds everything that is necessary for karma. */
166task ( ':test:build' , sequenceTask (
177 'clean' ,
@@ -32,60 +22,13 @@ task('test:single-run', [':test:build'], (done: () => void) => {
3222 // Load karma not outside. Karma pollutes Promise with a different implementation.
3323 const karma = require ( 'karma' ) ;
3424
35- new karma . Server ( { ...defaultOptions , singleRun : true } , ( exitCode : number ) => {
25+ new karma . Server ( {
26+ configFile : join ( buildConfig . projectDir , 'test/karma.conf.js' ) ,
27+ autoWatch : false ,
28+ singleRun : true
29+ } , ( exitCode : number ) => {
3630 // Immediately exit the process if Karma reported errors, because due to
3731 // potential still running tunnel-browsers gulp won't exit properly.
3832 exitCode === 0 ? done ( ) : process . exit ( exitCode ) ;
3933 } ) . start ( ) ;
4034} ) ;
41-
42- /**
43- * [Watch task] Runs the unit tests, rebuilding and re-testing when sources change.
44- * Does not inline resources.
45- *
46- * This task should be used when running unit tests locally.
47- */
48- task ( 'test' , [ ':test:build' ] , karmaWatchTask ( ) ) ;
49-
50- /**
51- * Runs a Karma server which will run the unit tests against any browser that connects to it.
52- * This is identical to `gulp test`, however it won't launch and manage Chrome automatically,
53- * which makes it convenient debugging unit tests against multiple different browsers.
54- */
55- task ( 'test:static' , [ ':test:build' ] , karmaWatchTask ( { browsers : [ ] } ) ) ;
56-
57- /**
58- * Returns a Gulp task that spawns a Karma server and reloads whenever the files change.
59- * Note that this doesn't use Karma's built-in file watching. Due to the way our build
60- * process is set up, Karma ends up firing its change detection for every file that is
61- * written to disk, which causes it to run tests multiple time and makes it hard to follow
62- * the console output. This approach runs the Karma server and then depends on the Gulp API
63- * to tell Karma when to run the tests.
64- * @param options Karma options to use on top of the defaults.
65- */
66- function karmaWatchTask ( options ?: any ) {
67- return ( ) => {
68- const patternRoot = join ( buildConfig . packagesDir , '**/*' ) ;
69- // Note: Karma shouldn't be required from the outside, because it
70- // pollutes the global Promise with a custom implementation.
71- const karma = require ( 'karma' ) ;
72-
73- // Configure the Karma server and override the autoWatch and singleRun just in case.
74- const server = new karma . Server ( { ...defaultOptions , ...options } ) ;
75-
76- // Refreshes Karma's file list and schedules a test run.
77- // Tests will only run if TypeScript compilation was successful.
78- const runTests = ( error ?: Error ) => {
79- if ( ! error ) {
80- server . refreshFiles ( ) . then ( ( ) => server . _injector . get ( 'executor' ) . schedule ( ) ) ;
81- }
82- } ;
83-
84- // Boot up the test server and run the tests whenever a new browser connects.
85- server . start ( ) ;
86- server . on ( 'browser_register' , ( ) => runTests ( ) ) ;
87-
88- // Whenever a file change has been recognized, rebuild and re-run the tests.
89- watch ( patternRoot + '.+(ts|scss|html)' , ( ) => runSequence ( ':test:build' , runTests ) ) ;
90- } ;
91- }
0 commit comments