Skip to content

Implement bun:test support #247

@ceopaludetto

Description

@ceopaludetto

Is there an existing issue for this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe it

Since bun supports jest.fn I was able to use @automock/jest in my test by supplying jest in global. Working example:

import type { Mock } from "bun:test";

import { beforeAll, describe, expect, it, jest } from "bun:test";
import { TestBed } from "@automock/jest";
import { EntityManager } from "@mikro-orm/postgresql";
import { UserService } from "~/components";

(global as any).jest = jest;

type Mocked<T> = {
	[P in keyof T]: T[P] extends (...args: infer A) => infer R ? Mock<(...args: A) => R> : T[P];
};

describe("UserService", () => {
	let userService!: UserService;
	let entityManager!: Mocked<EntityManager>;

	beforeAll(() => {
		const { unit, unitRef } = TestBed.create(UserService).compile();

		userService = unit;
		entityManager = unitRef.get(EntityManager);
	});

	it("#findAll", () => {
		entityManager.findAndCount.mockResolvedValue([[], 0]);

		expect(userService.findAll({ limit: 10, offset: 0 })).resolves.toEqual([[], 0]);
		expect(entityManager.findAndCount).toHaveBeenCalled();
	});
});

Bun also doesn't have Mocked type by default, so I've created one instead.

Describe the solution you'd like

Provide a @automock/bun which is a copy of @automock/jest but relying on import { jest } from 'bun:test'; instead of global.jest. Also reexport the type Mocked for convenience(I dunno if this type should be recursive).

What is the motivation / use case for changing the behavior?

This library is awesome and bun test runner is fast af. Both combined make NestJS tests enjoyable

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions