nestjs / typeorm

TypeORM module for Nest framework (node.js) 🍇

Home Page:https://nestjs.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

getCustomRepositoryToken is not fired in unit test

railsstudent opened this issue · comments

I'm submitting a...


[ ] Regression 
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

getCustomRepositoryToken() is not injected in unit test and unit test fires getCustomRepostory and look up default connection in connection manager

Expected behavior

I expect getCustomRepositoryToken is called to mock default connection and the entity repository for unit test.
Then, my test cases are run to verify the correctness of logic

Minimal reproduction of the problem with instructions

  1. Create a custom repository that extends Repository
import { EntityRepository, Repository } from 'typeorm'
import { BillingPlan, BILLING_PLAN_TYPE } from '../entity'

@EntityRepository(BillingPlan)
export class BillPlanRepository extends Repository<BillingPlan> {
  async getDiscount(planType: BILLING_PLAN_TYPE): Promise<number> {
    const billingPlan = await this.findOne({
      where: {
        type: planType
      }
    })

    return billingPlan ? billingPlan.discount : 0
  }
}
  1. Use the custom repository in a service
  async getSubscriptionPrices(): Promise<any> {
    const billingPlanRepo = getCustomRepository(BillPlanRepository)
    const monthlyDiscount = await billingPlanRepo.getDiscount(BILLING_PLAN_TYPE.MONTHLY)
    const annualDiscount = await billingPlanRepo.getDiscount(BILLING_PLAN_TYPE.ANNUALLY)

     return {
         totalPricePaidMonthly: 100 * (1 - monthlyDiscount) ,
         totalPricePaidAnnually: 100 * (1 - annualDiscount) 
     }
  }
  1. Test the service
beforeEach(async () => {
    const module: TestingModule = await Test.createTestingModule({
      providers: [
        StripeService,
        {
          provide: getCustomRepositoryToken(BillPlanRepository),
          useFactory: () => ({
            getDiscount(planType: BILLING_PLAN_TYPE) {
              if (planType === BILLING_PLAN_TYPE.MONTHLY) {
                return 0
              } else if (planType === BILLING_PLAN_TYPE.ANNUALLY) {
                return 0.12
              }
              return 0
            }
          })
        },
      ],
    }).compile()

    service = module.get<StripeService>(StripeService)
  })

  it('should return correct subscription prices', async () => {
    const response = await service.getSubscriptionPrices()
    expect(response.totalPricePaidMonthly).toEqual(100)
    expect(response.totalPricePaidAnnually).toEqual(88)
  })

When I run the test case, getCustomRepository is fired and error message " ConnectionNotFoundError: Connection "default" was not found." is issued

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

Environment


Nest version: 7.6.5

 
For Tooling issues:
- Node version: 10.18.1
- Platform:  Ubuntu

Others:

Please, use our Discord channel (support) for such questions. We are using GitHub to track bugs, feature requests, and potential improvements.

@kamilmysliwiec The discord link you posted here did not work. I could not input my email address and password to claim account. Please kindly help. Thanks.

The link is expired. Thanks