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

No connection options found in NestJS application #114

Open
delucca opened this issue Feb 6, 2021 · 3 comments
Open

No connection options found in NestJS application #114

delucca opened this issue Feb 6, 2021 · 3 comments

Comments

@delucca
Copy link

delucca commented Feb 6, 2021

Hi!

I'm trying to integrate typeorm-seeding in my NestJS application to use it for test data seeding. My NestJS structure does not use an ormconfig file, it defines the config at runtime, like the following:

import { Module } from '@nestjs/common'
import { TypeOrmModule } from '@nestjs/typeorm'

import databaseConfig from 'src/config/database/config'
...

@Module({
  imports: [
    TypeOrmModule.forRoot(databaseConfig),
    TypeOrmModule.forFeature([DomainCycleRepository]),
    ,,,
  ],
  ,,,
})
class DomainCycleModule {}

export default DomainCycleModule

I've done like that to use environment variables and isolate that usage only to my ConfigService.

Anyway, I've added all required configs, following the how-to in the README docs, and ended up with something like this in a test file:

import { Test, TestingModule } from '@nestjs/testing'
import { TypeOrmModule } from '@nestjs/typeorm'
import { getRepository } from 'typeorm'
import { setConnectionOptions, useSeeding, tearDownDatabase, factory } from 'typeorm-seeding'

import databaseConfig from 'src/config/database/config'
import DomainCycleRepository from 'src/domain/cycle/repository'
import DomainTeamModule from 'src/domain/team'
import { User } from 'src/domain/user/entities'

import DomainCycleService from './service'

const buildTestModule = () =>
  Test.createTestingModule({
    imports: [
      TypeOrmModule.forRoot(databaseConfig),
      TypeOrmModule.forFeature([DomainCycleRepository]),
      DomainTeamModule,
    ],
    providers: [DomainCycleService],
  })

describe('DomainCycleService', () => {
  let module: TestingModule
  let domainCycleService: DomainCycleService

  beforeAll(async () => {
    module = await buildTestModule().compile()
    domainCycleService = module.get<DomainCycleService>(DomainCycleService)

    setConnectionOptions({
      type: 'sqlite',
      database: ':memory:',
      entities: [User],
    })
    await useSeeding()
  })

  afterAll(async () => {
    await module.close()
    await tearDownDatabase()
  })

  describe('get a single cycle', () => {
    it('can get a single company cycle, constrained to company', async () => {
      const createdUser = await factory(User)().create()
      const fakeUsers = await getRepository(User).find({})

      console.log(fakeUsers, createdUser)

      expect(true).toEqual(false)
    })
  })
})

And I get two errors:

  ● DomainCycleService › get a single cycle › can get a single company cycle, constrained to company

    No connection options were found in any orm configuration files.

      at ConnectionOptionsReader.<anonymous> (src/connection/ConnectionOptionsReader.ts:42:19)
      at step (node_modules/typeorm/node_modules/tslib/tslib.js:141:27)
      at Object.next (node_modules/typeorm/node_modules/tslib/tslib.js:122:57)
      at fulfilled (node_modules/typeorm/node_modules/tslib/tslib.js:112:62)

And:

  ● DomainCycleService › get a single cycle › can get a single company cycle, constrained to company

    TypeError: Cannot read property 'factory' of undefined

      44 |   describe('get a single cycle', () => {
      45 |     it('can get a single company cycle, constrained to company', async () => {
    > 46 |       const createdUser = await factory(User)().create()
         |                                              ^
      47 |       const fakeUsers = await getRepository(User).find({})
      48 |
      49 |       console.log(fakeUsers, createdUser)

      at node_modules/typeorm-seeding/src/typeorm-seeding.ts:40:79
      at Object.<anonymous> (src/domain/cycle/cycle.spec.ts:46:46)

Also, here is my testConfig file:

import { Cycle } from 'src/domain/cycle/entities'
import { KeyResultCheckIn } from 'src/domain/key-result/check-in/entities'
import { KeyResultCustomList } from 'src/domain/key-result/custom-list/entities'
import { KeyResult } from 'src/domain/key-result/entities'
import { Objective } from 'src/domain/objective/entities'
import { Team } from 'src/domain/team/entities'
import { User } from 'src/domain/user/entities'

export const testConfig = {
  type: 'sqlite' as any,
  database: ':memory:',
  dropSchema: true,
  synchronize: true,
  logging: false,
  entities: [Cycle, KeyResult, KeyResultCheckIn, KeyResultCustomList, Objective, Team, User],
  factories: ['/home/delucca/Code/delucca/business/src/domain/user/factory.ts'],
  seeds: ['/home/delucca/Code/delucca/business/src/domain/user/seed.ts'],
}

My factory:

import * as Faker from 'faker'
import { define } from 'typeorm-seeding'

import { optionalValue } from 'lib/jest/faker'
import { User } from './entities'
import { USER_GENDER } from './constants'

define(User, (faker: typeof Faker) => {
  const firstName = faker.name.firstName()
  const authzSub = faker.random.uuid()

  const lastName = faker.name.lastName()
  const gender = faker.helpers.randomize(Object.values(USER_GENDER))
  const role = faker.company.catchPhraseNoun()
  const picture = faker.internet.avatar()

  const user = new User()
  user.firstName = firstName
  user.authzSub = authzSub
  user.lastName = optionalValue(lastName)
  user.gender = optionalValue(gender)
  user.role = optionalValue(role)
  user.picture = optionalValue(picture)

  return user
})

And my seed:

import { Factory, Seeder } from 'typeorm-seeding'

import { User } from './entities'

export default class CreateUsers implements Seeder {
  public async run(factory: Factory) {
    await factory(User)().createMany(5)
  }
}

Anyone know what should I do to make it work?

@metalcamp
Copy link
Collaborator

Can you share link to example app or even the actual repo of this app?

@MoreiraJorge
Copy link

MoreiraJorge commented Feb 2, 2022

Any updates on this issue guys? I'm also trying to seed a in memory DB without having the orm config file, but i have the configuration in the test module, and i have the same error.

@jorgebodega
Copy link
Collaborator

Hi! Could you check the fork on #201 if this is working? No new versions could be deployed here.

This part have been refactored, maybe is fixed now.

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

4 participants