From 52bf0def4e9f5202be1682e39a89472f931eba39 Mon Sep 17 00:00:00 2001 From: Marc Laval Date: Wed, 18 Mar 2015 00:43:30 +0100 Subject: [PATCH] chore(test): improve test.unit.cjs task Closes #998 --- DEVELOPER.md | 2 +- gulpfile.js | 31 +++++++++++++++++-- scripts/ci/test_unit_js.sh | 2 +- tools/transpiler/index.js | 10 +++--- .../transpiler/karma-traceur-preprocessor.js | 2 +- 5 files changed, 38 insertions(+), 9 deletions(-) diff --git a/DEVELOPER.md b/DEVELOPER.md index c6300b2ce2..7d2d80c05c 100644 --- a/DEVELOPER.md +++ b/DEVELOPER.md @@ -137,7 +137,7 @@ $(npm bin)/gulp clean 1. `$(npm bin)/gulp test.unit.js`: JS tests in a browser; runs in **watch mode** (i.e. karma watches the test files for changes and re-runs tests when files are updated). -2. `$(npm bin)/gulp test.unit.cjs`: JS tests in NodeJS (requires a build before) +2. `$(npm bin)/gulp test.unit.cjs`: JS tests in NodeJS; runs in **watch mode** 3. `$(npm bin)/gulp test.unit.dart`: Dart tests in Dartium; runs in **watch mode**. If you prefer running tests in "single-run" mode rather than watch mode use diff --git a/gulpfile.js b/gulpfile.js index 20007c54ff..5c1810ca0f 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -3,6 +3,8 @@ var gulpPlugins = require('gulp-load-plugins')(); var runSequence = require('run-sequence'); var madge = require('madge'); var merge = require('merge'); +var path = require('path'); + var gulpTraceur = require('./tools/transpiler/gulp-traceur'); var clean = require('./tools/build/clean'); @@ -16,6 +18,7 @@ var jsserve = require('./tools/build/jsserve'); var pubserve = require('./tools/build/pubserve'); var rundartpackage = require('./tools/build/rundartpackage'); var copy = require('./tools/build/copy'); +var file2moduleName = require('./tools/build/file2modulename'); var karma = require('karma').server; var minimist = require('minimist'); var es5build = require('./tools/build/es5build'); @@ -606,8 +609,32 @@ gulp.task('test.unit.dart/ci', function (done) { karma.start({configFile: __dirname + '/karma-dart.conf.js', singleRun: true, reporters: ['dots'], browsers: getBrowsersFromCLI()}, done); }); -gulp.task('test.unit.cjs', function (done) { - return gulp.src(CONFIG.test.js.cjs).pipe(jasmine(/*{verbose: true, includeStackTrace: true}*/)); +gulp.task('test.unit.cjs/ci', function () { + return gulp.src(CONFIG.test.js.cjs).pipe(jasmine({includeStackTrace: true, timeout: 1000})); +}); +gulp.task('test.unit.cjs', ['build.js.cjs'], function () { + //Run tests once + runSequence('test.unit.cjs/ci', function() {}); + //Watcher to transpile file changed + gulp.watch(CONFIG.transpile.src.js.concat(['modules/**/*.cjs']), function(event) { + var relPath = path.relative(__dirname, event.path).replace(/\\/g, "/"); + gulp.src(relPath) + .pipe(gulpPlugins.rename({extname: '.'+ 'js'})) + .pipe(util.insertSrcFolder(gulpPlugins, CONFIG.srcFolderInsertion.js)) + .pipe(gulpTraceur(CONFIG.transpile.options.js.cjs, file2moduleName)) + .pipe(transformCJSTests()) + .pipe(gulp.dest(CONFIG.dest.js.cjs + path.dirname(relPath.replace("modules", "")))); + }); + //Watcher to run tests when dist/js/cjs/angular2 is updated by the first watcher (after clearing the node cache) + gulp.watch(CONFIG.dest.js.cjs + '/angular2/**/*.js', function(event) { + for (var id in require.cache) { + if (id.replace(/\\/g, "/").indexOf(CONFIG.dest.js.cjs) > -1) { + delete require.cache[id]; + } + } + runSequence('test.unit.cjs/ci', function() {}); + }); + }); // ------------------ diff --git a/scripts/ci/test_unit_js.sh b/scripts/ci/test_unit_js.sh index 6d2e6e963d..5ea4eee013 100755 --- a/scripts/ci/test_unit_js.sh +++ b/scripts/ci/test_unit_js.sh @@ -10,4 +10,4 @@ cd $SCRIPT_DIR/../.. ./node_modules/.bin/gulp test.transpiler.unittest ./node_modules/.bin/gulp docs/test ./node_modules/.bin/gulp test.unit.js/ci --browsers=$KARMA_BROWSERS -./node_modules/.bin/gulp test.unit.cjs +./node_modules/.bin/gulp test.unit.cjs/ci diff --git a/tools/transpiler/index.js b/tools/transpiler/index.js index f3471ee8af..3e6366780b 100644 --- a/tools/transpiler/index.js +++ b/tools/transpiler/index.js @@ -23,9 +23,9 @@ exports.reloadSources = function() { needsReload = true; }; -exports.compile = function compile(options, paths, source) { +exports.compile = function compile(options, paths, source, reloadTraceur) { if (needsReload) { - reloadCompiler(); + reloadCompiler(reloadTraceur); needsReload = false; } var inputPath, outputPath, moduleName; @@ -73,8 +73,10 @@ exports.init = function() { // Transpile and evaluate the code in `src`. // Use existing traceur to compile our sources. -function reloadCompiler() { - loadModule(TRACEUR_PATH, false); +function reloadCompiler(reloadTraceur) { + if (reloadTraceur) { + loadModule(TRACEUR_PATH, false); + } glob.sync(__dirname + '/src/**/*.js').forEach(function(fileName) { loadModule(fileName, true); }); diff --git a/tools/transpiler/karma-traceur-preprocessor.js b/tools/transpiler/karma-traceur-preprocessor.js index efbf384170..49d8faa334 100644 --- a/tools/transpiler/karma-traceur-preprocessor.js +++ b/tools/transpiler/karma-traceur-preprocessor.js @@ -25,7 +25,7 @@ function createJs2DartPreprocessor(logger, basePath, config, emitter) { inputPath: file.originalPath, outputPath: file.path, moduleName: moduleName - }, content); + }, content, true); var transpiledContent = result.js; var sourceMap = result.sourceMap;