Skip to content

Make bun test isolated #6024

@erwinstone

Description

@erwinstone

What is the problem this feature would solve?

Leaks across files.

Example:

// data.ts
export const data = {
  name: 'User',
}
// test1.test.ts
import { expect, test } from 'bun:test'
import { data } from './data'

data.name = 'Admin'

test('test-1', () => {
  expect(data).toEqual({
    name: 'Admin',
  })
})
// test2.test.ts
import { expect, test } from 'bun:test'
import { data } from './data'

test('test-2', () => {
  expect(data).toEqual({
    name: 'User',
  })
})

test2 will fail because the data has been modified in test1.

bun test v1.0.3 (25e69c71)

test/test1.test.ts:
✓ test-1 [0.08ms]

test/test2.test.ts:
1 | import { expect, test } from 'bun:test'
2 | import { data } from './data'
3 | 
4 | test('test-2', () => {
5 |   expect(data).toEqual({
      ^
error: expect(received).toEqual(expected)

  {
+   name: "Admin"
-   name: "User"
  }

- Expected  - 1
+ Received  + 1

In vitest and jest, both tests pass.

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

Isolated by default, but can be disabled using a parameter.
Ref: https://vitest.dev/config/isolate.html

What alternatives have you considered?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bun:testSomething related to the `bun test` runnerenhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions