test.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. const fs = require('fs');
  2. const path = require('path');
  3. const glob = require('glob');
  4. const gulp = require('gulp');
  5. const webpack = require('webpack-stream');
  6. const KarmaServer = require('karma').Server;
  7. const karmaConfigPath = path.join(__dirname, '../karma.config.js');
  8. const webpackConfig = require('../webpack.tests.config');
  9. // NOTE: top-level JS files in tests/ will be executed first
  10. const TEST_GLOB = 'tests/*/*.{js,ts}';
  11. // runs a server, outputs a URL to visit.
  12. // expects dist to be compiled or watcher to be running.
  13. gulp.task('test', [ 'test:async-watch' ], function() {
  14. new KarmaServer({
  15. configFile: karmaConfigPath,
  16. singleRun: false,
  17. autoWatch: true
  18. }, function(exitCode) { // plays best with developing from command line
  19. process.exit(exitCode);
  20. }).start();
  21. });
  22. // runs headlessly and continuously, watching files.
  23. // expects dist to be compiled or watcher to be running.
  24. gulp.task('test:headless', [ 'test:async-watch' ], function() {
  25. new KarmaServer({
  26. configFile: karmaConfigPath,
  27. browsers: [ 'PhantomJS_custom' ],
  28. singleRun: false,
  29. autoWatch: true
  30. }, function(exitCode) { // plays best with developing from command line
  31. process.exit(exitCode);
  32. }).start();
  33. });
  34. // runs headlessly once, then exits
  35. gulp.task('test:single', [ 'webpack', 'test:sync-compile' ], function(done) {
  36. new KarmaServer({
  37. configFile: karmaConfigPath,
  38. browsers: [ 'PhantomJS_custom' ],
  39. singleRun: true,
  40. autoWatch: false
  41. }).on('run_complete', function(browsers, results) { // plays best with CI and other tasks
  42. done(results.exitCode);
  43. }).start();
  44. });
  45. // compilation
  46. gulp.task('test:sync-compile', [ 'test:index' ], function() {
  47. return createStream();
  48. });
  49. gulp.task('test:async-watch', [ 'test:watch-index' ], function() {
  50. createStream(true); // not returning stream causes task to ignore termination
  51. });
  52. gulp.task('test:watch-index', [ 'test:index' ], function() {
  53. return gulp.watch(TEST_GLOB, [ 'test:index' ]);
  54. });
  55. gulp.task('test:index', function() {
  56. let out = '';
  57. glob.sync(TEST_GLOB).forEach(function(path) {
  58. out += "import '../" + path + "';\n";
  59. });
  60. if (!fs.existsSync('tmp')) {
  61. fs.mkdirSync('tmp');
  62. }
  63. fs.writeFileSync('tmp/test-index.js', out, { encoding: 'utf8' });
  64. });
  65. function createStream(enableWatch) {
  66. return gulp.src('tmp/test-index.js')
  67. .pipe(
  68. webpack(Object.assign({}, webpackConfig, {
  69. devtool: 'source-map',
  70. watch: enableWatch || false,
  71. }))
  72. )
  73. .pipe(gulp.dest('tmp/'));
  74. }