From 2402e987f04b3bc5e6bcd1b770a357d1713473f4 Mon Sep 17 00:00:00 2001 From: Peter Burns Date: Tue, 20 Dec 2016 17:04:38 -0800 Subject: [PATCH] Add this.currentTest to setup and teardown --- mocha/index.d.ts | 19 +++++-- mocha/mocha-tests.ts | 129 +++++++++++++++++++++++++++++++++++++++---- 2 files changed, 130 insertions(+), 18 deletions(-) diff --git a/mocha/index.d.ts b/mocha/index.d.ts index 1ef3a772c9..9ac84fa097 100644 --- a/mocha/index.d.ts +++ b/mocha/index.d.ts @@ -49,18 +49,18 @@ interface MochaDone { (error?: any): any; } -declare function setup(callback: (this: Mocha.IHookCallbackContext, done: MochaDone) => any): void; -declare function teardown(callback: (this: Mocha.IHookCallbackContext, done: MochaDone) => any): void; +declare function setup(callback: (this: Mocha.IBeforeAndAfterContext, done: MochaDone) => any): void; +declare function teardown(callback: (this: Mocha.IBeforeAndAfterContext, 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 function beforeEach(callback: (this: Mocha.IBeforeAndAfterContext, done: MochaDone) => any): void; +declare function beforeEach(description: string, callback: (this: Mocha.IBeforeAndAfterContext, done: MochaDone) => any): void; +declare function afterEach(callback: (this: Mocha.IBeforeAndAfterContext, done: MochaDone) => any): void; +declare function afterEach(description: string, callback: (this: Mocha.IBeforeAndAfterContext, done: MochaDone) => any): void; declare class Mocha { currentTest: Mocha.ITestDefinition; @@ -120,6 +120,7 @@ declare namespace Mocha { timeout(ms: number): void; } + interface ITestCallbackContext { skip(): void; timeout(ms: number): void; @@ -148,10 +149,16 @@ declare namespace Mocha { interface ITest extends IRunnable { parent: ISuite; pending: boolean; + state: 'failed'|'passed'|undefined; fullTitle(): string; } + interface IBeforeAndAfterContext extends IHookCallbackContext { + currentTest: ITest; + } + + /** Partial interface for Mocha's `Runner` class. */ interface IRunner { } diff --git a/mocha/mocha-tests.ts b/mocha/mocha-tests.ts index 6bb80859cb..fc17a18221 100644 --- a/mocha/mocha-tests.ts +++ b/mocha/mocha-tests.ts @@ -1,3 +1,5 @@ +let boolean: boolean; +let string: string; function test_describe() { describe('something', () => { }); @@ -108,9 +110,26 @@ function test_before() { } function test_setup() { - setup(() => { }); + setup(function() { + boolean = this.currentTest.async; + boolean = this.currentTest.pending; + boolean = this.currentTest.sync; + boolean = this.currentTest.timedOut; + string = this.currentTest.title; + string = this.currentTest.fullTitle(); + string = this.currentTest.state; + }); - setup((done) => { done(); }); + setup(function (done) { + done(); + boolean = this.currentTest.async; + boolean = this.currentTest.pending; + boolean = this.currentTest.sync; + boolean = this.currentTest.timedOut; + string = this.currentTest.title; + string = this.currentTest.fullTitle(); + string = this.currentTest.state; + }); } function test_after() { @@ -124,19 +143,70 @@ function test_after() { } function test_teardown() { - teardown(() => { }); + teardown(function() { + boolean = this.currentTest.async; + boolean = this.currentTest.pending; + boolean = this.currentTest.sync; + boolean = this.currentTest.timedOut; + string = this.currentTest.title; + string = this.currentTest.fullTitle(); + string = this.currentTest.state; + }); - teardown((done) => { done(); }); + teardown(function(done) { + done(); + boolean = this.currentTest.async; + boolean = this.currentTest.pending; + boolean = this.currentTest.sync; + boolean = this.currentTest.timedOut; + string = this.currentTest.title; + string = this.currentTest.fullTitle(); + string = this.currentTest.state; + }); } function test_beforeEach() { - beforeEach(() => { }); + beforeEach(function () { + boolean = this.currentTest.async; + boolean = this.currentTest.pending; + boolean = this.currentTest.sync; + boolean = this.currentTest.timedOut; + string = this.currentTest.title; + string = this.currentTest.fullTitle(); + string = this.currentTest.state; + }); - beforeEach((done) => { done(); }); + beforeEach(function (done) { + done(); + boolean = this.currentTest.async; + boolean = this.currentTest.pending; + boolean = this.currentTest.sync; + boolean = this.currentTest.timedOut; + string = this.currentTest.title; + string = this.currentTest.fullTitle(); + string = this.currentTest.state; + }); - beforeEach("my description", () => { }); + beforeEach("my description", function() { + boolean = this.currentTest.async; + boolean = this.currentTest.pending; + boolean = this.currentTest.sync; + boolean = this.currentTest.timedOut; + string = this.currentTest.title; + string = this.currentTest.fullTitle(); + string = this.currentTest.state; + }); - beforeEach("my description", done => { }); + beforeEach("my description", function(done) { + done(); + boolean = this.currentTest.async; + boolean = this.currentTest.pending; + boolean = this.currentTest.sync; + boolean = this.currentTest.timedOut; + string = this.currentTest.title; + string = this.currentTest.fullTitle(); + string = this.currentTest.state; + }); } function test_suiteSetup() { @@ -146,13 +216,48 @@ function test_suiteSetup() { } function test_afterEach() { - afterEach(() => { }); + afterEach(function () { + boolean = this.currentTest.async; + boolean = this.currentTest.pending; + boolean = this.currentTest.sync; + boolean = this.currentTest.timedOut; + string = this.currentTest.title; + string = this.currentTest.fullTitle(); + string = this.currentTest.state; + }); - afterEach((done) => { done(); }); + afterEach(function (done) { + done(); + boolean = this.currentTest.async; + boolean = this.currentTest.pending; + boolean = this.currentTest.sync; + boolean = this.currentTest.timedOut; + string = this.currentTest.title; + string = this.currentTest.fullTitle(); + string = this.currentTest.state; + }); - afterEach("my description", () => { }); + afterEach("my description", function() { + boolean = this.currentTest.async; + boolean = this.currentTest.pending; + boolean = this.currentTest.sync; + boolean = this.currentTest.timedOut; + string = this.currentTest.title; + string = this.currentTest.fullTitle(); + string = this.currentTest.state; + }); + + afterEach("my description", function(done) { + done(); + boolean = this.currentTest.async; + boolean = this.currentTest.pending; + boolean = this.currentTest.sync; + boolean = this.currentTest.timedOut; + string = this.currentTest.title; + string = this.currentTest.fullTitle(); + string = this.currentTest.state; + }); - afterEach("my description", done => { }); } function test_suiteTeardown() {