bun icon indicating copy to clipboard operation
bun copied to clipboard

`bun test`

Open Electroid opened this issue 2 years ago • 98 comments
trafficstars

Jest

describe()

test()

Lifecycle hooks

expect()

Mocks

Misc

Vitest

expect()

Mocks

Timers

Misc

jest-extended

expect()

Electroid avatar Jan 18 '23 03:01 Electroid

ts-jest does some magical hoisting with mock* functions which may or may not be a challenge with bun: https://github.com/swc-project/swc/issues/5448#issuecomment-1386969864

useafterfree avatar Jan 18 '23 21:01 useafterfree

This issue can be updated to check off these four (they were implemented in PR https://github.com/oven-sh/bun/pull/1573):

  • .toBeGreaterThan(number | bigint)
  • .toBeGreaterThanOrEqual(number | bigint)
  • .toBeLessThan(number | bigint)
  • .toBeLessThanOrEqual(number | bigint)

nskins avatar Mar 11 '23 16:03 nskins

Should jest.mock be added as well? Doesn't seem to be tracked.

erikshestopal avatar Jun 26 '23 20:06 erikshestopal

@erikshestopal please give it a try in the canary build, a lot of it is in there

Jarred-Sumner avatar Jun 26 '23 20:06 Jarred-Sumner

jest.clearAllMocks() is also missing 👀 🙏

image

brapifra avatar Jun 27 '23 11:06 brapifra

Would there be any scope to add DOM matchers? If so, I'd love for some parity with the jest-dom library.

itsezc avatar Jul 07 '23 04:07 itsezc

2023-07-07_10-56

toThrowError is missing. It's a alias for toThrow.

Ref: https://jestjs.io/docs/expect#tothrowerror

Hanaasagi avatar Jul 07 '23 08:07 Hanaasagi

test.each and describe.each are super helpful to build data-driven unit tests.

riywo avatar Jul 10 '23 17:07 riywo

I am proposing adding https://vitest.dev/api/expect-typeof.html to the vitest list.

simylein avatar Jul 14 '23 18:07 simylein

test.each and describe.each are super helpful to build data-driven unit tests.

It's being worked on in PR #4047

ghiscoding avatar Aug 08 '23 15:08 ghiscoding

Are there plans to add the expect.toBeInTheDocument matcher from @testing-library/jest-dom that many typically use for JSX component testing? Or is that out of scope? Feels like that would be something useful to provide out of the box.

dsanchezjt avatar Aug 10 '23 18:08 dsanchezjt

expect.objectContaining

any ideas how to replace this call in tests?

Solution / Workaround

    expect(Object.fromEntries(data.statistics[Kpi.codes])).toHaveProperty(`502`, 1)
    expect(Object.fromEntries(data.statistics[Kpi.codes])).toHaveProperty(`200`, 32)

// instead of:

    expect(Object.fromEntries(data.statistics[Kpi.codes])).toStrictEqual(
      expect.objectContaining({ '200': 32, '502': 1 })
    )

OleksandrKucherenko avatar Aug 17 '23 07:08 OleksandrKucherenko

What can you recommend as a replacement for such jest code?

await expect(async () => await askGPT(text, prompt, context)).rejects.toThrowError(`⛔ API error.`)

OleksandrKucherenko avatar Aug 29 '23 09:08 OleksandrKucherenko

Shouldn't that be working?

Does this suit your needs?

import { describe, expect, test } from 'bun:test';

const askGPT = async (text: string, prompt: string, context: string): Promise<void> => {
	throw new Error('⛔ API error.');
};

describe('', () => {
	test('', async () => {
		await expect(async () => await askGPT('text', 'prompt', 'context')).toThrow(Error(`⛔ API error.`));
	});
});

simylein avatar Aug 29 '23 10:08 simylein

Edit: these functions have been added to the list, now, thank you!!


Should jest.mock be added as well? Doesn't seem to be tracked.

This still seems to not be on the list. Could we get support for jest.mock and jest.requireActual compatibility APIs added as an official target on this list? Module mocking is a pretty important part of jest compatibility.

Example:

import { foo } from 'some-module'

jest.mock('some-module', () => ({
  ...jest.requireActual<object>('some-module'),
  foo: jest.fn(() => 'foo'),
}))

The pattern here allows you to override just the foo method of some-module, while requireActual gives you the real implementation for the rest.

Jest also supports placing an override for some-module in a __mocks__/some-module.ts file, as another way to automatically module-mock, though imho that is less priority.

TroyAlford avatar Sep 07 '23 21:09 TroyAlford

Shouldn't that be working?

Does this suit your needs?

import { describe, expect, test } from 'bun:test';

const askGPT = async (text: string, prompt: string, context: string): Promise<void> => {
	throw new Error('⛔ API error.');
};

describe('', () => {
	test('', async () => {
		await expect(async () => await askGPT('text', 'prompt', 'context')).toThrow(Error(`⛔ API error.`));
	});
});

This works only in bun, but not in vitest, so it's not compatible. It seems .rejects.toThrow is not implemented correctly or at all in bun, it throws Expected value must be a function on code like this that works in vitest:

await expect(asyncFn()).rejects.toThrow()

silverwind avatar Sep 09 '23 00:09 silverwind

There is an issue on spyon https://github.com/oven-sh/bun/issues/4482

coratgerl avatar Sep 09 '23 12:09 coratgerl

Would love to see these implemented:

  • test.concurrent
  • test.concurrent.each
  • test.each

Even basic half-broken implementation of concurrent (like in jest) would be awesome, as it's absolutely vital for various slow integration tests involving external APIs and services. I'm saying half-broken because in jest before* and after* hooks don't work correctly with concurrent, but it's still usable - you can call the setup/teardown logic directly from each test.

supersmile2009 avatar Sep 11 '23 08:09 supersmile2009

Should jest.mock be added as well? Doesn't seem to be tracked.

This still seems to not be on the list. Could we get support for jest.mock and jest.requireActual compatibility APIs added as an official target on this list? Module mocking is a pretty important part of jest compatibility.

Example:

import { foo } from 'some-module'

jest.mock('some-module', () => ({
  ...jest.requireActual<object>('some-module'),
  foo: jest.fn(() => 'foo'),
}))

The pattern here allows you to override just the foo method of some-module, while requireActual gives you the real implementation for the rest.

Jest also supports placing an override for some-module in a __mocks__/some-module.ts file, as another way to automatically module-mock, though imho that is less priority.

Well put together @TroyAlford I think this is a very clear implementation maybe you want to create a new issue?

coolxeo avatar Sep 11 '23 14:09 coolxeo

Are there plans to add the expect.toBeInTheDocument matcher from @testing-library/jest-dom that many typically use for JSX component testing? Or is that out of scope? Feels like that would be something useful to provide out of the box.

Very much looking for information on this as well to be honest.

riezahughes avatar Sep 13 '23 15:09 riezahughes

Could we get support for jest.mock and jest.requireActual compatibility APIs

Well put together @TroyAlford I think this is a very clear implementation maybe you want to create a new issue?

This has been added to the list above, now. Thank you!! :) As suggested by @coolxeo, I've also opened a new issue in relation for tracking the sub-task: https://github.com/oven-sh/bun/issues/5394.

TroyAlford avatar Sep 14 '23 17:09 TroyAlford

Shouldn't that be working? Does this suit your needs?

import { describe, expect, test } from 'bun:test';

const askGPT = async (text: string, prompt: string, context: string): Promise<void> => {
	throw new Error('⛔ API error.');
};

describe('', () => {
	test('', async () => {
		await expect(async () => await askGPT('text', 'prompt', 'context')).toThrow(Error(`⛔ API error.`));
	});
});

This works only in bun, but not in vitest, so it's not compatible. It seems .rejects.toThrow is not implemented correctly or at all in bun, it throws Expected value must be a function on code like this that works in vitest:

await expect(asyncFn()).rejects.toThrow()

I have the exact same issue: it is not ISO from jest to bun. expect().rejects.toThrow breaks in bun while working in Jest.

vidup avatar Sep 15 '23 16:09 vidup

Shouldn't that be working? Does this suit your needs?

import { describe, expect, test } from 'bun:test';

const askGPT = async (text: string, prompt: string, context: string): Promise<void> => {
	throw new Error('⛔ API error.');
};

describe('', () => {
	test('', async () => {
		await expect(async () => await askGPT('text', 'prompt', 'context')).toThrow(Error(`⛔ API error.`));
	});
});

This works only in bun, but not in vitest, so it's not compatible. It seems .rejects.toThrow is not implemented correctly or at all in bun, it throws Expected value must be a function on code like this that works in vitest:

await expect(asyncFn()).rejects.toThrow()

I have the exact same issue: it is not ISO from jest to bun. expect().rejects.toThrow breaks in bun while working in Jest.

Same here, it seems like we cannot await on expect also

Screenshot 2023-09-15 at 17 10 19

mrherickz avatar Sep 15 '23 20:09 mrherickz

Are there plans to add the expect.toBeInTheDocument matcher from @testing-library/jest-dom that many typically use for JSX component testing? Or is that out of scope? Feels like that would be something useful to provide out of the box.

I also need this feature for component testing

ImBIOS avatar Sep 23 '23 09:09 ImBIOS

Are there plans to add the expect.toBeInTheDocument matcher from @testing-library/jest-dom that many typically use for JSX component testing? Or is that out of scope? Feels like that would be something useful to provide out of the box.

I also need this feature for component testing

i've asked a few times on their discord but nobodies been able to give any answer at all regarding this.

riezahughes avatar Sep 23 '23 09:09 riezahughes

@riezahughes we will add support for expect.extend and get @testing-library/jest-dom to work

Jarred-Sumner avatar Sep 23 '23 10:09 Jarred-Sumner

Another thing to consider for compatibility is the ability to configure custom file names to pick up: https://github.com/oven-sh/bun/discussions/5880

In jest you can do this with https://jestjs.io/docs/configuration#testregex-string--arraystring

Almenon avatar Sep 23 '23 18:09 Almenon

@Jarred-Sumner @Electroid Hi, is there any roadmap or planned date of release for this missing features? Personally interested in concurrent tests, as it could speed up some tests significantly.  By the way do you plan to create some sort of fixtures for tests? I think it is big pain point for a lot of people. Right now we use a workaround, but it would be nice to see it in standard bun testing library. P.S. by fixtures I mean some objects that are reused between tests and there is a ways for tuning its lifetime

Voldemat avatar Sep 26 '23 11:09 Voldemat

@riezahughes we will add support for expect.extend and get @testing-library/jest-dom to work

This is the primary thing keeping us from adopting bun.

stevensacks avatar Oct 04 '23 19:10 stevensacks

@riezahughes we will add support for expect.extend and get @testing-library/jest-dom to work

This is the primary thing keeping us from adopting bun.

This is also the only thing keeping us from adopting bun.

mgasner avatar Oct 11 '23 02:10 mgasner