-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add utility function to augment tests with runtime config
- Loading branch information
Showing
1 changed file
with
21 additions
and
0 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,21 @@ | ||
import { beforeAll as myBeforeAll } from "vitest"; | ||
|
||
/** | ||
* Testing utility to set the `window.config` object before tests are executed and clear it automatically afterwards | ||
* | ||
* @param beforeAll The beforeAll hook imported from vitest. | ||
* For some reason this needs to be passed as an argument and cannot be imported by this utility function on its own :/ | ||
* @param config The configuration that should be set on `window.config` | ||
*/ | ||
export function withConfig(beforeAll: typeof myBeforeAll, config: Record<string, string>): void { | ||
let previousValue: typeof window.config; | ||
|
||
beforeAll(() => { | ||
previousValue = window.config; | ||
window.config = Object.freeze(config); | ||
|
||
return () => { | ||
window.config = previousValue; | ||
}; | ||
}); | ||
} |