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

Implement vi.useFakeTimers, vi.setSystemTime, vi.useRealTimers like vitest #10275

Open
NateRadebaugh opened this issue Apr 15, 2024 · 2 comments
Labels
bun:test Something related to the `bun test` runner enhancement New feature or request

Comments

@NateRadebaugh
Copy link

What is the problem this feature would solve?

Test file in formatDate.test.ts

import { afterAll, beforeAll, describe, vi } from "vitest";

describe("formatDate", () => {
  beforeAll(() => {
    vi.useFakeTimers();
    vi.setSystemTime(new Date("2021-01-01T18:00:00Z"));
  });

  afterAll(() => {
    vi.useRealTimers();
  });
});

Run:

bun test formatDate

Error:

XXX> bun test formatDate        
bun test v1.1.3 (2615dc74)

XXX\formatDate.test.ts:
2 | import { formatDate } from "XXX/format";
3 | import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
4 |
5 | describe("formatDate", () => {
6 |   beforeAll(() => {
7 |     vi.useFakeTimers();
        ^
TypeError: vi.useFakeTimers is not a function. (In 'vi.useFakeTimers()', 'vi.useFakeTimers' is undefined)
      at XXX\formatDate.test.ts:7:5
      at XXX\formatDate.test.ts:5:1

 0 pass
 0 fail
Ran 0 tests across 1 files. [107.00ms]

What is the feature you are proposing to solve the problem?

There's already some magic that occurs within bun test when using jest methods via @jest/globals but it'd be great if the same logic could be extended to include vitest helper methods from vi so we don't need to change our test code in order to run with bun test.

What alternatives have you considered?

  • Rewrite tests to use @jest/globals. However, jest is much slower than vitest
  • Rewrite bun:test. However bun:test does not have first-class vscode extension integration like vitest
@NateRadebaugh NateRadebaugh added the enhancement New feature or request label Apr 15, 2024
@Electroid Electroid changed the title bun test should drop-in replace vi.useFakeTimers + vi.setSystemTime + vi.useRealTimers using vitest like it does with jest globals Implement vi.useFakeTimers, vi.setSystemTime, vi.useRealTimers like vitest Apr 15, 2024
@Electroid Electroid added the bun:test Something related to the `bun test` runner label Apr 15, 2024
@BlakeEddie
Copy link

If this is too much work as a quick fix how hard would it be to add to the bunfig.toml the option to use the package.json test script ignoring the bun testing tool?

@NateRadebaugh
Copy link
Author

If this is too much work as a quick fix how hard would it be to add to the bunfig.toml the option to use the package.json test script ignoring the bun testing tool?

I don't think that is necessary as a workaround -- we already have the ability to use "bun run test" to run the package.json test script even with bun as the package manager.

My goal here is to be able to use bun in CI for perf before forcing all my devs to worry about installing/using bun explicitly as part of local development, so bun supporting the baked-in vitest functionality is preferred without any other workarounds.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bun:test Something related to the `bun test` runner enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants