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

How to mock dataSource #7

Open
MBach opened this issue Jan 6, 2025 · 1 comment
Open

How to mock dataSource #7

MBach opened this issue Jan 6, 2025 · 1 comment

Comments

@MBach
Copy link

MBach commented Jan 6, 2025

Hello,

In your documentation, you're assuming that we can access the datasource directly. However, I have a big service like:

@Injectable()
export class ChuteService extends BaseService {
    constructor(
        private dataSource: DataSource,
        
        @InjectRepository(SomeRepo)
        private someRepo: Repository<SomeRepo>,
        
    ) {
        super()
    }
}

Inside, for some methods I'm using transactions like:

public async updateSomeAttribute(
        attrId : string
    ): Promise<SomeOtherEntity> {
        const queryRunner = this.dataSource.createQueryRunner()
        await queryRunner.connect()
        await queryRunner.startTransaction()
        try {
            let obj: SomeOtherEntity= await queryRunner.manager
                .createQueryBuilder(SomeOtherEntity, "s")
                .innerJoinAndSelect("s.anotherOne", "i")
                .where("s.id = :attrId", { attrId })
                .getOneOrFail()

          
            ent = await queryRunner.manager.save<SomeOtherEntity>(entity)
            await queryRunner.commitTransaction()
            return ent 
        } catch (error) {
            await queryRunner.rollbackTransaction()
            throw new BadRequestException(error, "Transaction failed")
        } finally {
            await queryRunner.release()
        }
    }

How can I use your package in my test? I'd like to reflect that some methods are called in my unit test, and to be able to use the dataSource from my service, but it's injected in my real service

describe("Update some attr", () => {
        it("should update some attr and return a Entity or null", async () => {
            
            const typeorm = new MockTypeORM()
            typeorm.onMock(SomeEntity).toReturn(entity, "getOneOrFail")

            const queryRunner = dataSource.createQueryRunner()
            queryRunner.manager.createQueryBuilder()

            // Call the Service
            const result = await myService.updateSomeAttribute(attrId)

            expect(queryRunner.connect).toHaveBeenCalled()
            expect(queryRunner.startTransaction).toHaveBeenCalled()
            expect(queryRunner.commitTransaction).toHaveBeenCalled()
            expect(queryRunner.release).toHaveBeenCalled()
        })
    })
@jazimabbas
Copy link
Owner

I haven't tested on Nestjs, but I'll take a look into your example this weekend and then will let you know. Sorry I am overloaded with projects right now. But I'll definitely take a look this weekend.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants