jmcdo29 / jmcdo29/testing-nestjs

[NEW TEST] Sequelize transactions

Open
#1,985 1 comment 1 reaction 1 assignee Claimed by @jmcdo29 View on GitHub
enhancement
Dominant language
TypeScript
Stars
3k
Forks
375
PR merge metrics
No merged PRs in 30d

Description

### Is there an existing issue for this?

- [X] I have searched the existing issues

### Feature Test To Be Requested

The [sequelize-sample](https://github.com/jmcdo29/testing-nestjs/tree/main/apps/sequelize-sample) is really awesome, but we're using [Sequelize Transactions](https://docs.nestjs.com/techniques/database#sequelize-transactions) in our codebase and by doing so we have to mock the Sequelize `transaction()` method or Nest will throw dependency errors:

```
Nest can't resolve dependencies of the CatService (CatEntityRepository, ?). Please make sure that the argument Sequelize at index [5] is available in the RootTestModule context.

Potential solutions:
- Is RootTestModule a valid NestJS module?
- If Sequelize is a provider, is it part of the current RootTestModule?
- If Sequelize is exported from a separate @Module, is that module imported within RootTestModule?
@Module({
imports: [ /* the Module containing Sequelize */ ]
})
```

I've looked through the NestJS documentation and Discord but I couldn't find any good examples of how to do this because if I mock the Sequelize transaction like this, I don't get the value from my service call:
```ts
{
provide: Sequelize,
useValue: {
transaction: jest.fn(() => Promise.resolve()),
},
},
```

The Nest docs state to use a helper factory class but I couldn't find a good example of this either.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.