Skip to content

Commit ff74d2b

Browse files
nicknisiblakeembrey
authored andcommitted
Convert unit tests to TypeScript (#437)
1 parent 181a86d commit ff74d2b

File tree

99 files changed

+963
-931
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

99 files changed

+963
-931
lines changed

examples/basic/src/generics.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* @param value A generic parameter.
66
* @returns A generic return value.
77
*/
8-
function test<T>(value:T):T {
8+
function testFunction<T>(value:T):T {
99
return value;
1010
}
1111

@@ -77,4 +77,4 @@ interface ABNumber extends AB<number> {}
7777
*/
7878
function getGenericArray():Array<string> {
7979
return [''];
80-
}
80+
}

gruntfile.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ module.exports = function(grunt)
3434
configuration: 'tslint.json'
3535
},
3636
files: {
37-
src: [ 'src/**/*.ts' ]
37+
src: [ 'src/**/*.ts', '!src/test/converter/**/*.ts' ]
3838
}
3939
},
4040
'string-replace': {
@@ -64,9 +64,20 @@ module.exports = function(grunt)
6464
}
6565
}
6666
},
67+
copy: {
68+
staticTestFiles: {
69+
expand: true,
70+
cwd: 'src',
71+
src: [
72+
'test/converter/**/*',
73+
'test/renderer/**/*'
74+
],
75+
dest: 'dist/'
76+
}
77+
},
6778
clean: {
68-
specsBefore: ['test/renderer/specs'],
69-
specsAfter: ['test/renderer/specs/assets']
79+
specsBefore: ['src/test/renderer/specs'],
80+
specsAfter: ['src/test/renderer/specs/assets']
7081
},
7182
watch: {
7283
source: {
@@ -76,7 +87,7 @@ module.exports = function(grunt)
7687
},
7788
mocha_istanbul: {
7889
coverage: {
79-
src: 'test',
90+
src: 'dist/test',
8091
options: {
8192
mask: '*.js',
8293
timeout: 4000
@@ -89,20 +100,21 @@ module.exports = function(grunt)
89100
grunt.loadNpmTasks('grunt-contrib-watch');
90101
grunt.loadNpmTasks('grunt-string-replace');
91102
grunt.loadNpmTasks('grunt-contrib-clean');
103+
grunt.loadNpmTasks('grunt-contrib-copy');
92104
grunt.loadNpmTasks('grunt-ts');
93105
grunt.loadNpmTasks('grunt-tslint');
94106
grunt.loadNpmTasks('grunt-mocha-istanbul');
95107

96108
grunt.registerTask('default', ['tslint', 'ts:typedoc', 'string-replace:version']);
97-
grunt.registerTask('build_and_test', ['default', 'specs', 'mocha_istanbul:coverage']);
109+
grunt.registerTask('build_and_test', ['default', 'specs', 'copy', 'mocha_istanbul:coverage']);
98110
grunt.registerTask('specs', ['clean:specsBefore', 'build-specs', 'clean:specsAfter']);
99111

100112
grunt.registerTask('build-specs', function() {
101113
var FS = require('fs-extra');
102114
var Path = require('path');
103115
var TypeDoc = require('./');
104116

105-
var base = Path.join(__dirname, 'test', 'converter');
117+
var base = Path.join(__dirname, 'src', 'test', 'converter');
106118
var app = new TypeDoc.Application({
107119
mode: 'Modules',
108120
target: 'ES5',
@@ -127,7 +139,7 @@ module.exports = function(grunt)
127139
});
128140

129141
var src = Path.join(__dirname, 'examples', 'basic', 'src');
130-
var out = Path.join(__dirname, 'test', 'renderer', 'specs');
142+
var out = Path.join(__dirname, 'src', 'test', 'renderer', 'specs');
131143

132144
FS.removeSync(out);
133145
app.generateDocs(app.expandInputFiles([src]), out);

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,11 @@
4949
"typescript": "2.1.6"
5050
},
5151
"devDependencies": {
52+
"@types/mocha": "^2.2.39",
5253
"grunt": "^1.0.1",
5354
"grunt-cli": "^1.2.0",
5455
"grunt-contrib-clean": "^1.0.0",
56+
"grunt-contrib-copy": "^1.0.0",
5557
"grunt-contrib-watch": "^1.0.0",
5658
"grunt-mocha-istanbul": "^5.0.1",
5759
"grunt-string-replace": "^1.2.0",
@@ -64,11 +66,12 @@
6466
"files": [
6567
"bin",
6668
"dist",
69+
"!dist/test",
6770
"tasks",
6871
"LICENSE"
6972
],
7073
"scripts": {
71-
"test": "mocha -t 4000",
74+
"test": "mocha -t 4000 dist/test",
7275
"build": "grunt build_and_test",
7376
"prepublish": "npm run build"
7477
},

src/lib/utils/events.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -326,8 +326,11 @@ export class EventDispatcher {
326326
* Bind an event to a `callback` function. Passing `"all"` will bind
327327
* the callback to all events fired.
328328
*/
329-
on(name: EventMap|string, callback: EventCallback, context?: any, priority?: number) {
330-
this.internalOn(name, callback, context, priority);
329+
on(eventMap: EventMap, context?: any);
330+
on(eventMap: EventMap, callback?: EventCallback, context?: any, priority?: number);
331+
on(name: string, callback: EventCallback, context?: any, priority?: number);
332+
on(nameOrMap: EventMap|string, callback: EventCallback, context?: any, priority?: number) {
333+
this.internalOn(nameOrMap, callback, context, priority);
331334
return this;
332335
}
333336

@@ -354,7 +357,9 @@ export class EventDispatcher {
354357
* are passed in using the space-separated syntax, the handler will fire
355358
* once for each event, not once for a combination of all events.
356359
*/
357-
once(name: EventMap|string, callback: EventCallback, context?: any, priority?: number) {
360+
once(eventMap: EventMap, context?: any);
361+
once(name: string, callback: EventCallback, context?: any, priority?: any);
362+
once(name: EventMap|string, callback?: EventCallback, context?: any, priority?: number) {
358363
// Map the event into a `{event: once}` object.
359364
const events = eventsApi(onceMap, <EventMap> {}, name, callback, _.bind(this.off, this));
360365
return this.on(events, void 0, context, priority);
@@ -366,7 +371,10 @@ export class EventDispatcher {
366371
* callbacks for the event. If `name` is null, removes all bound
367372
* callbacks for all events.
368373
*/
369-
off(name: EventMap|string, callback: EventCallback, context?: any) {
374+
off();
375+
off(eventMap: EventMap, context?: any);
376+
off(name: string, callback?: EventCallback, context?: any);
377+
off(name?: EventMap|string, callback?: EventCallback, context?: any) {
370378
if (!this._events) {
371379
return this;
372380
}
@@ -413,7 +421,9 @@ export class EventDispatcher {
413421
/**
414422
* Inversion-of-control versions of `once`.
415423
*/
416-
listenToOnce(obj: EventDispatcher, name: EventMap|string, callback: EventCallback, priority?: number) {
424+
listenToOnce(obj: EventDispatcher, eventMap: EventMap);
425+
listenToOnce(obj: EventDispatcher, name: string, callback: EventCallback, priority?: number);
426+
listenToOnce(obj: EventDispatcher, name: EventMap|string, callback?: EventCallback, priority?: number) {
417427
// Map the event into a `{event: once}` object.
418428
const events = eventsApi(onceMap, <EventMap> {}, name, callback, _.bind(this.stopListening, this, obj));
419429
return this.listenTo(obj, events, void 0, priority);

test/converter.js renamed to src/test/converter.ts

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
1-
var TypeDoc = require("../");
2-
var FS = require('fs');
3-
var Path = require('path');
4-
var Assert = require("assert");
1+
import { Application, resetReflectionID, normalizePath, ProjectReflection } from '..';
2+
import * as FS from 'fs';
3+
import * as Path from 'path';
4+
import Assert = require('assert');
55

6-
7-
function compareReflections(fixture, spec, path) {
8-
var key;
6+
function compareReflections(fixture, spec, path?: string) {
97
path = (path ? path + '/' : '') + spec.name;
108
Assert.deepEqual(fixture, spec);
119

12-
for (key in spec) {
13-
if (!spec.hasOwnProperty(key)) continue;
10+
for (let key in spec) {
11+
if (!spec.hasOwnProperty(key)) {
12+
continue;
13+
}
1414
Assert(fixture.hasOwnProperty(key), path + ': Missing property "' + key + '"');
1515
}
1616

17-
for (key in fixture) {
18-
if (!fixture.hasOwnProperty(key) || typeof fixture[key] == 'undefined') continue;
17+
for (let key in fixture) {
18+
if (!fixture.hasOwnProperty(key) || typeof fixture[key] === 'undefined') {
19+
continue;
20+
}
1921
Assert(spec.hasOwnProperty(key), path + ': Unknown property "' + key + '"');
2022

21-
var a = fixture[key];
22-
var b = spec[key];
23+
const a = fixture[key];
24+
const b = spec[key];
2325
Assert(a instanceof Object === b instanceof Object, path + ': Property "' + key + '" type mismatch');
2426

2527
if (a instanceof Object) {
@@ -43,26 +45,24 @@ function compareReflections(fixture, spec, path) {
4345
}
4446
}
4547

46-
4748
function compareChildren(fixture, spec, path) {
48-
var a = fixture.map(function(child) { return child.id; });
49-
var b = spec.map(function(child) { return child.id; });
49+
const a = fixture.map(function(child) { return child.id; });
50+
const b = spec.map(function(child) { return child.id; });
5051

51-
Assert(a.length == b.length, path + ': Number of children differs');
52+
Assert(a.length === b.length, path + ': Number of children differs');
5253
Assert(a.every(function(u, i) { return u === b[i]; }), path + ': Children are different');
5354

5455
fixture.forEach(function(a, index) {
5556
compareReflections(a, spec[index], path);
5657
});
5758
}
5859

59-
6060
describe('Converter', function() {
61-
var base = Path.join(__dirname, 'converter');
62-
var app;
61+
const base = Path.join(__dirname, 'converter');
62+
let app: Application;
6363

6464
it('constructs', function() {
65-
app = new TypeDoc.Application({
65+
app = new Application({
6666
mode: 'Modules',
6767
logger: 'none',
6868
target: 'ES5',
@@ -73,22 +73,24 @@ describe('Converter', function() {
7373
});
7474

7575
FS.readdirSync(base).forEach(function (directory) {
76-
var path = Path.join(base, directory);
77-
if (!FS.lstatSync(path).isDirectory()) return;
76+
const path = Path.join(base, directory);
77+
if (!FS.lstatSync(path).isDirectory()) {
78+
return;
79+
}
7880

7981
describe(directory, function() {
80-
var result;
82+
let result: ProjectReflection;
8183

8284
it('converts fixtures', function() {
83-
TypeDoc.resetReflectionID();
85+
resetReflectionID();
8486
result = app.convert(app.expandInputFiles([path]));
85-
Assert(result instanceof TypeDoc.ProjectReflection, 'No reflection returned');
87+
Assert(result instanceof ProjectReflection, 'No reflection returned');
8688
});
8789

8890
it('matches specs', function() {
89-
var specs = JSON.parse(FS.readFileSync(Path.join(path, 'specs.json')));
90-
var data = JSON.stringify(result.toObject(), null, ' ');
91-
data = data.split(TypeDoc.normalizePath(base)).join('%BASE%');
91+
const specs = JSON.parse(FS.readFileSync(Path.join(path, 'specs.json')).toString());
92+
let data = JSON.stringify(result.toObject(), null, ' ');
93+
data = data.split(normalizePath(base)).join('%BASE%');
9294

9395
compareReflections(JSON.parse(data), specs);
9496
});
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)