Skip to content

Commit

Permalink
Add Babel test case
Browse files Browse the repository at this point in the history
  • Loading branch information
mormahr committed Apr 8, 2018
1 parent 5704f78 commit fd9ad74
Show file tree
Hide file tree
Showing 6 changed files with 285 additions and 6 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
},
"devDependencies": {
"@types/express": "^4.11.0",
"babel-core": "^6.26.0",
"babel-plugin-transform-es2015-modules-commonjs": "^6.26.0",
"chai": "^4.0.0",
"eslint": "^4.0.0",
"express": "4.x",
Expand Down
35 changes: 35 additions & 0 deletions test/babel.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict';

var resolve = require('path').resolve;
var assert = require('chai').assert;
var spawnBabel = require('./util/launch-utils').spawnBabel;
var GET = require('./util/http-utils').GET;

describe('Babel', function() {
it('should run the example and respond', function(done) {
this.timeout(5000);
var js_file = resolve(__dirname, './test-resources/babel-base-case.js');
var target = spawnBabel(js_file);
var called = false;

target.stdout.on('data', function(data) {
if (data.toString().indexOf('START') === -1) {
return;
}

GET('/').then(function() {
called = true;
target.kill('SIGINT');
});
});

target.stderr.on('data', function(data) {
console.error(data.toString());
});

target.on('close', function() {
assert(called);
done();
});
});
});
6 changes: 6 additions & 0 deletions test/test-resources/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"parserOptions": {
"ecmaVersion": 2015,
"sourceType": "module"
}
}
12 changes: 12 additions & 0 deletions test/test-resources/babel-base-case.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import express from 'express';
import Router from '../../lib/express-promise-router.js';
const router = Router();

router.get('/', function(req, res) {
res.send('Hi!');
});

const app = express();
app.use(router);
app.listen(12345);
console.log('START');
11 changes: 11 additions & 0 deletions test/util/launch-utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var ts = require('typescript');
var babel = require('babel-core');
var fs = require('fs');
var spawn = require('child_process').spawn;
var dirname = require('path').dirname;
Expand Down Expand Up @@ -27,3 +28,13 @@ exports.spawnJavaScript = function spawnJavaScript(path) {

return spawnString(content, dirname(path));
};

exports.spawnBabel = function spawnBabel(path) {
var content = fs.readFileSync(path, 'utf-8');

var result = babel.transform(content, {
plugins: ['transform-es2015-modules-commonjs'],
});

return spawnString(result.code, dirname(path));
};
Loading

0 comments on commit fd9ad74

Please sign in to comment.