Skip to content

Commit

Permalink
Add unit tests to test typings and options
Browse files Browse the repository at this point in the history
This part of the effort to create an official TypeScript compiler:
Urigo/angular2-meteor#89
Urigo/angular2-meteor#90
Urigo/angular2-meteor#102
  • Loading branch information
barbatus committed Feb 15, 2016
1 parent fcf4607 commit c9e5322
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 41 deletions.
16 changes: 11 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,20 @@ exports.compile = function compile(source, options) {
};

var validOptions = {
"compilerOptions": "object",
"filePath": "string",
"moduleName": "string",
"typings": "array"
"compilerOptions": "Object",
"filePath": "String",
"moduleName": "String",
"typings": "Array"
};
var validOptionsMsg = "Valid options are" +
"compilerOptions, filePath, moduleName, and typings";

function checkType(option, optionName) {
if (! option) return true;

return option.constructor.name === validOptions[optionName];
}

function validateAndConvertOptions(options) {
if (! options) return;

Expand All @@ -59,7 +65,7 @@ function validateAndConvertOptions(options) {
validOptionsMsg);
}

if (typeof options[option] !== validOptions[option]) {
if (! checkType(options[option], option)) {
throw new Error(option + " should be of type " +
validOptions[option]);
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "meteor-typescript",
"author": "@barbatus",
"version": "0.5.2",
"version": "0.5.5",
"license": "MIT",
"description": "TypeScript wrapper package for use with Meteor",
"keywords": [
Expand Down
100 changes: 66 additions & 34 deletions tests/ts.spec.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,85 @@
var meteorTS = require("../index");

describe("meteor-typescript", function() {
describe("meteor-typescript -> ", function() {

var testCodeLine = "export const foo = 'foo'";
describe("testing exports and options -> ", function() {
var testCodeLine = "export const foo = 'foo'";

it("should compile with defaults", function() {
var result = meteorTS.compile(testCodeLine);
expect(result.code.indexOf("exports.foo")).toEqual(0);
});
it("should compile with defaults", function() {
var result = meteorTS.compile(testCodeLine);
expect(result.code.indexOf("exports.foo")).toEqual(0);
});

it("should throw on wrong option", function() {
var test = function() {
meteorTS.compile(testCodeLine, {
"wrong": true
});
};
expect(test).toThrow();
});
it("should throw on wrong option", function() {
var test = function() {
meteorTS.compile(testCodeLine, {
"wrong": true
});
};
expect(test).toThrow();
});

it("should recognize preset options", function() {
var result = meteorTS.compile(testCodeLine, {
compilerOptions: {
module: "system"
}
it("should allow null options", function() {
var result = meteorTS.compile(testCodeLine, {
compilerOptions: null,
typings: undefined
});
expect(result.code).not.toBeNull();
});
expect(result.code.indexOf("System.register")).toEqual(0);
});

it("should add module with moduleName name when moduleName is set",
function() {
it("should recognize preset options", function() {
var result = meteorTS.compile(testCodeLine, {
compilerOptions: {
module: "system"
},
moduleName: "fooModule"
}
});
expect(result.code.indexOf("System.register(\"fooModule\""))
.toEqual(0);
expect(result.code).toContain("System.register");
});

it("should throw on wrong compiler option", function() {
var test = function() {
meteorTS.compile(testCodeLine, {
it("should add module with moduleName name when moduleName is set",
function() {
var result = meteorTS.compile(testCodeLine, {
compilerOptions: {
module: "wrong"
}
module: "system"
},
moduleName: "fooModule"
});
expect(result.code.indexOf("System.register(\"fooModule\""))
.toEqual(0);
});
};
expect(test).toThrow();

it("should throw on wrong compiler option", function() {
var test = function() {
meteorTS.compile(testCodeLine, {
compilerOptions: {
module: "wrong"
}
});
};
expect(test).toThrow();
});
});

describe("testing diagnostics and typings -> ", function() {
var codeLineWithImport = "import {api} from 'lib'; export const foo = 'foo'";

it("should contain a semantic error when some module undefined", function() {
var result = meteorTS.compile(codeLineWithImport);

expect(result.diagnostics.semanticErrors).not.toBeNull();
expect(result.diagnostics.semanticErrors.length).toEqual(1);
var error = result.diagnostics.semanticErrors[0].message;
expect(error).toContain("Cannot find module 'lib'");
});

it("declaration file with module declaration should remove an error", function() {
var result = meteorTS.compile(codeLineWithImport, {
typings: ["typings/lib.d.ts"]
});

expect(result.diagnostics.semanticErrors).not.toBeNull();
expect(result.diagnostics.semanticErrors.length).toEqual(0);
});
});

});
3 changes: 3 additions & 0 deletions tests/typings/lib.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare module "lib" {
export let api = {};
}
2 changes: 1 addition & 1 deletion typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ exports.compile = function compile(fileContent, options) {
var compilerHost = _.extend({}, defaultHost, customHost);
var fileNames = [filePath];
if (options.typings) {
fileNames.concat(options.typings);
fileNames = fileNames.concat(options.typings);
}
var program = ts.createProgram(fileNames, compilerOptions, compilerHost);

Expand Down

0 comments on commit c9e5322

Please sign in to comment.