Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass predicate's input to beforeEach and afterEach hooks #4491

Open
harrisgilliam opened this issue Nov 28, 2023 · 1 comment
Open

Pass predicate's input to beforeEach and afterEach hooks #4491

harrisgilliam opened this issue Nov 28, 2023 · 1 comment

Comments

@harrisgilliam
Copy link

馃殌 Feature Request

It would be really useful if the beforeEach and afterEach hooks had access to the input being feed to the predicate for every check. So the hooks would take two parameters instead of one... the previous hook and the values that will be passed to the predicate.

Motivation

This is related to mocking using Sinon as I have multiple tests that need to use the values generated by the arbitraries to create new mocks for each iteration of a check. This feature would allow me to effectively replace Mocha's beforeEach and afterEach hooks which are usually used for this purpose.

Example

I'm using this handy function to mock entire modules which are dependencies of my code under test: https://spin.atomicobject.com/2018/06/13/mock-typescript-modules-sinon/

This is a fake example but I hope it illustrates what I'm wanting. Imagine we have a dependency "someDep" which the code I'm testing imports. For each iteration I want a function exported by "someDep" (call it: "depFunc") to return something based on the current tests data produced by my arbitraries.

Let say myTestFunc looks like this:

import { depFunc } from "someDep";

export myTestFunc(val: string) {
  return depFunc(val);
}

Right now I have to do something like this in my test:

decribe("my probem", () => {
  const mockDep = mockModule("someDep");

  it("should use my mocked function", () => {
    fc.assert(
      fc.property(fc.string(), (testString: string) => {
        mockedDep(undefined, { depFunc: () => `dep-${testString}` });

        expect(myTestFunc()).to.equal(`dep-${testString`);

        sinon.restore();
      })
   );
  });
});

What I'd rather do is something like this:

decribe("my probem", () => {
  const mockDep = mockModule("someDep");

  it("should use my mocked function", () => {
    fc.assert(
      fc.property(fc.string(), (testString: string) => {
        expect(myTestFunc()).to.equal(`dep-${testString`);
      })
      .beforeEach((prevHook, testString) => {
          mockDep(undefined, { depFunc: () => `dep-${testString}` });
       })
      .afterEach((prevHook, testData) => {
          sinon.restore();
       })
   );
  });
});

The way I have to do things now I end up duplicating the code that sets up and tears down the mock in every test.. which is tedious and clutters the test. If I had access to the test data in the beforeEach and afterEach then it greatly simplifies the test code and is so much cleaner and easier to understand.

@dubzzz
Copy link
Owner

dubzzz commented Nov 28, 2023

Good idea, I need to check how it can fit with my on-going stream on v4 of fast-check. But as I'm currently preparing a new major, probably worth I check that one before putting the major live 馃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants