Best JavaScript code snippet using fast-check-monorepo
useTimeout.spec.ts
Source:useTimeout.spec.ts
1import { renderHook } from '@testing-library/react-hooks';2import { useTimeout, TimeoutHandler } from '../src';3jest.useFakeTimers();4let timeoutHandler: TimeoutHandler<number>;5let setTimeoutSpy: jest.Mock<number, [TimerHandler, number?, ...any[]]>;6let clearTimeoutSpy: jest.Mock<void, [number?]>;7afterEach(() => {8 jest.clearAllMocks();9 jest.clearAllTimers();10 setTimeoutSpy = jest.fn(window.setTimeout);11 clearTimeoutSpy = jest.fn(window.clearTimeout);12 timeoutHandler = {13 setTimeout: (fn: () => void, timeout: number) => setTimeoutSpy(fn, timeout),14 clearTimeout: (timeoutFn: number | undefined) => { clearTimeoutSpy(timeoutFn) },15 };16})17describe('useTimeout with custom TimeoutHandler', () => {18 it('should be defined', () => {19 expect(useTimeout).toBeDefined();20 });21 it('should execute a callback after the specified timeout, which must not leak', () => {22 const callbackProp = jest.fn();23 const timeoutProp = 1000;24 const { unmount } = renderHook(({ callback, timeout, deps }) => (25 useTimeout(callback, timeout, timeoutHandler, deps)26 ), {27 initialProps: {28 callback: callbackProp,29 timeout: timeoutProp,30 deps: [timeoutProp],31 },32 });33 expect(setTimeoutSpy).toHaveBeenCalledTimes(1);34 expect(setTimeoutSpy).toHaveBeenCalledWith(callbackProp, 1000);35 expect(callbackProp).toHaveBeenCalledTimes(0);36 expect(clearTimeoutSpy).toHaveBeenCalledTimes(0);37 jest.advanceTimersByTime(1500);38 expect(callbackProp).toHaveBeenCalledTimes(1);39 expect(clearTimeoutSpy).toHaveBeenCalledTimes(0);40 unmount();41 expect(clearTimeoutSpy).toHaveBeenCalledTimes(1);42 });43 it('should clear the timeout early if cancelTimeout is called', () => {44 const callbackProp = jest.fn();45 const timeoutProp = 1000;46 const { result, unmount } = renderHook(({ callback, timeout, deps }) => (47 useTimeout(callback, timeout, timeoutHandler, deps)48 ), {49 initialProps: {50 callback: callbackProp,51 timeout: timeoutProp,52 deps: [timeoutProp],53 },54 });55 const { current: cancelTimeout } = result;56 expect(typeof cancelTimeout).toBe('function');57 expect(setTimeoutSpy).toHaveBeenCalledTimes(1);58 expect(setTimeoutSpy).toHaveBeenCalledWith(callbackProp, timeoutProp);59 expect(callbackProp).toHaveBeenCalledTimes(0);60 expect(clearTimeoutSpy).toHaveBeenCalledTimes(0);61 jest.advanceTimersByTime(495);62 cancelTimeout();63 jest.advanceTimersByTime(5);64 expect(callbackProp).toHaveBeenCalledTimes(0);65 expect(clearTimeoutSpy).toHaveBeenCalledTimes(1);66 jest.advanceTimersByTime(timeoutProp);67 expect(callbackProp).toHaveBeenCalledTimes(0);68 expect(clearTimeoutSpy).toHaveBeenCalledTimes(1);69 unmount();70 expect(clearTimeoutSpy).toHaveBeenCalledTimes(2);71 });72 it('should not execute the original callback if the callback property changes before the old timeout expires', () => {73 const callbackProp = jest.fn();74 const timeoutProp = 1000;75 const { rerender, unmount } = renderHook(({ callback, timeout, deps }) => (76 useTimeout(callback, timeout, timeoutHandler, deps)77 ), {78 initialProps: {79 callback: callbackProp,80 timeout: timeoutProp,81 deps: [timeoutProp, callbackProp],82 },83 });84 expect(setTimeoutSpy).toHaveBeenCalledTimes(1);85 expect(setTimeoutSpy).toHaveBeenCalledWith(callbackProp, timeoutProp);86 expect(callbackProp).toHaveBeenCalledTimes(0);87 expect(clearTimeoutSpy).toHaveBeenCalledTimes(0);88 jest.advanceTimersByTime(500);89 const newCallbackProp = jest.fn();90 rerender({91 callback: newCallbackProp,92 timeout: timeoutProp,93 deps: [timeoutProp, newCallbackProp],94 });95 jest.advanceTimersByTime(750);96 expect(clearTimeoutSpy).toHaveBeenCalledTimes(1);97 expect(callbackProp).toHaveBeenCalledTimes(0);98 expect(newCallbackProp).toHaveBeenCalledTimes(0);99 jest.advanceTimersByTime(timeoutProp);100 expect(setTimeoutSpy).toHaveBeenCalledTimes(2);101 expect(clearTimeoutSpy).toHaveBeenCalledTimes(1);102 expect(callbackProp).toHaveBeenCalledTimes(0);103 expect(newCallbackProp).toHaveBeenCalledTimes(1);104 unmount();105 expect(clearTimeoutSpy).toHaveBeenCalledTimes(2);106 });107 it(`should delay the callback if the timeout property changes before the old timeout expires108 and if the timeout was originally added to the hook dependencies`, () => {109 const callbackProp = jest.fn();110 const timeoutProp = 1000;111 const { rerender, unmount } = renderHook(({ callback, timeout, deps }) => (112 useTimeout(callback, timeout, timeoutHandler, deps)113 ), {114 initialProps: {115 callback: callbackProp,116 timeout: timeoutProp,117 deps: [timeoutProp],118 },119 });120 expect(setTimeoutSpy).toHaveBeenCalledTimes(1);121 expect(setTimeoutSpy).toHaveBeenCalledWith(callbackProp, timeoutProp);122 expect(callbackProp).toHaveBeenCalledTimes(0);123 expect(clearTimeoutSpy).toHaveBeenCalledTimes(0);124 jest.advanceTimersByTime(500);125 const newTimeoutProp = 3000;126 rerender({127 callback: callbackProp,128 timeout: newTimeoutProp,129 deps: [newTimeoutProp],130 });131 expect(setTimeoutSpy).toHaveBeenCalledTimes(2);132 expect(setTimeoutSpy).toHaveBeenCalledWith(callbackProp, newTimeoutProp);133 expect(callbackProp).toHaveBeenCalledTimes(0);134 expect(clearTimeoutSpy).toHaveBeenCalledTimes(1);135 jest.advanceTimersByTime(1000);136 expect(callbackProp).toHaveBeenCalledTimes(0);137 jest.advanceTimersByTime(newTimeoutProp);138 expect(setTimeoutSpy).toHaveBeenCalledTimes(2);139 expect(clearTimeoutSpy).toHaveBeenCalledTimes(1);140 expect(callbackProp).toHaveBeenCalledTimes(1);141 unmount();142 expect(clearTimeoutSpy).toHaveBeenCalledTimes(2);143 });144 it('if no dependencies is passed, it should not reset the counter, even if some property changed', () => {145 const callbackProp = jest.fn();146 const timeoutProp = 1000;147 const { rerender, unmount } = renderHook(({ callback, timeout, deps }) => (148 useTimeout(callback, timeout, timeoutHandler, deps)149 ), {150 initialProps: {151 callback: callbackProp,152 timeout: timeoutProp,153 deps: undefined,154 },155 });156 expect(setTimeoutSpy).toHaveBeenCalledTimes(1);157 expect(setTimeoutSpy).toHaveBeenCalledWith(callbackProp, timeoutProp);158 expect(callbackProp).toHaveBeenCalledTimes(0);159 expect(clearTimeoutSpy).toHaveBeenCalledTimes(0);160 jest.advanceTimersByTime(500);161 const newTimeoutProp = 3000;162 rerender({163 callback: callbackProp,164 timeout: newTimeoutProp,165 deps: undefined,166 });167 expect(setTimeoutSpy).toHaveBeenCalledTimes(1);168 expect(setTimeoutSpy).toHaveBeenCalledWith(callbackProp, timeoutProp);169 expect(callbackProp).toHaveBeenCalledTimes(0);170 expect(clearTimeoutSpy).toHaveBeenCalledTimes(0);171 jest.advanceTimersByTime(1000);172 expect(callbackProp).toHaveBeenCalledTimes(1);173 expect(setTimeoutSpy).toHaveBeenCalledTimes(1);174 expect(clearTimeoutSpy).toHaveBeenCalledTimes(0);175 unmount();176 expect(clearTimeoutSpy).toHaveBeenCalledTimes(1);177 });...
useDefaultTimeout.spec.ts
Source:useDefaultTimeout.spec.ts
1import { renderHook } from '@testing-library/react-hooks';2import useDefaultTimeout from '../src';3jest.useFakeTimers();4afterEach(() => {5 jest.clearAllMocks();6 jest.clearAllTimers();7})8describe('useTimeout with default TimeoutHandler', () => {9 it('should be defined', () => {10 expect(useDefaultTimeout).toBeDefined();11 });12 it('should execute a callback after the specified timeout, which must not leak', () => {13 const callbackProp = jest.fn();14 const timeoutProp = 1000;15 const { unmount } = renderHook(({ callback, timeout, deps }) => (16 useDefaultTimeout(callback, timeout, deps)17 ), {18 initialProps: {19 callback: callbackProp,20 timeout: timeoutProp,21 deps: [timeoutProp],22 },23 });24 expect(setTimeout).toHaveBeenCalledTimes(1);25 expect(setTimeout).toHaveBeenCalledWith(callbackProp, 1000);26 expect(callbackProp).toHaveBeenCalledTimes(0);27 expect(clearTimeout).toHaveBeenCalledTimes(0);28 jest.advanceTimersByTime(1500);29 expect(callbackProp).toHaveBeenCalledTimes(1);30 expect(clearTimeout).toHaveBeenCalledTimes(0);31 unmount();32 expect(clearTimeout).toHaveBeenCalledTimes(1);33 });34 it('should clear the timeout early if cancelTimeout is called', () => {35 const callbackProp = jest.fn();36 const timeoutProp = 1000;37 const { result, unmount } = renderHook(({ callback, timeout, deps }) => (38 useDefaultTimeout(callback, timeout, deps)39 ), {40 initialProps: {41 callback: callbackProp,42 timeout: timeoutProp,43 deps: [timeoutProp],44 },45 });46 const { current: cancelTimeout } = result;47 expect(typeof cancelTimeout).toBe('function');48 expect(setTimeout).toHaveBeenCalledTimes(1);49 expect(setTimeout).toHaveBeenCalledWith(callbackProp, timeoutProp);50 expect(callbackProp).toHaveBeenCalledTimes(0);51 expect(clearTimeout).toHaveBeenCalledTimes(0);52 jest.advanceTimersByTime(495);53 cancelTimeout();54 jest.advanceTimersByTime(5);55 expect(callbackProp).toHaveBeenCalledTimes(0);56 expect(clearTimeout).toHaveBeenCalledTimes(1);57 jest.advanceTimersByTime(timeoutProp);58 expect(callbackProp).toHaveBeenCalledTimes(0);59 expect(clearTimeout).toHaveBeenCalledTimes(1);60 unmount();61 expect(clearTimeout).toHaveBeenCalledTimes(2);62 });63 it('should not execute the original callback if the callback property changes before the old timeout expires', () => {64 const callbackProp = jest.fn();65 const timeoutProp = 1000;66 const { rerender, unmount } = renderHook(({ callback, timeout, deps }) => (67 useDefaultTimeout(callback, timeout, deps)68 ), {69 initialProps: {70 callback: callbackProp,71 timeout: timeoutProp,72 deps: [timeoutProp, callbackProp],73 },74 });75 expect(setTimeout).toHaveBeenCalledTimes(1);76 expect(setTimeout).toHaveBeenCalledWith(callbackProp, timeoutProp);77 expect(callbackProp).toHaveBeenCalledTimes(0);78 expect(clearTimeout).toHaveBeenCalledTimes(0);79 jest.advanceTimersByTime(500);80 const newCallbackProp = jest.fn();81 rerender({82 callback: newCallbackProp,83 timeout: timeoutProp,84 deps: [timeoutProp, newCallbackProp],85 });86 jest.advanceTimersByTime(750);87 expect(clearTimeout).toHaveBeenCalledTimes(1);88 expect(callbackProp).toHaveBeenCalledTimes(0);89 expect(newCallbackProp).toHaveBeenCalledTimes(0);90 jest.advanceTimersByTime(timeoutProp);91 expect(setTimeout).toHaveBeenCalledTimes(2);92 expect(clearTimeout).toHaveBeenCalledTimes(1);93 expect(callbackProp).toHaveBeenCalledTimes(0);94 expect(newCallbackProp).toHaveBeenCalledTimes(1);95 unmount();96 expect(clearTimeout).toHaveBeenCalledTimes(2);97 });98 it(`should delay the callback if the timeout property changes before the old timeout expires99 and if the timeout was originally added to the hook dependencies`, () => {100 const callbackProp = jest.fn();101 const timeoutProp = 1000;102 const { rerender, unmount } = renderHook(({ callback, timeout, deps }) => (103 useDefaultTimeout(callback, timeout, deps)104 ), {105 initialProps: {106 callback: callbackProp,107 timeout: timeoutProp,108 deps: [timeoutProp],109 },110 });111 expect(setTimeout).toHaveBeenCalledTimes(1);112 expect(setTimeout).toHaveBeenCalledWith(callbackProp, timeoutProp);113 expect(callbackProp).toHaveBeenCalledTimes(0);114 expect(clearTimeout).toHaveBeenCalledTimes(0);115 jest.advanceTimersByTime(500);116 const newTimeoutProp = 3000;117 rerender({118 callback: callbackProp,119 timeout: newTimeoutProp,120 deps: [newTimeoutProp],121 });122 expect(setTimeout).toHaveBeenCalledTimes(2);123 expect(setTimeout).toHaveBeenCalledWith(callbackProp, newTimeoutProp);124 expect(callbackProp).toHaveBeenCalledTimes(0);125 expect(clearTimeout).toHaveBeenCalledTimes(1);126 jest.advanceTimersByTime(1000);127 expect(callbackProp).toHaveBeenCalledTimes(0);128 jest.advanceTimersByTime(newTimeoutProp);129 expect(setTimeout).toHaveBeenCalledTimes(2);130 expect(clearTimeout).toHaveBeenCalledTimes(1);131 expect(callbackProp).toHaveBeenCalledTimes(1);132 unmount();133 expect(clearTimeout).toHaveBeenCalledTimes(2);134 });135 it('if no dependencies is passed, it should not reset the counter, even if some property changed', () => {136 const callbackProp = jest.fn();137 const timeoutProp = 1000;138 const { rerender, unmount } = renderHook(({ callback, timeout, deps }) => (139 useDefaultTimeout(callback, timeout, deps)140 ), {141 initialProps: {142 callback: callbackProp,143 timeout: timeoutProp,144 deps: undefined,145 },146 });147 expect(setTimeout).toHaveBeenCalledTimes(1);148 expect(setTimeout).toHaveBeenCalledWith(callbackProp, timeoutProp);149 expect(callbackProp).toHaveBeenCalledTimes(0);150 expect(clearTimeout).toHaveBeenCalledTimes(0);151 jest.advanceTimersByTime(500);152 const newTimeoutProp = 3000;153 rerender({154 callback: callbackProp,155 timeout: newTimeoutProp,156 deps: undefined,157 });158 expect(setTimeout).toHaveBeenCalledTimes(1);159 expect(setTimeout).toHaveBeenCalledWith(callbackProp, timeoutProp);160 expect(callbackProp).toHaveBeenCalledTimes(0);161 expect(clearTimeout).toHaveBeenCalledTimes(0);162 jest.advanceTimersByTime(1000);163 expect(callbackProp).toHaveBeenCalledTimes(1);164 expect(setTimeout).toHaveBeenCalledTimes(1);165 expect(clearTimeout).toHaveBeenCalledTimes(0);166 unmount();167 expect(clearTimeout).toHaveBeenCalledTimes(1);168 });...
TimeoutProperty.spec.ts
Source:TimeoutProperty.spec.ts
1import { Value } from '../../../../src/check/arbitrary/definition/Value';2import { TimeoutProperty } from '../../../../src/check/property/TimeoutProperty';3import { fakeRandom } from '../../arbitrary/__test-helpers__/RandomHelpers';4import { fakeProperty } from './__test-helpers__/PropertyHelpers';5describe('TimeoutProperty', () => {6 beforeEach(() => {7 jest.clearAllTimers();8 });9 it('should forward calls to generate', () => {10 // Arrange11 jest.useFakeTimers();12 const { instance: decoratedProperty, generate } = fakeProperty(true);13 const { instance: mrng } = fakeRandom();14 const expectedRunId = 42;15 const expectedOut = new Value(Symbol('value'), Symbol('context'));16 generate.mockReturnValueOnce(expectedOut);17 // Act18 const timeoutProp = new TimeoutProperty(decoratedProperty, 100);19 const out = timeoutProp.generate(mrng, expectedRunId);20 // Assert21 expect(out).toBe(expectedOut);22 expect(generate).toHaveBeenCalledTimes(1);23 expect(generate).toHaveBeenCalledWith(mrng, expectedRunId);24 });25 it('should forward inputs to run', async () => {26 // Arrange27 jest.useFakeTimers();28 const { instance: decoratedProperty, run } = fakeProperty(true);29 const expectedRunInput = { anything: Symbol('something') };30 // Act31 const timeoutProp = new TimeoutProperty(decoratedProperty, 10);32 const runPromise = timeoutProp.run(expectedRunInput);33 jest.advanceTimersByTime(10);34 await runPromise;35 // Assert36 expect(run).toHaveBeenCalledTimes(1);37 expect(run).toHaveBeenCalledWith(expectedRunInput);38 });39 it('should not timeout if it succeeds in time', async () => {40 // Arrange41 jest.useFakeTimers();42 const { instance: decoratedProperty, run } = fakeProperty(true);43 run.mockReturnValueOnce(44 new Promise(function (resolve) {45 setTimeout(() => resolve(null), 10);46 })47 );48 // Act49 const timeoutProp = new TimeoutProperty(decoratedProperty, 100);50 const runPromise = timeoutProp.run({});51 jest.advanceTimersByTime(10);52 await runPromise;53 // Assert54 expect(await runPromise).toBe(null);55 });56 it('should not timeout if it fails in time', async () => {57 // Arrange58 const errorFromUnderlying = { error: undefined, errorMessage: 'plop' };59 jest.useFakeTimers();60 const { instance: decoratedProperty, run } = fakeProperty(true);61 run.mockReturnValueOnce(62 new Promise(function (resolve) {63 // underlying property is not supposed to throw (reject)64 setTimeout(() => resolve(errorFromUnderlying), 10);65 })66 );67 // Act68 const timeoutProp = new TimeoutProperty(decoratedProperty, 100);69 const runPromise = timeoutProp.run({});70 jest.advanceTimersByTime(10);71 await runPromise;72 // Assert73 expect(await runPromise).toBe(errorFromUnderlying);74 });75 it('should clear all started timeouts on success', async () => {76 // Arrange77 jest.useFakeTimers();78 jest.spyOn(global, 'setTimeout');79 jest.spyOn(global, 'clearTimeout');80 const { instance: decoratedProperty, run } = fakeProperty(true);81 run.mockResolvedValueOnce(null);82 // Act83 const timeoutProp = new TimeoutProperty(decoratedProperty, 100);84 await timeoutProp.run({});85 // Assert86 expect(setTimeout).toBeCalledTimes(1);87 expect(clearTimeout).toBeCalledTimes(1);88 });89 it('should clear all started timeouts on failure', async () => {90 // Arrange91 const errorFromUnderlying = { error: undefined, errorMessage: 'plop' };92 jest.useFakeTimers();93 jest.spyOn(global, 'setTimeout');94 jest.spyOn(global, 'clearTimeout');95 const { instance: decoratedProperty, run } = fakeProperty(true);96 run.mockResolvedValueOnce(errorFromUnderlying);97 // Act98 const timeoutProp = new TimeoutProperty(decoratedProperty, 100);99 await timeoutProp.run({});100 // Assert101 expect(setTimeout).toBeCalledTimes(1);102 expect(clearTimeout).toBeCalledTimes(1);103 });104 it('should timeout if it takes to long', async () => {105 // Arrange106 jest.useFakeTimers();107 const { instance: decoratedProperty, run } = fakeProperty(true);108 run.mockReturnValueOnce(109 new Promise(function (resolve) {110 setTimeout(() => resolve(null), 100);111 })112 );113 // Act114 const timeoutProp = new TimeoutProperty(decoratedProperty, 10);115 const runPromise = timeoutProp.run({});116 jest.advanceTimersByTime(10);117 // Assert118 expect(await runPromise).toEqual({119 error: expect.any(Error),120 errorMessage: `Property timeout: exceeded limit of 10 milliseconds`,121 });122 });123 it('Should timeout if it never ends', async () => {124 // Arrange125 jest.useFakeTimers();126 const { instance: decoratedProperty, run } = fakeProperty(true);127 run.mockReturnValueOnce(new Promise(() => {}));128 // Act129 const timeoutProp = new TimeoutProperty(decoratedProperty, 10);130 const runPromise = timeoutProp.run({});131 jest.advanceTimersByTime(10);132 // Assert133 expect(await runPromise).toEqual({134 error: expect.any(Error),135 errorMessage: `Property timeout: exceeded limit of 10 milliseconds`,136 });137 });...
Using AI Code Generation
1const fc = require('fast-check');2const { timeoutProp } = require('fast-check-monorepo');3timeoutProp(4 () => fc.property(fc.integer(), fc.integer(), (a, b) => a + b === b + a),5);6{7 "dependencies": {8 },9 "devDependencies": {},10 "scripts": {11 },12}
Using AI Code Generation
1const fc = require('fast-check')2const timeoutProp = require('fast-check-monorepo').timeoutProp3const isEven = (n) => n % 2 === 04const arbEvenNumber = fc.integer().filter(isEven)5const property = fc.property(arbEvenNumber, (n) => {6 return isEven(n)7})
Using AI Code Generation
1const fc = require('fast-check');2const { timeoutProp } = require('fast-check-monorepo');3const test = () => {4 return timeoutProp(fc.property(fc.integer(), (a) => a > 0), 5000);5};6test();7{8 "scripts": {9 },10 "dependencies": {11 }12}13module.exports = {14};15{16}17{18 "parserOptions": {19 },20 "rules": {21 }22}23{24}25{26 "editor.codeActionsOnSave": {27 },
Using AI Code Generation
1const { timeoutProp } = require('fast-check');2timeoutProp(3 () => {4 },5 { numRuns: 1000 }6);7{8 "dependencies": {9 }10}11{12}13{14 "parserOptions": {15 },16 "env": {17 },18 "rules": {19 }20}
Using AI Code Generation
1const fc = require('fast-check');2const { timeoutProp } = require('fast-check-monorepo');3const { expect } = require('chai');4describe('TimeoutProp', () => {5 it('should timeout', () => {6 const test = fc.property(fc.integer(), (x) => x % 2 === 0);7 const result = timeoutProp(test, 10);8 expect(result).to.be.false;9 });10});
Using AI Code Generation
1const fc = require('fast-check');2const {timeoutProp} = require('fast-check-monorepo');3timeoutProp(fc.property(fc.integer(), i => i > 0), 1000);4const fc = require('fast-check');5const {timeoutProp} = require('fast-check-monorepo');6timeoutProp(fc.property(fc.integer(), i => i > 0), 1000);7const fc = require('fast-check');8const {timeoutProp} = require('fast-check-monorepo');9timeoutProp(fc.property(fc.integer(), i => i > 0), 1000);10const fc = require('fast-check');11const {timeoutProp} = require('fast-check-monorepo');12timeoutProp(fc.property(fc.integer(), i => i > 0), 1000);13const fc = require('fast-check');14const {timeoutProp} = require('fast-check-monorepo');15timeoutProp(fc.property(fc.integer(), i => i > 0), 1000);16const fc = require('fast-check');17const {timeoutProp} = require('fast-check-monorepo');18timeoutProp(fc.property(fc.integer(), i => i > 0), 1000);19const fc = require('fast-check');20const {timeoutProp} = require('fast-check
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!