mercredi 4 novembre 2020

nestjs pg test connection maps prod db from ConfigService windows

I experience some weird behavior regarding setting up a test postgres connection on linux contra windows. When I develop on windows I run my tests with

"test:e2e:win32": "set NODE_ENV=test && jest --config ./test/jest-e2e.json",

logging the process.env inside of the constructor body of app.module.ts AND the tests it successfully gives back test as expected. But the database which is created based on the ConfigService configuration

export default () => ({
  pg: {
    dialect: 'postgres',
    host: 'localhost',
    port: 5432,
    username: '...',
    password: '...',
    database:
      process.env.NODE_ENV === 'development'
        ? 'db_development'
        : process.env.NODE_ENV === 'staging'
        ? 'db_staging'
        : process.env.NODE_ENV === 'test'
        ? 'db_test'
        : 'db_production'
  },
});

creates the database as db_production.

app.module.ts

@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true,
      load: [configuration]
    }),
    ConfigModule.forFeature(configuration),
    SequelizeModule.forRootAsync({
      inject: [ConfigService],
      useFactory: async (configService: ConfigService) => ({
        host: configService.get<string>('pg.host'),
        port: configService.get<number>('pg.port'),
        dialect: 'postgres',
        username: configService.get<string>('pg.username'),
        password: configService.get<string>('pg.password'),
        database: configService.get<string>('pg.database'),
        models: [Store, Reservation, Clothing, User]
      })
    }),
  ],
})
export class RootModule {
  constructor(
    @InjectConnection()
    readonly connection
  ) {
//on linux it successfully yields db_test connection on windows it gives db_production
    console.log('connection', connection); 
//yields "test" BOTH on linux and windows
    console.log('env', process.env.NODE_ENV);
  }
}

When running it on linux it works as expected running with

"test:e2e:darwin:linux": "NODE_ENV=test && jest --config ./test/jest-e2e.json"

Not that I think the command is relevant, since the logs yields "test" in both environments

Aucun commentaire:

Enregistrer un commentaire