import { join } from 'path' ; import { task , watch } from 'gulp' ; import { buildConfig , sequenceTask } from 'material2-build-tools' ; const runSequence = require ( 'run-sequence' ) ; const defaultOptions = { configFile : join ( buildConfig . projectDir , 'test/karma.conf.js' ) , autoWatch : false , singleRun : false } ; /** Builds everything that is necessary for karma. */ task ( ':test:build' , sequenceTask ( 'clean' , 'cdk:build-no-bundles' , 'material:build-no-bundles' , 'cdk-experimental:build-no-bundles' , 'material-experimental:build-no-bundles' , 'material-moment-adapter:build-no-bundles' ) ) ; /**
 * Runs the unit tests. Does not watch for changes.
 * This task should be used when running tests on the CI server.
 */ task ( 'test:single-run' , [ ':test:build' ] , ( done ) => { const karma = require ( 'karma' ) ; new karma . Server ( { ... defaultOptions , singleRun : true } , ( exitCode ) => { exitCode === 0 ? done ( ) : process . exit ( exitCode ) ; } ) . start ( ) ; } ) ; /**
 * [Watch task] Runs the unit tests, rebuilding and re-testing when sources change.
 * Does not inline resources.
 *
 * This task should be used when running unit tests locally.
 */ task ( 'test' , [ ':test:build' ] , karmaWatchTask ( ) ) ; /**
 * Runs a Karma server which will run the unit tests against any browser that connects to it.
 * This is identical to `gulp test`, however it won't launch and manage Chrome automatically,
 * which makes it convenient debugging unit tests against multiple different browsers.
 */ task ( 'test:static' , [ ':test:build' ] , karmaWatchTask ( { browsers : [ ] } ) ) ; param overrides function karmaWatchTask ( options ? ) { return ( ) => { const patternRoot = join ( buildConfig . packagesDir , '**/*' ) ; const karma = require ( 'karma' ) ; const server = new karma . Server ( { ... defaultOptions , ... options } ) ; const runTests = ( error ? ) => { if ( ! error ) { server . refreshFiles ( ) . then ( ( ) => server . _injector . get ( 'executor' ) . schedule ( ) ) ; } } ; server . start ( ) ; server . on ( 'browser_register' , ( ) => runTests ( ) ) ; watch ( patternRoot + '.+(ts|scss|html)' , ( ) => runSequence ( ':test:build' , runTests ) ) ; } ; }