-
-
Notifications
You must be signed in to change notification settings - Fork 5
doc(proposal): suite-level hooks #25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
JakobJingleheimer
wants to merge
1
commit into
main
Choose a base branch
from
proposal/suite-level-hooks
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+89
−0
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,89 @@ | ||
| # Suite-level hooks | ||
|
|
||
| ## Problem(s) to solve | ||
|
|
||
| * Hooks (`after`/`afterEach`, `before`/`beforeEach`) are currently global, which causes them to trigger for subtests. That is often counter-productive, rendering subtests useless (they end up clobbering each other). | ||
| * Tests often need common setup but are affected by concurrency (for instance, mocks). In those scenarios, either concurrency must be disabled or a lot of code must be repeated. | ||
| * For mocks, it is generally desirable to reset them between tests. | ||
|
|
||
| ## API & Behaviour | ||
|
|
||
| Note in the below: | ||
|
|
||
| * `s` is `SuiteContext` | ||
| * `th` is `TestHookContext` | ||
| * `t` is `TestContext` | ||
|
|
||
| ```ts | ||
| type SuiteContextHook = ( | ||
| c?: (th: TestHookContext): void, | ||
| options: HookOptions, | ||
| ); | ||
| type SuiteContext = { | ||
| // … | ||
| after: SuiteContextHook, | ||
| afterEach: SuiteContextHook, | ||
| before: SuiteContextHook, | ||
| beforeEach: SuiteContextHook, | ||
| mock: SuiteContextMockTracker → MockTracker, | ||
| } | ||
| ``` | ||
| ```ts | ||
| type TestHookContext = Record<string | Symbol, any>; | ||
| ``` | ||
| ```ts | ||
| type TestContext = { | ||
| // … | ||
| bikeshed: TestHookContext, | ||
| } | ||
| ``` | ||
|
|
||
| ```js | ||
| describe('Suite-level hooks', (s) => { | ||
| s.before((th) => { | ||
| const foo = s.mock.fn(); | ||
| s.mock.module('foo', { exports: { default: foo } }); | ||
| const bar = s.mock.fn(); | ||
| s.mock.module('bar', { exports: { default: bar } }); | ||
|
|
||
| th.mocks = { | ||
| bar: bar.mock, | ||
| foo: foo.mock, | ||
| }; | ||
| }); | ||
|
|
||
| it('should abort on error', (t) => { | ||
| t.bikeshed.mocks.foo.mockImplementation(() => false); | ||
|
|
||
| widget(); | ||
|
|
||
| t.test('call foo', () => assert.equal(t.bikeshed.mocks.foo.callCount(), 1)); | ||
| t.test('call bar', () => assert.equal(t.bikeshed.mocks.bar.callCount(), 0)); | ||
| }); | ||
|
|
||
| it('should succeed on happy-path', (t) => { | ||
| widget(); | ||
|
|
||
| t.test('call foo', () => assert.equal(t.bikeshed.mocks.foo.callCount(), 1)); | ||
| t.test('call bar', () => assert.equal(t.bikeshed.mocks.bar.callCount(), 1)); | ||
| }); | ||
| }); | ||
| ``` | ||
|
|
||
| A lot of subtle things are happening in the above: | ||
|
|
||
| Each test (`it`) receives a clone of `TestHookContext` (nested on a known/reserved slot currently called `bikeshed` because I couldn't think of a good name). This means each mock is distinct: | ||
| * The `mockImplementation` from _'should abort on error'_ applies to only its own copy of the mock of `foo` and does NOT affect the `foo` mock in _'should succeed on happy-path'_. | ||
| * A mock's calls (counters, arguments, etc) are isolated to the test. | ||
| * Mutations to the `TestHookContext` a test receives do not affect `TestHookContext` in other tests. | ||
|
|
||
| | | between tests (`it`) | between subtests (`t.test`) | ||
| :-- | :--: | :--: | ||
| `SuiteContext.mock.resetCalls()` | ✅ | ❌ | ||
| `SuiteHook`s | ✅ | ❌ | ||
|
|
||
| ## Under the hood | ||
|
|
||
| The `SuiteContextHookFn` passed to a `SuiteContextHook` is run for every test, receiving a fresh `TestHookContext` mapped to the test to which it will be provided. | ||
|
|
||
| Each `SuiteContextMockTracker` will need to intelligently apply appropriate mocks. | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.