Skip to content

Commit

Permalink
chore: karma with JS, Dart
Browse files Browse the repository at this point in the history
Note: karma with dart is still not working
because of how `karma-dart` loads `package:…` dependencies.

Usage:
```
karma start karma-js.conf.js
karma start karma-dart.conf.js
```

Make sure to set `DARTIUM_BIN` env variable.

Refactors `js2dart`:
- live outside of the traceur module (`tools/js2dart/index.js`)
  so it can be reused by gulp and karma
- automatically build the sources in memory,
  so that `js2dart` can be used without running `gulp build` first
- provide a way to specify the moduleName of a compilation run
  independently of the input filename. This helps error messages
  and source maps (not yet enabled) to report the correct file name

Changes project setup:
- add module `test_lib` that contains the primitives for tests
  (e.g. `describe`, `it`, …)
- clean up some sources that had errors in them
- module names in transpiled js and dart files don’t contain
  `lib`, `test` nor `src` any more (e.g. `di/di`).
  • Loading branch information
vojtajina authored and tbosch committed Sep 26, 2014
1 parent 6335fc4 commit c3b442e
Show file tree
Hide file tree
Showing 26 changed files with 335 additions and 235 deletions.
48 changes: 21 additions & 27 deletions gulpfile.js
Expand Up @@ -21,19 +21,27 @@ var js2es5Options = {
script: false, // parse as a module
modules: 'register',
typeAssertionModule: 'assert',
typeAssertions: true,
moduleName: true
typeAssertions: true
};

var js2dartOptions = {
annotations: true, // parse annotations
types: true, // parse types
script: false, // parse as a module
outputLanguage: 'dart',
moduleName: true
outputLanguage: 'dart'
};

var traceur = require('./tools/js2dart/gulp-traceur');
var gulpTraceur = require('./tools/js2dart/gulp-traceur');

function resolveModuleName(fileName) {
var moduleName = fileName
.replace(/.*\/modules\//, '')
.replace(/\/src\//, '/')
.replace(/\/lib\//, '/')
.replace(/\/test\//, '/');
return moduleName;
}


// ---------
// rtts-assert and traceur runtime
Expand All @@ -45,9 +53,9 @@ gulp.task('jsRuntime/build', function() {
function createJsRuntimeTask(isWatch) {
var srcFn = isWatch ? watch : gulp.src.bind(gulp);
var rttsAssert = srcFn('tools/rtts-assert/src/assert.js')
.pipe(traceur(js2es5Options))
.pipe(gulpTraceur(js2es5Options, resolveModuleName))
.pipe(gulp.dest('build/js'));
var traceurRuntime = srcFn('tools/js2dart/node_modules/traceur/bin/traceur-runtime.js')
var traceurRuntime = srcFn(gulpTraceur.RUNTIME_PATH)
.pipe(gulp.dest('build/js'));
return mergeStreams(rttsAssert, traceurRuntime);
}
Expand All @@ -57,7 +65,7 @@ function createJsRuntimeTask(isWatch) {
var sourceTypeConfigs = {
dart: {
compiler: function() {
return traceur(js2dartOptions, true);
return gulpTraceur(js2dartOptions, resolveModuleName);
},
transpileSrc: ['modules/**/*.js'],
htmlSrc: ['modules/*/src/**/*.html'],
Expand All @@ -78,7 +86,7 @@ var sourceTypeConfigs = {
},
js: {
compiler: function() {
return traceur(js2es5Options, true);
return gulpTraceur(js2es5Options, resolveModuleName);
},
transpileSrc: ['modules/**/*.js', 'modules/**/*.es6'],
htmlSrc: ['modules/*/src/**/*.html'],
Expand Down Expand Up @@ -174,26 +182,12 @@ gulp.task('serve', connect.server({

gulp.task('clean', ['js2dart/clean', 'modules/clean']);

gulp.task('build', function() {
return runSequence(
// sequential
'js2dart/build',
// parallel
['jsRuntime/build', 'modules/build.dart', 'modules/build.js']
);
});
gulp.task('build', ['jsRuntime/build', 'modules/build.dart', 'modules/build.js']);

gulp.task('watch', function() {
runSequence('js2dart/test/watch');
var js2dartWatch = watch(js2dartTasks.paths.js2dartSrc, function(_, done) {
runSequence(
// sequential
'js2dart/build', 'js2dart/test',
// parallel
['jsRuntime/build', 'modules/build.dart', 'modules/build.js'],
done);
});
// parallel is important as both streams are infinite!
runSequence(['js2dart/test/watch', 'js2dart/src/watch']);
var dartModuleWatch = createModuleTask(sourceTypeConfigs.dart, true);
var jsModuleWatch = createModuleTask(sourceTypeConfigs.js, true);
return mergeStreams(js2dartWatch, dartModuleWatch, jsModuleWatch, createJsRuntimeTask(true));
return mergeStreams(dartModuleWatch, jsModuleWatch, createJsRuntimeTask(true));
});
61 changes: 61 additions & 0 deletions karma-dart.conf.js
@@ -0,0 +1,61 @@
// Karma configuration
// Generated on Thu Sep 25 2014 11:52:02 GMT-0700 (PDT)

module.exports = function(config) {
config.set({

frameworks: ['dart-unittest'],

files: [
{pattern: 'packages/**/*.dart', included: false},
{pattern: 'modules/*/src/**/*.js', included: false},
{pattern: 'modules/*/test/**/*.js', included: true},
{pattern: 'modules/**/*.dart', included: false},
'packages/browser/dart.js'
],

karmaDartImports: {
guinness: 'package:guinness/guinness_html.dart'
},

preprocessors: {
'modules/**/*.js': ['traceur']
},
customFileHandlers: [{
urlRegex: /.*\/packages\/.*$/,
handler: function(request, response, fa, fb, basePath) {
var url = request.url;
var path = url.indexOf('?') > -1 ? url.substring(0, url.indexOf('?')) : url;
var contets = fs.readFileSync(basePath + path);
response.writeHead(200);
response.end(contets);
}
}],
traceurPreprocessor: {
options: {
outputLanguage: 'dart',
script: false,
modules: 'register',
types: true,
// typeAssertions: true,
// typeAssertionModule: 'assert',
annotations: true
},
resolveModuleName: function(fileName) {
var moduleName = fileName
.replace(/.*\/modules\//, '')
.replace(/\/src\//, '/')
.replace(/\/test\//, '/');
return moduleName;
},
transformPath: function(fileName) {
return fileName.replace('.js', '.dart');
}
},

browsers: ['Dartium']
});


config.plugins.push(require('./tools/js2dart/karma-traceur-preprocessor'));
};
50 changes: 50 additions & 0 deletions karma-js.conf.js
@@ -0,0 +1,50 @@
// Karma configuration
// Generated on Thu Sep 25 2014 11:52:02 GMT-0700 (PDT)

module.exports = function(config) {
config.set({

frameworks: ['jasmine'],

files: [
'node_modules/traceur/bin/traceur-runtime.js',
'./karma-mock-annotations.js',
'modules/**/test_lib/**/*.es6',
'modules/**/*.js',
'modules/**/*.es6',
'test-main.js'
],

preprocessors: {
'modules/**/*.js': ['traceur'],
'modules/**/*.es6': ['traceur']
},

traceurPreprocessor: {
options: {
outputLanguage: 'es5',
script: false,
modules: 'register',
types: true,
// TODO: turn this on!
// typeAssertions: true,
// typeAssertionModule: 'assert',
annotations: true
},
resolveModuleName: function(fileName) {
var moduleName = fileName
.replace(/.*\/modules\//, '')
.replace(/\/src\//, '/')
.replace(/\/test\//, '/');
return moduleName;
},
transformPath: function(fileName) {
return fileName.replace('.es6', '');
}
},

browsers: ['Chrome']
});

config.plugins.push(require('./tools/js2dart/karma-traceur-preprocessor'));
};
3 changes: 3 additions & 0 deletions karma-mock-annotations.js
@@ -0,0 +1,3 @@

// TODO: Remove these annotations in the JS traceur build as they are only needed in Dart
window.FIELD = function() {};
3 changes: 2 additions & 1 deletion modules/change_detection/pubspec.yaml
Expand Up @@ -3,4 +3,5 @@ environment:
sdk: '>=1.4.0'
dependencies:
dev_dependencies:
unittest: '>=0.10.1 <0.12.0'
test_lib:
path: ../test_lib
3 changes: 2 additions & 1 deletion modules/core/pubspec.yaml
Expand Up @@ -9,4 +9,5 @@ dependencies:
facade:
path: ../facade
dev_dependencies:
unittest: '>=0.10.1 <0.12.0'
test_lib:
path: ../test_lib
2 changes: 1 addition & 1 deletion modules/core/src/compiler/compiler.js
@@ -1,4 +1,4 @@
import {Future} from 'facade/lang';
import {Future, Type} from 'facade/lang';
import {Element} from 'facade/dom';
import {ProtoView} from '../view/proto_view';
import {TemplateLoader} from './template_loader';
Expand Down
5 changes: 3 additions & 2 deletions modules/core/test/compiler/compiler_spec.js
@@ -1,6 +1,7 @@
import {describe, id} from 'spec/spec';
import {describe, id} from 'test_lib/test_lib';
import {Compiler} from './compiler';

function main() {
export function main() {
describe('compiler', () => {
it('should hello', () => {
print('I am working');
Expand Down
3 changes: 2 additions & 1 deletion modules/di/pubspec.yaml
Expand Up @@ -5,4 +5,5 @@ dependencies:
facade:
path: ../facade
dev_dependencies:
unittest: '>=0.10.1 <0.12.0'
test_lib:
path: ../test_lib
3 changes: 3 additions & 0 deletions modules/di/src/key.js
@@ -0,0 +1,3 @@
export class Key {

}
1 change: 1 addition & 0 deletions modules/di/src/module.js
@@ -1,5 +1,6 @@
import {Type} from 'facade/lang';
import {Map, MapWrapper wraps Map} from 'facade/collection';
import {Key} from './key';

/// becouse we need to know when toValue was not set.
/// (it could be that toValue is set to null or undefined in js)
Expand Down
3 changes: 2 additions & 1 deletion modules/examples/pubspec.yaml
Expand Up @@ -3,4 +3,5 @@ environment:
sdk: '>=1.4.0'
dependencies:
dev_dependencies:
unittest: '>=0.10.1 <0.12.0'
test_lib:
path: ../test_lib
3 changes: 2 additions & 1 deletion modules/facade/pubspec.yaml
Expand Up @@ -3,4 +3,5 @@ environment:
sdk: '>=1.4.0'
dependencies:
dev_dependencies:
unittest: '>=0.10.1 <0.12.0'
test_lib:
path: ../test_lib
6 changes: 6 additions & 0 deletions modules/test_lib/pubspec.yaml
@@ -0,0 +1,6 @@
name: test_lib
environment:
sdk: '>=1.4.0'
dependencies:
dev_dependencies:
guinness: ">=0.1.5 <0.2.0"
1 change: 1 addition & 0 deletions modules/test_lib/src/test_lib.dart
@@ -0,0 +1 @@
export 'package:guinness/guinness.dart' show describe, it, beforeEach, afterEach, expect;
8 changes: 8 additions & 0 deletions modules/test_lib/src/test_lib.es6
@@ -0,0 +1,8 @@
export var describe = window.describe;
export var it = window.it;
export var beforeEach = window.beforeEach;
export var afterEach = window.afterEach;
export var expect = window.expect;

// To make testing consistent between dart and js
window.print = window.dump || window.console.log;
9 changes: 7 additions & 2 deletions package.json
Expand Up @@ -12,13 +12,18 @@
"gulp-rename": "^1.2.0",
"gulp-shell": "^0.2.9",
"gulp-watch": "^1.0.3",
"karma": "^0.12.23",
"karma-chrome-launcher": "^0.1.4",
"karma-dart": "^0.2.8",
"karma-jasmine": "^0.2.2",
"q": "^1.0.1",
"through2": "^0.6.1",
"event-stream": "^3.1.5",
"gulp-connect": "~1.0.5",
"gulp-rimraf": "^0.1.0",
"run-sequence": "^0.3.6",
"glob": "^4.0.6",
"gulp-ejs": "^0.3.1"
}
"gulp-ejs": "^0.3.1",
"traceur": "0.0.66"
}
}
6 changes: 5 additions & 1 deletion pubspec.yaml
Expand Up @@ -2,9 +2,13 @@ name: angular
version: 0.0.0
authors:
- Vojta Jina <vojta.jina@gmail.com>
description: Compile JavaScript to Dart so that you can compile it back to JavaScript and run.
description: Angular
environment:
sdk: '>=1.4.0'
dependencies:
dev_dependencies:
test_lib:
path: modules/test_lib
unittest: '>=0.10.1 <0.12.0'
guinness: ">=0.1.5 <0.2.0"
browser: '>=0.10.0 <0.11.0'
12 changes: 12 additions & 0 deletions test-main.js
@@ -0,0 +1,12 @@
var TEST_REGEXP = /^\/base\/modules\/[^\/]*\/test\/.*/;

Object.keys(window.__karma__.files).forEach(function(path) {
if (TEST_REGEXP.test(path)) {
var moduleName = path
.replace(/.*\/modules\//, '')
.replace(/\/src\//, '/')
.replace(/\/test\//, '/')
.replace(/\.\w*$/, '');
System.get(moduleName).main();
}
});

0 comments on commit c3b442e

Please sign in to comment.