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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calling RunWithDrivine() when using it.each in a test file causes TypeError: it.each is not a function #114

Open
NilsMoller opened this issue Apr 10, 2023 · 7 comments

Comments

@NilsMoller
Copy link

The title says it all, but for more detail here is a spec file which shows the issue. The argument for RunWithDrivine does not seem to make a difference. Any idea what could be causing this?

import SLUG_REGEX from "./slug-regex.util";
import { RunWithDrivine } from "@liberation-data/drivine";

RunWithDrivine(); // If this is removed, the tests pass. If this is called, the error shown below is thrown
describe("slug-regex", () => {
  let slugRegex: RegExp;

  beforeEach(async () => {
    slugRegex = new RegExp(SLUG_REGEX);
  });

  it.each([
    "correct",
    "also-correct",
    "this-is-a-valid-slug",
    "c4n-4ls0-c0nt41n-num6er5",
  ])("accepts correct slug string '%s'", async (slugString: string) => {
    // Act
    const result: boolean = slugRegex.test(slugString);

    // Assert
    expect(result).toBeDefined();
    expect(result).toStrictEqual<boolean>(true);
  });
});

Error:

  ● Test suite failed to run

    TypeError: it.each is not a function

       9 |     slugRegex = new RegExp(SLUG_REGEX);
      10 |   });
    > 11 |
         | ^
      12 |   it.each([
      13 |     "correct",
      14 |     "also-correct",

      at utils/slug-regex.util.spec.ts:11:8
      at Object.<anonymous> (utils/slug-regex.util.spec.ts:6:1)
@NilsMoller NilsMoller changed the title Calling RunWithDrivine() in a test file causes TypeError: it.each is not a function Calling RunWithDrivine() when using it.each in a test file causes TypeError: it.each is not a function Apr 10, 2023
@jasperblues
Copy link
Member

jasperblues commented Apr 10, 2023

RunWithDrivine() is supposed to go at the top of the spec, before jest does anything.

For a single test, you can use InDrivineContext.run( () => inside the test.

I appreciate this is a bit confusing.

(Will wait for confirmation that the above works)

@NilsMoller
Copy link
Author

Is this not what I am doing? I am importing the SUT (SLUG_REGEX in this case) and the RunWithDrivine method, then calling RunWithDrivine() and then the first describe. Could it have something to do with me using a globalSetup file?

@jasperblues
Copy link
Member

Oh sorry, I thought it was on a nested describe block (was super early in the morning, pre-mountain bike ride, when I read that).

I would say its either the jest version or the import order. Could you try putting the import { RunWithDrivine } from "@liberation-data/drivine"; in the global setup?

@NilsMoller
Copy link
Author

I will try this tomorrow morning (in about 8 hours from now) and get back to you.

@NilsMoller
Copy link
Author

Just importing it does not work, but calling RunWithDrivine anywhere in the global setup does work, but this is not ideal.

@jasperblues
Copy link
Member

And that's even if it is the first import in the global setup?

@NilsMoller
Copy link
Author

Yes, I tried it as a first and later import, which does not work. In both scenarios I also tried it with actually calling the method, which does work.

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

No branches or pull requests

2 participants