-
Notifications
You must be signed in to change notification settings - Fork 471
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
Allow fake timers other than jest #987
Comments
The reference to jest is not relevant here. We do support jest's modern fake timers (sinonjs) and their own legacy timers. What I'd like to see is the option to pass clock adapter that we use internally. Then people can write adapters for all sorts of fake timer libraries and we just continue supporting jest. |
Thanks @eps1lon. That's essentially the approach you laid out in the comment I linked to, right? So the goal would be jest would continue work "out of the box" via the detection mechanisms already in place, and if anyone wants to use something else, they would call |
That was my initial plan, yes. Not sure about the API yet. And I think we may want to cleanup Though we may want to setup a custom "flush microtask" method. But that's not important for a first iteration of this feature. |
Can you talk a bit more about this? Is that work being tracked anywhere, in a separate issue maybe? |
I've actually been using @sinonjs/fake-timers with @testing-library/[email protected], @testing-library/[email protected], [email protected], and [email protected] quite successfully - I had no idea I wasn't supposed to. Upgrading to the latest @testing-library with the new @testing-library/dom makes a whole bunch of my tests fail and spews out tons of warnings about |
It's not blocking this issue. |
I think setting different fake timers implementation shouldn't be in the scope of this library. It would make more sense for jest to allow different fake timers implementations. Argument for using fake timers with async utilities was that we shouldn't mix timers, but because jsdom is always using real timers we will always be dealing with both real and fake timers being used. |
What if you want to use this library with mocha? Or with some other non-jest test runner? |
if someone is using mocha and mock timers with sinon is this not supported? I think testing-library should work with any test runner + fake timers combination but should not require any extra api to do it. |
Is this true? I'm using this library to interact with the real DOM via web-test-runner, and it seems to work well, aside from the issues I've had with fake timers. |
I run my tests in Chrome using Karma and I never had any problems. 600 tests in 10K lines of code. |
According to this comment Jest fake timers are the only one supported. I'm using jest + sinon timers, and everything was working fine for me up until the latest release. This particular combination is a problem. dom-testing-library does some custom checking to see if jest timers are enabled (which will come back true in this case, even though jest timers are not enabled, because sinon will create a |
Any news on that topic? beforeEach(() => {
vi.useFakeTimers()
})
test('Component render', async () => {
const { container } = render(<MyComponent/>)
// I have to switch back to real timer otherwhise the await will never resolve
vi.runOnlyPendingTimers()
vi.useRealTimers()
const heading = await screen.findByRole('heading', { level: 1 })
expect(heading).toHaveTextContent('xxx')
} |
@KamiKillertO I am also use // for vitest
// see See https://sinonjs.org/releases/latest/fake-timers/
vi.useFakeTimers({ shouldAdvanceTime: true });
// for jest
// see https://jestjs.io/docs/jest-object#jestusefaketimersfaketimersconfig
jest.useFakeTimers({ advanceTimers: true }) I rather agree with @mjetek's sentiment that
I guess this enables real time advancement for all timers, whereas it probably would be ideal to only have real-time for
|
@ChristopherChudzicki Thanks. I've tested with |
Describe the feature you'd like:
This is an follow-on to #939. The request is to support fake timers other than jest. For instance, jest uses @sinonjs/fake-timers under the hood, and yet it's not possible to use @sinonjs/fake-timers directly with testing-library without jest. I would like to be able to use testing-library with @sinonjs/fake-timers without jest.
Suggested implementation:
@eps1lon had a good suggestion in #939 (comment) to decouple testing-library from jest timers, and @MatanBobi suggested doing so in a way that can be set up in the
configure
step instead of requiring wrappers.Describe alternatives you've considered:
I have tried to manually advance the clock with
clock.tick()
, and it works but causesact
warnings.Teachability, Documentation, Adoption, Migration Strategy:
I'm not certain what, if anything, will need to be done in user land to make this work. Ideally, it could be handled under-the-hood within this library, but I'm not sure how that could be done in a robust way.
The text was updated successfully, but these errors were encountered: