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

Does not recognise Vitest 2 inside beforeAll hook #689

Closed
RobinTail opened this issue Jul 10, 2024 · 18 comments
Closed

Does not recognise Vitest 2 inside beforeAll hook #689

RobinTail opened this issue Jul 10, 2024 · 18 comments

Comments

@RobinTail
Copy link
Contributor

Describe the bug A clear and concise description of what the bug is.

When trying to upgrade Vitest from 1.6 to 2.0 I'm getting these warnings:

React Intersection Observer was not configured to handle mocking.
Outside Jest and Vitest, you might need to manually configure it by calling setupIntersectionMocking() and resetIntersectionMocking() in your test setup file.

// test-setup.js
import { resetIntersectionMocking, setupIntersectionMocking } from 'react-intersection-observer/test-utils';

beforeEach(() => {
  setupIntersectionMocking(vi.fn);
});

afterEach(() => {
  resetIntersectionMocking();
});

To Reproduce Try and recreate the issue in a Codesandbox:

none yet

Expected behavior A clear and concise description of what you expected to
happen.

Should detect Vitest 2 similar to Vitest 1.6

Additional context Add any other context about the problem here.

Running latest version, 9.10.3

@RobinTail
Copy link
Contributor Author

Workaround

// vitest.setup.ts
import { setupIntersectionMocking } from "react-intersection-observer/test-utils";
setupIntersectionMocking(vi.fn);
// vitest.config.ts
export default defineConfig({
  test: {
    setupFiles: ["vitest.setup.ts"],
  }
});

@thebuilder
Copy link
Owner

Interesting. I'll have a look.
Also what to see how it works with the new browser mode in Vitest.

@thebuilder
Copy link
Owner

@RobinTail are you sure it's running with global: true in Vitest config?

I've tried upgrading the library itself to Vitest 2, and it doesn't break anything.

@RobinTail
Copy link
Contributor Author

RobinTail commented Jul 10, 2024

@RobinTail are you sure it's running with global: true in Vitest config?

absolutely! I'm surprised myself: I checked your code and found out that it should just check the presence of vi in the global scope, but for some reason it stopped working in my case.

export default defineConfig({
  test: {
    globals: true,
    environment: "happy-dom",

I also tried --globals CLI argument in addition to that.

@RobinTail
Copy link
Contributor Author

Maybe it's because of this settings. I will try another pool.

    pool: "vmThreads",
    poolOptions: {
      vmThreads: { useAtomics: true, memoryLimit: 0.5 },
    },

@RobinTail
Copy link
Contributor Author

No, pool: "forks" does not fix this.

@thebuilder
Copy link
Owner

#690 The tests using the test-utils in the library are all running fine after upgrading.

@RobinTail
Copy link
Contributor Author

I'll try to prepare a sandbox to reproduce it

@RobinTail
Copy link
Contributor Author

RobinTail commented Jul 10, 2024

Dear @thebuilder , here is the VM that reproduces the issue:

https://codesandbox.io/p/devbox/crimson-night-w64sxn

In terminal try to execute yarn test.

(you may probably need to be logged in for that)

@RobinTail
Copy link
Contributor Author

RobinTail commented Jul 10, 2024

Perhaps it may be related to the some changes of the hooks execution order in Vitest 2.
Because I'm calling the method inside beforeAll hook.
I'm not sure what exactly they mean by "reverse order"

Screenshot 2024-07-10 at 21 43 44

UPD: no, tried sequence: { hooks: "parallel" }, for the previous behavior — that didn't fix the issue.

@thebuilder
Copy link
Owner

thebuilder commented Jul 10, 2024

Thanks a lot - I think the problem (in the example) is that you trigger mockAllIsIntersecting in a beforeAll. It's running before the beforeAll in test-utils is triggered.

If I change it so mockAllIsIntersecting is triggered inside the test it works:

import { mockAllIsIntersecting } from "react-intersection-observer/test-utils";

describe("Test", () => {
  test("test", () => {
    mockAllIsIntersecting(true);
    expect(true).toBeTruthy();
  });
});

Looking at the code in test-utils, I think it should be fairly straightforward to make it also instantiate when the mocking functions are triggered.

@RobinTail
Copy link
Contributor Author

Yes, @thebuilder , BUT :)
If I downgrade Vitest to 1.6.0, it works inside beforeAll without warnings.

@RobinTail
Copy link
Contributor Author

RobinTail commented Jul 10, 2024

It's running before the beforeAll in test-utils is triggered.

I see it's actually beforeEach in test-utils, @thebuilder

So maybe in Vitest 2 beforeEach runs after beforeAll.
They mentioned that they changed the order of hooks.
Perhaps it should be addressed somehow.

@RobinTail
Copy link
Contributor Author

The thing is that beforeAll is the recommended place to establish mocking

image

@RobinTail RobinTail changed the title Does not recognise Vitest 2 Does not recognise Vitest 2 inside beforeAll hook Jul 10, 2024
@thebuilder
Copy link
Owner

It runs in beforeEach to reset the mock between tests.

Regardless, I'm not sure why you are triggering the mockAllIsIntersecting before the test is running. 🤔 The intention is you trigger it after creating the Intersection Observers. It only affects active observers, so if you haven't created any, then it won't do anything.

@RobinTail
Copy link
Contributor Author

Ok

@RobinTail RobinTail closed this as not planned Won't fix, can't repro, duplicate, stale Jul 11, 2024
@RobinTail
Copy link
Contributor Author

RobinTail commented Jul 11, 2024

It runs in beforeEach to reset the mock between tests.

Reset mocking you do in afterEach, @thebuilder :

afterEach(() => {
resetIntersectionMocking();
});

In beforeEach you do mocking setup, which I believe, must be in beforeAll instead.

beforeEach(() => {
// Use the exposed mock function. Currently, only supports Jest (`jest.fn`) and Vitest with globals (`vi.fn`).
if (typeof jest !== "undefined") setupIntersectionMocking(jest.fn);
else if (typeof vi !== "undefined") {
// Cast the `vi.fn` to `jest.fn` - The returned `Mock` type has a different signature than `jest.fn`
setupIntersectionMocking(vi.fn as unknown as typeof jest.fn);

@thebuilder
Copy link
Owner

f72e455 (#690)

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