-
Notifications
You must be signed in to change notification settings - Fork 780
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Core: Add
QUnit.hooks
to globally add beforeEach and afterEach
Ref #1475.
- Loading branch information
Showing
10 changed files
with
294 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
--- | ||
layout: page-api | ||
title: QUnit.hooks | ||
excerpt: Add global callbacks to run before or after each test. | ||
groups: | ||
- main | ||
version_added: "unreleased" | ||
--- | ||
|
||
`QUnit.hooks.beforeEach( callback )`<br> | ||
`QUnit.hooks.afterEach( callback )` | ||
|
||
Register a global callback to run before or after each test. | ||
|
||
| parameter | description | | ||
|-----------|-------------| | ||
| callback (function) | Callback to execute. Called with an [assert](../assert/index.md) argument. | | ||
|
||
This is the equivalent of applying a `QUnit.module()` hook to all modules and all tests, including global tests that are not associated with any module. | ||
|
||
Similar to module hooks, global hooks support async functions or returning a Promise, which will be waited for before QUnit continues executing tests. Each global hook also has access to the same `assert` object and test context as the [QUnit.test](./test.md) that the hook is running for. | ||
|
||
For more details about hooks, refer to [QUnit.module § Hooks](./module.md#hooks). | ||
|
||
## Examples | ||
|
||
```js | ||
QUnit.hooks.beforeEach( function() { | ||
this.app = new MyApp(); | ||
}); | ||
|
||
QUnit.hooks.afterEach( async function( assert ) { | ||
assert.deepEqual( [], await this.app.getErrors(), "MyApp errors" ); | ||
|
||
MyApp.reset(); | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import config from "./config"; | ||
|
||
function makeAddGlobalHook( hookName ) { | ||
return function addGlobalHook( callback ) { | ||
if ( !config.globalHooks[ hookName ] ) { | ||
config.globalHooks[ hookName ] = []; | ||
} | ||
config.globalHooks[ hookName ].push( callback ); | ||
}; | ||
} | ||
|
||
export const hooks = { | ||
beforeEach: makeAddGlobalHook( "beforeEach" ), | ||
afterEach: makeAddGlobalHook( "afterEach" ) | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
QUnit.hooks.beforeEach( function() { | ||
this.x = 1; | ||
this.fromGlobal = true; | ||
} ); | ||
QUnit.hooks.afterEach( function() { | ||
this.x = 100; | ||
} ); | ||
|
||
QUnit.module( "A", function( hooks ) { | ||
hooks.beforeEach( function() { | ||
this.x = 2; | ||
this.fromModule = true; | ||
} ); | ||
hooks.afterEach( function() { | ||
this.x = 20; | ||
} ); | ||
|
||
QUnit.test( "A1", function( assert ) { | ||
assert.equal( this.x, 2 ); | ||
assert.strictEqual( this.fromGlobal, true ); | ||
assert.strictEqual( this.fromModule, true ); | ||
assert.strictEqual( this.fromNested, undefined ); | ||
} ); | ||
|
||
QUnit.module( "AB", function( hooks ) { | ||
hooks.beforeEach( function() { | ||
this.x = 3; | ||
this.fromNested = true; | ||
} ); | ||
hooks.afterEach( function() { | ||
this.x = 30; | ||
} ); | ||
|
||
QUnit.test( "AB1", function( assert ) { | ||
assert.strictEqual( this.x, 3 ); | ||
assert.strictEqual( this.fromGlobal, true ); | ||
assert.strictEqual( this.fromModule, true ); | ||
assert.strictEqual( this.fromNested, true ); | ||
} ); | ||
} ); | ||
} ); | ||
|
||
QUnit.test( "B", function( assert ) { | ||
assert.strictEqual( this.x, 1 ); | ||
assert.strictEqual( this.fromGlobal, true ); | ||
assert.strictEqual( this.fromModule, undefined ); | ||
assert.strictEqual( this.fromNested, undefined ); | ||
} ); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
function callback( label ) { | ||
return function() { | ||
console.warn( `HOOK: ${QUnit.config.current.testName} @ ${label}` ); | ||
}; | ||
} | ||
|
||
QUnit.hooks.beforeEach( callback( "global beforeEach-1" ) ); | ||
QUnit.hooks.beforeEach( callback( "global beforeEach-2" ) ); | ||
QUnit.hooks.afterEach( callback( "global afterEach-1" ) ); | ||
QUnit.hooks.afterEach( callback( "global afterEach-2" ) ); | ||
|
||
QUnit.test( "A1", assert => { | ||
assert.true( true ); | ||
} ); | ||
|
||
QUnit.module( "B", hooks => { | ||
hooks.before( callback( "B before" ) ); | ||
hooks.beforeEach( callback( "B beforeEach" ) ); | ||
hooks.afterEach( callback( "B afterEach" ) ); | ||
hooks.after( callback( "B after" ) ); | ||
|
||
QUnit.test( "B1", assert => { | ||
assert.true( true ); | ||
} ); | ||
|
||
QUnit.test( "B2", assert => { | ||
assert.true( true ); | ||
} ); | ||
|
||
QUnit.module( "BC", hooks => { | ||
hooks.before( callback( "BC before" ) ); | ||
hooks.beforeEach( callback( "BC beforeEach" ) ); | ||
hooks.afterEach( callback( "BC afterEach" ) ); | ||
hooks.after( callback( "BC after" ) ); | ||
|
||
QUnit.test( "BC1", assert => { | ||
assert.true( true ); | ||
} ); | ||
|
||
QUnit.test( "BC2", assert => { | ||
assert.true( true ); | ||
} ); | ||
|
||
QUnit.module( "BCD", hooks => { | ||
hooks.before( callback( "BCD before" ) ); | ||
hooks.beforeEach( callback( "BCD beforeEach" ) ); | ||
hooks.afterEach( callback( "BCD afterEach" ) ); | ||
hooks.after( callback( "BCD after" ) ); | ||
|
||
QUnit.test( "BCD1", assert => { | ||
assert.true( true ); | ||
} ); | ||
} ); | ||
} ); | ||
} ); | ||
|