You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
importtype{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";(globalasany).jest=jest;typeMocked<T>={[PinkeyofT]: T[P]extends(...args: infer A)=> infer R ? Mock<(...args: A)=>R> : T[P];};describe("UserService",()=>{letuserService!: UserService;letentityManager!: 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
Although I haven't personally used Automock with Bun, it's good to know that it can :)
It sounds like a good idea. It adopts a slightly different strategy, though. Instead of using jest from bun:test, I consider making a new TestBed specifically for Bun, using its mockFn (reference). This is not how users will have to use Jest with Bun.
Consider it as follows:
import{beforeAll,describe,expect,it}from"bun:test";import{TestBed,Mocked}from"@automock/bun";import{EntityManager}from"@mikro-orm/postgresql";import{UserService}from"~/components";describe("UserService",()=>{letuserService!: UserService;letentityManager!: 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();});});
Yeah I've suggested using jest instead of mockFn to be easier to write, since jest package is already written. But using mockFn will be better since it's use bun official mock api.
Activity
omermorad commentedon Jan 11, 2024
Hi @ceopaludetto,
Thank you :)
And thanks for posting this.
Although I haven't personally used Automock with Bun, it's good to know that it can :)
It sounds like a good idea. It adopts a slightly different strategy, though. Instead of using
jestfrombun:test, I consider making a newTestBedspecifically for Bun, using itsmockFn(reference). This is not how users will have to use Jest with Bun.Consider it as follows:
What do you think?
ceopaludetto commentedon Jan 17, 2024
Yeah I've suggested using
jestinstead ofmockFnto be easier to write, since jest package is already written. But usingmockFnwill be better since it's use bun official mock api.