Add this parameter types to Mocha callbacks. (#11905)

* Add this parameter types to Mocha callbacks.

* Fix chai-string to not use mocha context for storing variables.

* Allow to inject into a mocha context.
This commit is contained in:
Melvin Groenhoff
2016-12-20 20:55:04 +09:00
committed by Masahiro Wakame
parent 6b0942a8a6
commit 3c87910c0e
4 changed files with 120 additions and 88 deletions
+1 -1
View File
@@ -114,7 +114,7 @@ declare namespace bard {
* [strings] - same string array you'd use to set fn.$inject
* (...string) - string arguments turned into a string array
*/
function inject(context?: Function, ...args: string[]): void;
function inject(context?: Function | Mocha.IHookCallbackContext, ...args: string[]): void;
/**
* Write to console if bard debugging flag is on
+16 -14
View File
@@ -53,46 +53,48 @@ describe('chai-string', function() {
});
describe('tdd alias', function() {
let str: string;
let str2: string;
beforeEach(function() {
this.str = 'abcdef';
this.str2 = 'a\nb\tc\r d ef';
str = 'abcdef';
str2 = 'a\nb\tc\r d ef';
});
it('.startsWith', function() {
assert.startsWith(this.str, 'abc');
assert.startsWith(str, 'abc');
});
it('.notStartsWith', function() {
assert.notStartsWith(this.str, 'cba');
assert.notStartsWith(str, 'cba');
});
it('.endsWith', function() {
assert.endsWith(this.str, 'def');
assert.endsWith(str, 'def');
});
it('.notEndsWith', function() {
assert.notEndsWith(this.str, 'fed');
assert.notEndsWith(str, 'fed');
});
it('.equalIgnoreCase', function() {
assert.equalIgnoreCase(this.str, 'AbCdEf');
assert.equalIgnoreCase(str, 'AbCdEf');
});
it('.notEqualIgnoreCase', function() {
assert.notEqualIgnoreCase(this.str, 'abDDD');
assert.notEqualIgnoreCase(str, 'abDDD');
});
it('.equalIgnoreSpaces', function() {
assert.equalIgnoreSpaces(this.str, this.str2);
assert.equalIgnoreSpaces(str, str2);
});
it('.notEqualIgnoreSpaces', function() {
assert.notEqualIgnoreSpaces(this.str, this.str2 + 'g');
assert.notEqualIgnoreSpaces(str, str2 + 'g');
});
it('.singleLine', function() {
assert.singleLine(this.str);
assert.singleLine(str);
});
it('.notSingleLine', function() {
@@ -100,11 +102,11 @@ describe('chai-string', function() {
});
it('.reverseOf', function() {
assert.reverseOf(this.str, 'fedcba');
assert.reverseOf(str, 'fedcba');
});
it('.notReverseOf', function() {
assert.notReverseOf(this.str, 'aaaaa');
assert.notReverseOf(str, 'aaaaa');
});
it('.palindrome', function() {
@@ -114,7 +116,7 @@ describe('chai-string', function() {
});
it('.notPalindrome', function() {
assert.notPalindrome(this.str);
assert.notPalindrome(str);
});
it('.entriesCount', function() {
+51 -37
View File
@@ -49,22 +49,18 @@ interface MochaDone {
(error?: any): any;
}
interface ActionFunction {
(done: MochaDone): any | PromiseLike<any>
}
declare function setup(action: ActionFunction): void;
declare function teardown(action: ActionFunction): void;
declare function suiteSetup(action: ActionFunction): void;
declare function suiteTeardown(action: ActionFunction): void;
declare function before(action: ActionFunction): void;
declare function before(description: string, action: ActionFunction): void;
declare function after(action: ActionFunction): void;
declare function after(description: string, action: ActionFunction): void;
declare function beforeEach(action: ActionFunction): void;
declare function beforeEach(description: string, action: ActionFunction): void;
declare function afterEach(action: ActionFunction): void;
declare function afterEach(description: string, action: ActionFunction): void;
declare function setup(callback: (this: Mocha.IHookCallbackContext, done: MochaDone) => any): void;
declare function teardown(callback: (this: Mocha.IHookCallbackContext, done: MochaDone) => any): void;
declare function suiteSetup(callback: (this: Mocha.IHookCallbackContext, done: MochaDone) => any): void;
declare function suiteTeardown(callback: (this: Mocha.IHookCallbackContext, done: MochaDone) => any): void;
declare function before(callback: (this: Mocha.IHookCallbackContext, done: MochaDone) => any): void;
declare function before(description: string, callback: (this: Mocha.IHookCallbackContext, done: MochaDone) => any): void;
declare function after(callback: (this: Mocha.IHookCallbackContext, done: MochaDone) => any): void;
declare function after(description: string, callback: (this: Mocha.IHookCallbackContext, done: MochaDone) => any): void;
declare function beforeEach(callback: (this: Mocha.IHookCallbackContext, done: MochaDone) => any): void;
declare function beforeEach(description: string, callback: (this: Mocha.IHookCallbackContext, done: MochaDone) => any): void;
declare function afterEach(callback: (this: Mocha.IHookCallbackContext, done: MochaDone) => any): void;
declare function afterEach(description: string, callback: (this: Mocha.IHookCallbackContext, done: MochaDone) => any): void;
declare class Mocha {
currentTest: Mocha.ITestDefinition;
@@ -113,6 +109,24 @@ declare class Mocha {
// merge the Mocha class declaration with a module
declare namespace Mocha {
interface ISuiteCallbackContext {
timeout(ms: number): void;
retries(n: number): void;
slow(ms: number): void;
}
interface IHookCallbackContext {
skip(): void;
timeout(ms: number): void;
}
interface ITestCallbackContext {
skip(): void;
timeout(ms: number): void;
retries(n: number): void;
slow(ms: number): void;
}
/** Partial interface for Mocha's `Runnable` class. */
interface IRunnable {
title: string;
@@ -139,19 +153,19 @@ declare namespace Mocha {
}
/** Partial interface for Mocha's `Runner` class. */
interface IRunner {}
interface IRunner { }
interface IContextDefinition {
(description: string, spec: () => void): ISuite;
only(description: string, spec: () => void): ISuite;
skip(description: string, spec: () => void): void;
(description: string, callback: (this: ISuiteCallbackContext) => void): ISuite;
only(description: string, callback: (this: ISuiteCallbackContext) => void): ISuite;
skip(description: string, callback: (this: ISuiteCallbackContext) => void): void;
timeout(ms: number): void;
}
interface ITestDefinition {
(expectation: string, assertion?: ActionFunction): ITest;
only(expectation: string, assertion?: ActionFunction): ITest;
skip(expectation: string, assertion?: ActionFunction): void;
(expectation: string, callback?: (this: ITestCallbackContext, done: MochaDone) => any): ITest;
only(expectation: string, callback?: (this: ITestCallbackContext, done: MochaDone) => any): ITest;
skip(expectation: string, callback?: (this: ITestCallbackContext, done: MochaDone) => any): void;
timeout(ms: number): void;
state: "failed" | "passed";
}
@@ -169,18 +183,18 @@ declare namespace Mocha {
constructor(runner: IRunner);
}
export class Doc extends Base {}
export class Dot extends Base {}
export class HTML extends Base {}
export class HTMLCov extends Base {}
export class JSON extends Base {}
export class JSONCov extends Base {}
export class JSONStream extends Base {}
export class Landing extends Base {}
export class List extends Base {}
export class Markdown extends Base {}
export class Min extends Base {}
export class Nyan extends Base {}
export class Doc extends Base { }
export class Dot extends Base { }
export class HTML extends Base { }
export class HTMLCov extends Base { }
export class JSON extends Base { }
export class JSONCov extends Base { }
export class JSONStream extends Base { }
export class Landing extends Base { }
export class List extends Base { }
export class Markdown extends Base { }
export class Min extends Base { }
export class Nyan extends Base { }
export class Progress extends Base {
/**
* @param options.open String used to indicate the start of the progress bar.
@@ -195,8 +209,8 @@ declare namespace Mocha {
close?: string;
});
}
export class Spec extends Base {}
export class TAP extends Base {}
export class Spec extends Base { }
export class TAP extends Base { }
export class XUnit extends Base {
constructor(runner: IRunner, options?: any);
}
+52 -36
View File
@@ -1,5 +1,4 @@
function test_describe() {
describe('something', () => { });
@@ -7,7 +6,9 @@ function test_describe() {
describe.skip('something', () => { });
describe('something', function() {
describe('something', function () {
this.retries(3);
this.slow(1000);
this.timeout(2000);
});
}
@@ -19,7 +20,9 @@ function test_context() {
context.skip('some context', () => { });
context('some context', function() {
context('some context', function () {
this.retries(3);
this.slow(1000);
this.timeout(2000);
});
}
@@ -31,7 +34,9 @@ function test_suite() {
suite.skip('some context', () => { });
suite('some context', function() {
suite('some context', function () {
this.retries(3);
this.slow(1000);
this.timeout(2000);
});
}
@@ -47,6 +52,8 @@ function test_it() {
it.skip('does something', () => { });
it('does something', function () {
this.skip();
this.retries(3);
this.timeout(2000);
});
}
@@ -62,6 +69,8 @@ function test_test() {
test.skip('does something', () => { });
test('does something', function () {
this.skip();
this.retries(3);
this.timeout(2000);
});
}
@@ -77,6 +86,8 @@ function test_specify() {
specify.skip('does something', () => { });
specify('does something', function () {
this.skip();
this.retries(3);
this.timeout(2000);
});
}
@@ -89,6 +100,11 @@ function test_before() {
before("my description", () => { });
before("my description", done => { });
before("my description", function () {
this.skip();
this.timeout(2000);
});
}
function test_setup() {
@@ -145,59 +161,59 @@ function test_suiteTeardown() {
suiteTeardown((done) => { done(); });
}
function test_reporter_string(){
function test_reporter_string() {
mocha.reporter('html');
}
function test_reporter_function(){
mocha.reporter(function(){});
function test_reporter_function() {
mocha.reporter(function () { });
}
function test_setup_slow_option(){
mocha.setup({slow: 25});
function test_setup_slow_option() {
mocha.setup({ slow: 25 });
}
function test_setup_timeout_option(){
mocha.setup({timeout: 25});
function test_setup_timeout_option() {
mocha.setup({ timeout: 25 });
}
function test_setup_globals_option(){
mocha.setup({globals: ['mocha']});
function test_setup_globals_option() {
mocha.setup({ globals: ['mocha'] });
}
function test_setup_ui_option(){
mocha.setup({ui: 'bdd'});
function test_setup_ui_option() {
mocha.setup({ ui: 'bdd' });
}
function test_setup_reporter_string_option(){
mocha.setup({reporter: 'html'});
function test_setup_reporter_string_option() {
mocha.setup({ reporter: 'html' });
}
function test_setup_reporter_function_option(){
mocha.setup({reporter: function(){}});
function test_setup_reporter_function_option() {
mocha.setup({ reporter: function () { } });
}
function test_setup_bail_option(){
mocha.setup({bail: false});
function test_setup_bail_option() {
mocha.setup({ bail: false });
}
function test_setup_ignore_leaks_option(){
mocha.setup({ignoreLeaks: false});
function test_setup_ignore_leaks_option() {
mocha.setup({ ignoreLeaks: false });
}
function test_setup_grep_string_option(){
mocha.setup({grep: "describe"});
function test_setup_grep_string_option() {
mocha.setup({ grep: "describe" });
}
function test_setup_grep_regex_option(){
mocha.setup({grep: new RegExp('describe')});
function test_setup_grep_regex_option() {
mocha.setup({ grep: new RegExp('describe') });
}
function test_setup_grep_regex_literal_option(){
mocha.setup({grep: /(expect|should)/i });
function test_setup_grep_regex_literal_option() {
mocha.setup({ grep: /(expect|should)/i });
}
function test_setup_all_options(){
function test_setup_all_options() {
mocha.setup({
slow: 25,
timeout: 25,
@@ -210,20 +226,20 @@ function test_setup_all_options(){
});
}
function test_run(){
mocha.run(function(){})
function test_run() {
mocha.run(function () { })
}
function test_growl(){
function test_growl() {
mocha.growl();
}
function test_chaining(){
function test_chaining() {
mocha
.setup({slow:25})
.setup({ slow: 25 })
.growl()
.reporter('html')
.reporter(function(){});
.reporter(function () { });
}
import MochaDef = require('mocha');