Skip to content

Commit c9e5322

Browse files
committed
Add unit tests to test typings and options
This part of the effort to create an official TypeScript compiler: Urigo/angular2-meteor#89 Urigo/angular2-meteor#90 Urigo/angular2-meteor#102
1 parent fcf4607 commit c9e5322

File tree

5 files changed

+82
-41
lines changed

5 files changed

+82
-41
lines changed

index.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,20 @@ exports.compile = function compile(source, options) {
4040
};
4141

4242
var validOptions = {
43-
"compilerOptions": "object",
44-
"filePath": "string",
45-
"moduleName": "string",
46-
"typings": "array"
43+
"compilerOptions": "Object",
44+
"filePath": "String",
45+
"moduleName": "String",
46+
"typings": "Array"
4747
};
4848
var validOptionsMsg = "Valid options are" +
4949
"compilerOptions, filePath, moduleName, and typings";
5050

51+
function checkType(option, optionName) {
52+
if (! option) return true;
53+
54+
return option.constructor.name === validOptions[optionName];
55+
}
56+
5157
function validateAndConvertOptions(options) {
5258
if (! options) return;
5359

@@ -59,7 +65,7 @@ function validateAndConvertOptions(options) {
5965
validOptionsMsg);
6066
}
6167

62-
if (typeof options[option] !== validOptions[option]) {
68+
if (! checkType(options[option], option)) {
6369
throw new Error(option + " should be of type " +
6470
validOptions[option]);
6571
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "meteor-typescript",
33
"author": "@barbatus",
4-
"version": "0.5.2",
4+
"version": "0.5.5",
55
"license": "MIT",
66
"description": "TypeScript wrapper package for use with Meteor",
77
"keywords": [

tests/ts.spec.js

Lines changed: 66 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,85 @@
11
var meteorTS = require("../index");
22

3-
describe("meteor-typescript", function() {
3+
describe("meteor-typescript -> ", function() {
44

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

7-
it("should compile with defaults", function() {
8-
var result = meteorTS.compile(testCodeLine);
9-
expect(result.code.indexOf("exports.foo")).toEqual(0);
10-
});
8+
it("should compile with defaults", function() {
9+
var result = meteorTS.compile(testCodeLine);
10+
expect(result.code.indexOf("exports.foo")).toEqual(0);
11+
});
1112

12-
it("should throw on wrong option", function() {
13-
var test = function() {
14-
meteorTS.compile(testCodeLine, {
15-
"wrong": true
16-
});
17-
};
18-
expect(test).toThrow();
19-
});
13+
it("should throw on wrong option", function() {
14+
var test = function() {
15+
meteorTS.compile(testCodeLine, {
16+
"wrong": true
17+
});
18+
};
19+
expect(test).toThrow();
20+
});
2021

21-
it("should recognize preset options", function() {
22-
var result = meteorTS.compile(testCodeLine, {
23-
compilerOptions: {
24-
module: "system"
25-
}
22+
it("should allow null options", function() {
23+
var result = meteorTS.compile(testCodeLine, {
24+
compilerOptions: null,
25+
typings: undefined
26+
});
27+
expect(result.code).not.toBeNull();
2628
});
27-
expect(result.code.indexOf("System.register")).toEqual(0);
28-
});
2929

30-
it("should add module with moduleName name when moduleName is set",
31-
function() {
30+
it("should recognize preset options", function() {
3231
var result = meteorTS.compile(testCodeLine, {
3332
compilerOptions: {
3433
module: "system"
35-
},
36-
moduleName: "fooModule"
34+
}
3735
});
38-
expect(result.code.indexOf("System.register(\"fooModule\""))
39-
.toEqual(0);
36+
expect(result.code).toContain("System.register");
4037
});
4138

42-
it("should throw on wrong compiler option", function() {
43-
var test = function() {
44-
meteorTS.compile(testCodeLine, {
39+
it("should add module with moduleName name when moduleName is set",
40+
function() {
41+
var result = meteorTS.compile(testCodeLine, {
4542
compilerOptions: {
46-
module: "wrong"
47-
}
43+
module: "system"
44+
},
45+
moduleName: "fooModule"
46+
});
47+
expect(result.code.indexOf("System.register(\"fooModule\""))
48+
.toEqual(0);
4849
});
49-
};
50-
expect(test).toThrow();
50+
51+
it("should throw on wrong compiler option", function() {
52+
var test = function() {
53+
meteorTS.compile(testCodeLine, {
54+
compilerOptions: {
55+
module: "wrong"
56+
}
57+
});
58+
};
59+
expect(test).toThrow();
60+
});
61+
});
62+
63+
describe("testing diagnostics and typings -> ", function() {
64+
var codeLineWithImport = "import {api} from 'lib'; export const foo = 'foo'";
65+
66+
it("should contain a semantic error when some module undefined", function() {
67+
var result = meteorTS.compile(codeLineWithImport);
68+
69+
expect(result.diagnostics.semanticErrors).not.toBeNull();
70+
expect(result.diagnostics.semanticErrors.length).toEqual(1);
71+
var error = result.diagnostics.semanticErrors[0].message;
72+
expect(error).toContain("Cannot find module 'lib'");
73+
});
74+
75+
it("declaration file with module declaration should remove an error", function() {
76+
var result = meteorTS.compile(codeLineWithImport, {
77+
typings: ["typings/lib.d.ts"]
78+
});
79+
80+
expect(result.diagnostics.semanticErrors).not.toBeNull();
81+
expect(result.diagnostics.semanticErrors.length).toEqual(0);
82+
});
5183
});
5284

5385
});

tests/typings/lib.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
declare module "lib" {
2+
export let api = {};
3+
}

typescript.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ exports.compile = function compile(fileContent, options) {
3232
var compilerHost = _.extend({}, defaultHost, customHost);
3333
var fileNames = [filePath];
3434
if (options.typings) {
35-
fileNames.concat(options.typings);
35+
fileNames = fileNames.concat(options.typings);
3636
}
3737
var program = ts.createProgram(fileNames, compilerOptions, compilerHost);
3838

0 commit comments

Comments
 (0)