Skip to content

Commit

Permalink
[code-infra] Allow overriding all options of useFakeTimers functi…
Browse files Browse the repository at this point in the history
…on (#43729)
  • Loading branch information
LukasTy authored Sep 12, 2024
1 parent 47cc78d commit 6840e7e
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions packages-internal/test-utils/src/createRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,11 @@ export interface Clock {

export type ClockConfig = undefined | number | Date;

function createClock(defaultMode: 'fake' | 'real', config: ClockConfig): Clock {
function createClock(
defaultMode: 'fake' | 'real',
config: ClockConfig,
options?: Exclude<Parameters<typeof useFakeTimers>[0], number | Date>,
): Clock {
let clock: ReturnType<typeof useFakeTimers> | null = null;

let mode = defaultMode;
Expand All @@ -384,6 +388,7 @@ function createClock(defaultMode: 'fake' | 'real', config: ClockConfig): Clock {
// Technically we'd want to reset all modules between tests but we don't have that technology.
// In the meantime just continue to clear native timers like with did for the past years when using `sinon` < 8.
shouldClearNativeTimers: true,
...options,
});
}
});
Expand Down Expand Up @@ -457,6 +462,7 @@ export interface CreateRendererOptions extends Pick<RenderOptions, 'strict' | 's
*/
clock?: 'fake' | 'real';
clockConfig?: ClockConfig;
clockOptions?: Parameters<typeof createClock>[2];
}

export function createRenderer(globalOptions: CreateRendererOptions = {}): Renderer {
Expand All @@ -465,10 +471,11 @@ export function createRenderer(globalOptions: CreateRendererOptions = {}): Rende
clockConfig,
strict: globalStrict = true,
strictEffects: globalStrictEffects = globalStrict,
clockOptions,
} = globalOptions;
// save stack to re-use in test-hooks
const { stack: createClientRenderStack } = new Error();
const clock = createClock(clockMode, clockConfig);
const clock = createClock(clockMode, clockConfig, clockOptions);

/**
* Flag whether `createRenderer` was called in a suite i.e. describe() block.
Expand Down

0 comments on commit 6840e7e

Please sign in to comment.