Skip to content
This repository was archived by the owner on Oct 14, 2020. It is now read-only.

Commit 2612516

Browse files
authored
Merge pull request #476 from kazk/feature/ts-target
[TypeScript] Make targets configurable
2 parents 5fa0aaf + 395fd7b commit 2612516

File tree

2 files changed

+89
-5
lines changed

2 files changed

+89
-5
lines changed

lib/runners/typescript.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ module.exports.run = function run(opts, cb) {
1111
// TODO: Support Setup Code
1212

1313
var solutionFile = util.codeWriteSync('typescript', opts.solution, dir, 'solution.ts', true);
14-
exec('tsc ' + solutionFile + ' --module commonjs', function(error, stdout, stderr) {
14+
exec(_tsc(opts) + ' ' + solutionFile, function(error, stdout, stderr) {
1515
if (error) return fail(error, stdout, stderr);
1616
runCode({name: 'node', args: [solutionFile.replace('.ts', '.js')]});
1717
});
@@ -52,13 +52,12 @@ module.exports.run = function run(opts, cb) {
5252
var code = opts.setup ? `${opts.setup}\n${opts.solution}` : opts.solution;
5353

5454
var codeFile = util.codeWriteSync('typescript', code, null, 'solution.ts', true);
55-
56-
exec('tsc --module commonjs ' + codeFile, function(error, stdout, stderr) {
55+
const cmd = _tsc(opts);
56+
exec(cmd + ' ' + codeFile, function(error, stdout, stderr) {
5757
if (error) return fail(error, stdout, stderr);
5858

5959
var specFile = util.codeWriteSync('typescript', opts.fixture, null, 'spec.ts', true);
60-
61-
exec('tsc --module commonjs ' + specFile, function(error, stdout, stderr) {
60+
exec(cmd + ' ' + specFile, function(error, stdout, stderr) {
6261
if (error) return fail(error, stdout, stderr);
6362
specFile = specFile.replace('.ts', '.js');
6463
runCode({name: 'mocha', 'args': ['-t', opts.timeout || 7000, '-u', interfaceType, '-R', 'mocha-reporter', specFile]});
@@ -149,3 +148,12 @@ module.exports.run = function run(opts, cb) {
149148
}
150149

151150
};
151+
152+
function _tsc(opts) {
153+
const m = (opts.languageVersion || '2.4/ES3').match(/ES(?:[356]|201[5-7]|Next)$/);
154+
return [
155+
'tsc',
156+
'--module', 'commonjs',
157+
'--target', m === null ? 'ES3' : m[0],
158+
].join(' ');
159+
}

test/runners/typescript_spec.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,4 +446,80 @@ describe('typescript runner', function() {
446446
});
447447
});
448448
});
449+
450+
describe('targets', function() {
451+
it('should support running with ES6 target', function(done) {
452+
runner.run({
453+
language: 'typescript',
454+
languageVersion: '2.4/ES6',
455+
solution: [
456+
`class Cube {`,
457+
` private _n: number;`,
458+
` constructor(n) {`,
459+
` this._n = n;`,
460+
` }`,
461+
` get volume(): number { return Math.pow(this._n, 3); }`,
462+
`}`,
463+
``,
464+
`const c = new Cube(2);`,
465+
`console.log(c.volume);`,
466+
].join('\n'),
467+
}, function(buffer) {
468+
expect(buffer.stdout).to.equal('8\n');
469+
done();
470+
});
471+
});
472+
473+
it('should default to ES3', function(done) {
474+
runner.run({
475+
language: 'typescript',
476+
solution: [
477+
`class Cube {`,
478+
` private _n: number;`,
479+
` constructor(n) {`,
480+
` this._n = n;`,
481+
` }`,
482+
` get volume(): number { return Math.pow(this._n, 3); }`,
483+
`}`,
484+
``,
485+
`const c = new Cube(2);`,
486+
`console.log(c.volume);`,
487+
].join('\n'),
488+
}, function(buffer) {
489+
expect(buffer.stdout).contains('Accessors are only available when targeting ECMAScript 5 and higher.');
490+
done();
491+
});
492+
});
493+
494+
it('should support testing with ES6 target', function(done) {
495+
runner.run({
496+
language: 'typescript',
497+
languageVersion: '2.4/ES6',
498+
testFramework: 'mocha_bdd',
499+
solution: [
500+
`export class Cube {`,
501+
` private _n: number;`,
502+
` constructor(n) {`,
503+
` this._n = n;`,
504+
` }`,
505+
` get volume(): number { return Math.pow(this._n, 3); }`,
506+
`}`,
507+
].join('\n'),
508+
fixture: [
509+
`/// <reference path="/runner/typings/mocha/index.d.ts" />`,
510+
`/// <reference path="/runner/typings/chai/index.d.ts" />`,
511+
`import {Cube} from "./solution";`,
512+
`import {assert} from "chai";`,
513+
`describe("Cube", function() {`,
514+
` it("should have volume getter", function() {`,
515+
` assert.equal(new Cube(2).volume, 8);`,
516+
` });`,
517+
`});`,
518+
].join('\n'),
519+
}, function(buffer) {
520+
expect(buffer.stdout).to.contain('<PASSED::>Passed');
521+
done();
522+
});
523+
});
524+
});
449525
});

0 commit comments

Comments
 (0)