-
-
Notifications
You must be signed in to change notification settings - Fork 291
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
058d007
commit 83101fa
Showing
6 changed files
with
47 additions
and
7 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
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,36 @@ | ||
export type LazyValue<T> = {value: T}; | ||
|
||
/** | ||
* Register a callback to compute a value in the before() block of mocha tests | ||
* ```ts | ||
* const state = beforeValue(() => getState()) | ||
* it("test", () => { | ||
* doTest(state.value) | ||
* }) | ||
* ``` | ||
*/ | ||
export function beforeValue<T>(fn: () => T | Promise<T>, timeout?: number): LazyValue<T> { | ||
let value: T = null as unknown as T; | ||
|
||
before(async function () { | ||
this.timeout(timeout ?? 300_000); | ||
value = await fn(); | ||
}); | ||
|
||
return new Proxy<{value: T}>( | ||
{value}, | ||
{ | ||
get: function (target, prop) { | ||
if (prop === "value") { | ||
if (value === null) { | ||
throw Error("beforeValue has not yet run the before() block"); | ||
} else { | ||
return value; | ||
} | ||
} else { | ||
return undefined; | ||
} | ||
}, | ||
} | ||
); | ||
} |
This file was deleted.
Oops, something went wrong.