Skip to content

Commit 839b92d

Browse files
Add first unit tests
1 parent 93a4672 commit 839b92d

File tree

4 files changed

+270
-1
lines changed

4 files changed

+270
-1
lines changed

lib/declarations.d.ts

+4
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,7 @@ interface IProcessInfo {
1717
*/
1818
exitCode?: number;
1919
}
20+
21+
interface IDictionary<T> {
22+
[key: string]: T
23+
}

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"main": "lib/index.js",
66
"types": "./typings/nativescript-doctor.d.ts",
77
"scripts": {
8-
"test": "echo \"Error: no test specified\" && exit 1"
8+
"test": "node_modules/.bin/istanbul cover node_modules/mocha/bin/_mocha"
99
},
1010
"repository": {
1111
"type": "git",

test/definitions/mocha.d.ts

+205
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
// Type definitions for mocha 2.2.5
2+
// Project: http://mochajs.org/
3+
// Definitions by: Kazi Manzur Rashid <https://github.com/kazimanzurrashid/>, otiai10 <https://github.com/otiai10>, jt000 <https://github.com/jt000>, Vadim Macagon <https://github.com/enlight>
4+
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
5+
6+
interface MochaSetupOptions {
7+
//milliseconds to wait before considering a test slow
8+
slow?: number;
9+
10+
// timeout in milliseconds
11+
timeout?: number;
12+
13+
// ui name "bdd", "tdd", "exports" etc
14+
ui?: string;
15+
16+
//array of accepted globals
17+
globals?: any[];
18+
19+
// reporter instance (function or string), defaults to `mocha.reporters.Spec`
20+
reporter?: any;
21+
22+
// bail on the first test failure
23+
bail?: boolean;
24+
25+
// ignore global leaks
26+
ignoreLeaks?: boolean;
27+
28+
// grep string or regexp to filter tests with
29+
grep?: any;
30+
}
31+
32+
declare var mocha: Mocha;
33+
declare var describe: Mocha.IContextDefinition;
34+
declare var xdescribe: Mocha.IContextDefinition;
35+
// alias for `describe`
36+
declare var context: Mocha.IContextDefinition;
37+
// alias for `describe`
38+
declare var suite: Mocha.IContextDefinition;
39+
declare var it: Mocha.ITestDefinition;
40+
declare var xit: Mocha.ITestDefinition;
41+
// alias for `it`
42+
declare var test: Mocha.ITestDefinition;
43+
declare var specify: Mocha.ITestDefinition;
44+
45+
interface MochaDone {
46+
(error?: any): any;
47+
}
48+
49+
interface ActionFunction {
50+
(done: MochaDone): any | PromiseLike<any>
51+
}
52+
53+
declare function setup(action: ActionFunction): void;
54+
declare function teardown(action: ActionFunction): void;
55+
declare function suiteSetup(action: ActionFunction): void;
56+
declare function suiteTeardown(action: ActionFunction): void;
57+
declare function before(action: ActionFunction): void;
58+
declare function before(description: string, action: ActionFunction): void;
59+
declare function after(action: ActionFunction): void;
60+
declare function after(description: string, action: ActionFunction): void;
61+
declare function beforeEach(action: ActionFunction): void;
62+
declare function beforeEach(description: string, action: ActionFunction): void;
63+
declare function afterEach(action: ActionFunction): void;
64+
declare function afterEach(description: string, action: ActionFunction): void;
65+
66+
declare class Mocha {
67+
currentTest: Mocha.ITestDefinition;
68+
constructor(options?: {
69+
grep?: RegExp;
70+
ui?: string;
71+
reporter?: string;
72+
timeout?: number;
73+
bail?: boolean;
74+
});
75+
76+
/** Setup mocha with the given options. */
77+
setup(options: MochaSetupOptions): Mocha;
78+
bail(value?: boolean): Mocha;
79+
addFile(file: string): Mocha;
80+
/** Sets reporter by name, defaults to "spec". */
81+
reporter(name: string): Mocha;
82+
/** Sets reporter constructor, defaults to mocha.reporters.Spec. */
83+
reporter(reporter: (runner: Mocha.IRunner, options: any) => any): Mocha;
84+
ui(value: string): Mocha;
85+
grep(value: string): Mocha;
86+
grep(value: RegExp): Mocha;
87+
invert(): Mocha;
88+
ignoreLeaks(value: boolean): Mocha;
89+
checkLeaks(): Mocha;
90+
/**
91+
* Function to allow assertion libraries to throw errors directly into mocha.
92+
* This is useful when running tests in a browser because window.onerror will
93+
* only receive the 'message' attribute of the Error.
94+
*/
95+
throwError(error: Error): void;
96+
/** Enables growl support. */
97+
growl(): Mocha;
98+
globals(value: string): Mocha;
99+
globals(values: string[]): Mocha;
100+
useColors(value: boolean): Mocha;
101+
useInlineDiffs(value: boolean): Mocha;
102+
timeout(value: number): Mocha;
103+
slow(value: number): Mocha;
104+
enableTimeouts(value: boolean): Mocha;
105+
asyncOnly(value: boolean): Mocha;
106+
noHighlighting(value: boolean): Mocha;
107+
/** Runs tests and invokes `onComplete()` when finished. */
108+
run(onComplete?: (failures: number) => void): Mocha.IRunner;
109+
}
110+
111+
// merge the Mocha class declaration with a module
112+
declare namespace Mocha {
113+
/** Partial interface for Mocha's `Runnable` class. */
114+
interface IRunnable {
115+
title: string;
116+
fn: Function;
117+
async: boolean;
118+
sync: boolean;
119+
timedOut: boolean;
120+
}
121+
122+
/** Partial interface for Mocha's `Suite` class. */
123+
interface ISuite {
124+
parent: ISuite;
125+
title: string;
126+
127+
fullTitle(): string;
128+
}
129+
130+
/** Partial interface for Mocha's `Test` class. */
131+
interface ITest extends IRunnable {
132+
parent: ISuite;
133+
pending: boolean;
134+
135+
fullTitle(): string;
136+
}
137+
138+
/** Partial interface for Mocha's `Runner` class. */
139+
interface IRunner { }
140+
141+
interface IContextDefinition {
142+
(description: string, spec: () => void): ISuite;
143+
only(description: string, spec: () => void): ISuite;
144+
skip(description: string, spec: () => void): void;
145+
timeout(ms: number): void;
146+
}
147+
148+
interface ITestDefinition {
149+
(expectation: string, assertion?: ActionFunction): ITest;
150+
only(expectation: string, assertion?: ActionFunction): ITest;
151+
skip(expectation: string, assertion?: ActionFunction): void;
152+
timeout(ms: number): void;
153+
state: "failed" | "passed";
154+
}
155+
156+
export module reporters {
157+
export class Base {
158+
stats: {
159+
suites: number;
160+
tests: number;
161+
passes: number;
162+
pending: number;
163+
failures: number;
164+
};
165+
166+
constructor(runner: IRunner);
167+
}
168+
169+
export class Doc extends Base { }
170+
export class Dot extends Base { }
171+
export class HTML extends Base { }
172+
export class HTMLCov extends Base { }
173+
export class JSON extends Base { }
174+
export class JSONCov extends Base { }
175+
export class JSONStream extends Base { }
176+
export class Landing extends Base { }
177+
export class List extends Base { }
178+
export class Markdown extends Base { }
179+
export class Min extends Base { }
180+
export class Nyan extends Base { }
181+
export class Progress extends Base {
182+
/**
183+
* @param options.open String used to indicate the start of the progress bar.
184+
* @param options.complete String used to indicate a complete test on the progress bar.
185+
* @param options.incomplete String used to indicate an incomplete test on the progress bar.
186+
* @param options.close String used to indicate the end of the progress bar.
187+
*/
188+
constructor(runner: IRunner, options?: {
189+
open?: string;
190+
complete?: string;
191+
incomplete?: string;
192+
close?: string;
193+
});
194+
}
195+
export class Spec extends Base { }
196+
export class TAP extends Base { }
197+
export class XUnit extends Base {
198+
constructor(runner: IRunner, options?: any);
199+
}
200+
}
201+
}
202+
203+
declare module "mocha" {
204+
export = Mocha;
205+
}

test/sys-info.ts

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import * as assert from "assert";
2+
import * as Promise from "bluebird";
3+
import * as path from "path";
4+
import { SysInfo } from "../lib/sys-info";
5+
import { ChildProcess } from "../lib/wrappers/child-process";
6+
7+
const JavaHomeName = "JAVA_HOME";
8+
9+
describe("SysInfo unit tests", () => {
10+
let sysInfo: SysInfo;
11+
let spawnFromEventCommand: string;
12+
let execCommand: string;
13+
14+
beforeEach(() => {
15+
let childProcess: ChildProcess = {
16+
spawnFromEvent: (command: string, args: string[], event: string) => {
17+
spawnFromEventCommand = `${command} ${args.join(" ")}`;
18+
return Promise.resolve({ stdout: "", stderr: "" });
19+
},
20+
exec: (command: string) => {
21+
execCommand = command;
22+
return Promise.resolve({ stdout: "", stderr: "" });
23+
}
24+
};
25+
26+
sysInfo = new SysInfo(childProcess);
27+
});
28+
29+
describe("Should execute correct commands to check for", () => {
30+
it("java version.", (done: MochaDone) => {
31+
sysInfo.getJavaVersion()
32+
.then(version => {
33+
assert.deepEqual(spawnFromEventCommand, "java -version");
34+
done();
35+
});
36+
});
37+
38+
it("java compiler version when there is JAVA_HOME.", (done: MochaDone) => {
39+
let pathToJavac = path.join(process.env[JavaHomeName], "bin", "javac");
40+
sysInfo.getJavaCompilerVersion()
41+
.then(version => {
42+
assert.deepEqual(execCommand, `"${pathToJavac}" -version`);
43+
done();
44+
});
45+
});
46+
47+
it("java compiler version when there is no JAVA_HOME.", (done: MochaDone) => {
48+
let originalJavaHome = process.env[JavaHomeName];
49+
50+
delete process.env[JavaHomeName];
51+
52+
sysInfo.getJavaCompilerVersion()
53+
.then(version => {
54+
assert.deepEqual(execCommand, `"javac" -version`);
55+
process.env[JavaHomeName] = originalJavaHome;
56+
done();
57+
});
58+
});
59+
});
60+
});

0 commit comments

Comments
 (0)