Skip to content

Commit

Permalink
Use mocha --compilers to test import runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
arv committed May 17, 2016
1 parent 584acdb commit 584d451
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ test/features: bin/traceur.js bin/traceur-runtime.js $(FEATURE_TESTS)
test/%-run: test/% bin/traceur.js
node_modules/.bin/mocha $(MOCHA_OPTIONS) $<

test/mocha: bin/traceur.js
node_modules/.bin/mocha $(MOCHA_OPTIONS) \
--compilers js:test/cjs-mocha-compiler.js --reporter dot test/feature/**

test/commonjs: test/commonjs-compiled
node_modules/.bin/mocha $(MOCHA_OPTIONS) test/node-commonjs-test.js

Expand Down
43 changes: 43 additions & 0 deletions test/cjs-mocha-compiler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
'use strict';

var fs = require('fs');
var traceurAPI = require('../src/node/api.js');

var org = require.extensions['.js'];
require.extensions['.js'] = function(module, path) {
if (!/\/node_modules\//.test(path)) {
var content = fs.readFileSync(path, 'utf8');
if (shouldSkip(path, content)) {
return;
}

var compiled = traceurAPI.compile(content, {
modules: 'commonjs',
importRuntime: true,
}, path, path);


if (needsWrapper(path)) {
var header = 'var assert = require("chai").assert, test = require("mocha").test;' + 'test("' + path + '", function(){';
var footer = '});';
compiled = header + compiled + footer;
}

try {
return module._compile(compiled, path);
} catch (ex) {
console.log(compiled, path);
throw ex;
}
}
return org(module, path);
};

function needsWrapper(path) {
return /\/test\/feature\//.test(path) && !/\/resources\//.test(path);
}

function shouldSkip(path, content) {
return /\/\/ (Error:|Options:|Skip.|Async.)/.test(content) ||
/\.script\.js$/.test(path);
}
2 changes: 2 additions & 0 deletions test/feature/Modules/ImportDefault.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {assert} from '../../asserts.js';

import x from './resources/default.js';
assert.equal(x, 42);

Expand Down
2 changes: 2 additions & 0 deletions test/feature/Modules/resources/default-class.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {assert} from '../../../asserts.js';

export default class C {
m() {
return 'm';
Expand Down
2 changes: 2 additions & 0 deletions test/feature/Modules/resources/default-function.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {assert} from '../../../asserts.js';

export default function f() {
return 123;
}
Expand Down
2 changes: 2 additions & 0 deletions test/feature/TemplateLiterals/resources/m.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@

import {assert} from '../../../asserts.js';
import {f} from './f.js';

assert.equal('a', (f `a`)[0][0]);
1 change: 1 addition & 0 deletions test/feature/TemplateLiterals/resources/n.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {assert} from '../../../asserts.js';
import {f} from './f.js';

assert.equal('b', (f `b`)[0][0]);

0 comments on commit 584d451

Please sign in to comment.