Skip to content

Commit

Permalink
add utility function to augment tests with runtime config
Browse files Browse the repository at this point in the history
  • Loading branch information
lilioid committed Sep 13, 2022
1 parent 1ad4786 commit ee1ba79
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/testing.ts
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;
};
});
}

0 comments on commit ee1ba79

Please sign in to comment.