diff --git a/.changeset/busy-pianos-wink.md b/.changeset/busy-pianos-wink.md new file mode 100644 index 000000000..a9d6d57a9 --- /dev/null +++ b/.changeset/busy-pianos-wink.md @@ -0,0 +1,5 @@ +--- +"fast-check": major +--- + +💥 Make `async{Before,After}Each` the defaults diff --git a/packages/fast-check/src/check/property/_internals/PropertyImplem.ts b/packages/fast-check/src/check/property/_internals/PropertyImplem.ts index b22e980cf..0b8a64bbf 100644 --- a/packages/fast-check/src/check/property/_internals/PropertyImplem.ts +++ b/packages/fast-check/src/check/property/_internals/PropertyImplem.ts @@ -2,7 +2,7 @@ import type { Random } from '../../../random/generator/Random.js'; import type { Arbitrary } from '../../arbitrary/definition/Arbitrary.js'; import { PreconditionFailure } from '../../precondition/PreconditionFailure.js'; import { runIdToFrequency } from './ToFrequency.js'; -import type { GlobalAsyncPropertyHookFunction } from '../../runner/configuration/GlobalParameters.js'; +import type { GlobalPropertyHookFunction } from '../../runner/configuration/GlobalParameters.js'; import { readConfigureGlobal } from '../../runner/configuration/GlobalParameters.js'; import type { Value } from '../../arbitrary/definition/Value.js'; import { Stream } from '../../../stream/Stream.js'; @@ -16,7 +16,7 @@ import type { PropertyWithHooks, PropertyHookFunction } from '../types/PropertyW // Default hook is a no-op // oxlint-disable-next-line no-empty-function -const dummyHook: GlobalAsyncPropertyHookFunction = () => {}; +const dummyHook: GlobalPropertyHookFunction = () => {}; /** @internal */ function outputToPropertyAnswer(output: boolean | void) { @@ -39,28 +39,16 @@ function errorToPropertyAnswer(err: unknown) { * @internal */ export class PropertyImplem implements PropertyWithHooks { - private beforeEachHook: GlobalAsyncPropertyHookFunction; - private afterEachHook: GlobalAsyncPropertyHookFunction; + private beforeEachHook: GlobalPropertyHookFunction; + private afterEachHook: GlobalPropertyHookFunction; constructor( readonly arb: Arbitrary, readonly predicate: (t: Ts) => Promise | boolean | void, ) { - const { asyncBeforeEach, asyncAfterEach, beforeEach, afterEach } = readConfigureGlobal() || {}; + const { beforeEach, afterEach } = readConfigureGlobal() || {}; - if (asyncBeforeEach !== undefined && beforeEach !== undefined) { - throw Error( - 'Global "asyncBeforeEach" and "beforeEach" parameters can\'t be set at the same time when running async properties', - ); - } - - if (asyncAfterEach !== undefined && afterEach !== undefined) { - throw Error( - 'Global "asyncAfterEach" and "afterEach" parameters can\'t be set at the same time when running async properties', - ); - } - - this.beforeEachHook = asyncBeforeEach || beforeEach || dummyHook; - this.afterEachHook = asyncAfterEach || afterEach || dummyHook; + this.beforeEachHook = beforeEach || dummyHook; + this.afterEachHook = afterEach || dummyHook; } generate(mrng: Random, runId?: number): Value { diff --git a/packages/fast-check/src/check/property/types/PropertyWithHooks.ts b/packages/fast-check/src/check/property/types/PropertyWithHooks.ts index 978df0f61..848a46e6f 100644 --- a/packages/fast-check/src/check/property/types/PropertyWithHooks.ts +++ b/packages/fast-check/src/check/property/types/PropertyWithHooks.ts @@ -1,4 +1,4 @@ -import type { GlobalAsyncPropertyHookFunction } from '../../runner/configuration/GlobalParameters.js'; +import type { GlobalPropertyHookFunction } from '../../runner/configuration/GlobalParameters.js'; import type { Property } from './Property.js'; /** @@ -9,8 +9,8 @@ import type { Property } from './Property.js'; * @public */ export type PropertyHookFunction = - | ((previousHookFunction: GlobalAsyncPropertyHookFunction) => Promise) - | ((previousHookFunction: GlobalAsyncPropertyHookFunction) => void); + | ((previousHookFunction: GlobalPropertyHookFunction) => Promise) + | ((previousHookFunction: GlobalPropertyHookFunction) => void); /** * Interface for property defining hooks, see {@link Property} diff --git a/packages/fast-check/src/check/runner/configuration/GlobalParameters.ts b/packages/fast-check/src/check/runner/configuration/GlobalParameters.ts index ad55612c1..5354d5d78 100644 --- a/packages/fast-check/src/check/runner/configuration/GlobalParameters.ts +++ b/packages/fast-check/src/check/runner/configuration/GlobalParameters.ts @@ -10,14 +10,7 @@ let globalParameters: GlobalParameters = {}; * @remarks Since 2.3.0 * @public */ -export type GlobalPropertyHookFunction = () => void; -/** - * Type of legal hook function that can be used in the global parameter `asyncBeforeEach` and/or `asyncAfterEach` - * Prefer `beforeEach` and/or `afterEach` plugins: `fc.assert(property, { plugins: [fc.beforeEach(fn)] })` - * @remarks Since 2.3.0 - * @public - */ -export type GlobalAsyncPropertyHookFunction = (() => Promise) | (() => void); +export type GlobalPropertyHookFunction = (() => Promise) | (() => void); /** * Type describing the global overrides @@ -32,9 +25,6 @@ export type GlobalParameters = Pick< * Specify a function that will be called before each execution of a property. * It behaves as-if you manually called `beforeEach` method on all the properties you execute with fast-check. * - * The function will be used for both {@link fast-check#property} and {@link fast-check#asyncProperty}. - * This global override should never be used in conjunction with `asyncBeforeEach`. - * * @deprecated Prefer the life-cycle plugins: `fc.installGlobalPlugin(fc.beforeEach(fn))` * @remarks Since 2.3.0 */ @@ -43,35 +33,10 @@ export type GlobalParameters = Pick< * Specify a function that will be called after each execution of a property. * It behaves as-if you manually called `afterEach` method on all the properties you execute with fast-check. * - * The function will be used for both {@link fast-check#property} and {@link fast-check#asyncProperty}. - * This global override should never be used in conjunction with `asyncAfterEach`. - * * @deprecated Prefer the life-cycle plugins: `fc.installGlobalPlugin(fc.afterEach(fn))` * @remarks Since 2.3.0 */ afterEach?: GlobalPropertyHookFunction; - /** - * Specify a function that will be called before each execution of an asynchronous property. - * It behaves as-if you manually called `beforeEach` method on all the asynchronous properties you execute with fast-check. - * - * The function will be used only for {@link fast-check#asyncProperty}. It makes synchronous properties created by {@link fast-check#property} unable to run. - * This global override should never be used in conjunction with `beforeEach`. - * - * @deprecated Prefer the life-cycle plugins: `fc.installGlobalPlugin(fc.beforeEach(fn))` - * @remarks Since 2.3.0 - */ - asyncBeforeEach?: GlobalAsyncPropertyHookFunction; - /** - * Specify a function that will be called after each execution of an asynchronous property. - * It behaves as-if you manually called `afterEach` method on all the asynchronous properties you execute with fast-check. - * - * The function will be used only for {@link fast-check#asyncProperty}. It makes synchronous properties created by {@link fast-check#property} unable to run. - * This global override should never be used in conjunction with `afterEach`. - * - * @deprecated Prefer the life-cycle plugins: `fc.installGlobalPlugin(fc.afterEach(fn))` - * @remarks Since 2.3.0 - */ - asyncAfterEach?: GlobalAsyncPropertyHookFunction; /** * Define the base size to be used by arbitraries. * diff --git a/packages/fast-check/src/fast-check.ts b/packages/fast-check/src/fast-check.ts index 10e884753..945c520c0 100644 --- a/packages/fast-check/src/fast-check.ts +++ b/packages/fast-check/src/fast-check.ts @@ -137,11 +137,7 @@ export { asyncModelRun, modelRun, scheduledModelRun } from './check/model/ModelR export { Random } from './random/generator/Random.js'; -export type { - GlobalParameters, - GlobalAsyncPropertyHookFunction, - GlobalPropertyHookFunction, -} from './check/runner/configuration/GlobalParameters.js'; +export type { GlobalParameters, GlobalPropertyHookFunction } from './check/runner/configuration/GlobalParameters.js'; export { configureGlobal, readConfigureGlobal, diff --git a/packages/fast-check/test/unit/check/property/AsyncProperty.spec.ts b/packages/fast-check/test/unit/check/property/AsyncProperty.spec.ts index 1ec73ed97..5dc3afede 100644 --- a/packages/fast-check/test/unit/check/property/AsyncProperty.spec.ts +++ b/packages/fast-check/test/unit/check/property/AsyncProperty.spec.ts @@ -197,10 +197,10 @@ describe('AsyncProperty', () => { await p.runAfterEach(); }); it('Should execute both global and local beforeEach hooks before the test', async () => { - const globalAsyncBeforeEach = vi.fn(); + const globalBeforeEach = vi.fn(); const prob = { beforeEachCalled: false }; configureGlobal({ - asyncBeforeEach: globalAsyncBeforeEach, + beforeEach: globalBeforeEach, }); const p = asyncProperty(stubArb.single(8), async (_arg: number) => { const beforeEachCalled = prob.beforeEachCalled; @@ -218,21 +218,7 @@ describe('AsyncProperty', () => { await p.runBeforeEach(); expect(await p.run(p.generate(stubRng.mutable.nocall()).value)).toBe(null); await p.runAfterEach(); - expect(globalAsyncBeforeEach).toBeCalledTimes(1); - }); - it('Should use global asyncBeforeEach as default if specified', async () => { - const prob = { beforeEachCalled: false }; - configureGlobal({ - asyncBeforeEach: () => (prob.beforeEachCalled = true), - }); - const p = asyncProperty(stubArb.single(8), async (_arg: number) => { - const beforeEachCalled = prob.beforeEachCalled; - prob.beforeEachCalled = false; - return beforeEachCalled; - }); - await p.runBeforeEach(); - expect(await p.run(p.generate(stubRng.mutable.nocall()).value)).toBe(null); - await p.runAfterEach(); + expect(globalBeforeEach).toBeCalledTimes(1); }); it('Should use global beforeEach as default if specified', async () => { const prob = { beforeEachCalled: false }; @@ -248,15 +234,6 @@ describe('AsyncProperty', () => { expect(await p.run(p.generate(stubRng.mutable.nocall()).value)).toBe(null); await p.runAfterEach(); }); - it('Should fail if both global asyncBeforeEach and beforeEach are specified', () => { - configureGlobal({ - asyncBeforeEach: () => {}, - beforeEach: () => {}, - }); - expect(() => asyncProperty(stubArb.single(8), async () => {})).toThrowError( - 'Global "asyncBeforeEach" and "beforeEach" parameters can\'t be set at the same time when running async properties', - ); - }); it('Should execute afterEach after the test on success', async () => { const callOrder: string[] = []; const p = asyncProperty(stubArb.single(8), async (_arg: number) => { @@ -296,20 +273,6 @@ describe('AsyncProperty', () => { await p.runAfterEach(); expect(callOrder).toEqual(['test', 'afterEach']); }); - it('Should use global asyncAfterEach as default if specified', async () => { - const callOrder: string[] = []; - configureGlobal({ - asyncAfterEach: async () => callOrder.push('globalAsyncAfterEach'), - }); - const p = asyncProperty(stubArb.single(8), async (_arg: number) => { - callOrder.push('test'); - return false; - }); - await p.runBeforeEach(); - expect(await p.run(p.generate(stubRng.mutable.nocall()).value)).not.toBe(null); - await p.runAfterEach(); - expect(callOrder).toEqual(['test', 'globalAsyncAfterEach']); - }); it('Should use global afterEach as default if specified', async () => { const callOrder: string[] = []; configureGlobal({ @@ -327,7 +290,7 @@ describe('AsyncProperty', () => { it('Should execute both global and local afterEach hooks', async () => { const callOrder: string[] = []; configureGlobal({ - asyncAfterEach: async () => callOrder.push('globalAsyncAfterEach'), + afterEach: async () => callOrder.push('globalAfterEach'), }); const p = asyncProperty(stubArb.single(8), async (_arg: number) => { callOrder.push('test'); @@ -344,16 +307,7 @@ describe('AsyncProperty', () => { await p.runBeforeEach(); expect(await p.run(p.generate(stubRng.mutable.nocall()).value)).toBe(null); await p.runAfterEach(); - expect(callOrder).toEqual(['test', 'afterEach', 'globalAsyncAfterEach', 'after afterEach']); - }); - it('Should fail if both global asyncAfterEach and afterEach are specified', () => { - configureGlobal({ - asyncAfterEach: () => {}, - afterEach: () => {}, - }); - expect(() => asyncProperty(stubArb.single(8), async () => {})).toThrowError( - 'Global "asyncAfterEach" and "afterEach" parameters can\'t be set at the same time when running async properties', - ); + expect(callOrder).toEqual(['test', 'afterEach', 'globalAfterEach', 'after afterEach']); }); it('should not call shrink on the arbitrary if no context and not unhandled value', () => { // Arrange