【问题标题】:nestjs pg test connection maps prod db from ConfigService windowsnestjs pg 测试连接从 ConfigService 窗口映射 prod db
【发布时间】:2021-02-16 23:21:19
【问题描述】:

关于在 linux contra windows 上设置测试 postgres 连接时,我遇到了一些奇怪的行为。当我在 Windows 上开发时,我会使用

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

app.module.ts 的构造函数主体内记录process.env AND 它成功返回test 的测试符合预期。但是基于ConfigService配置创建的数据库

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'
  },
});

将数据库创建为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);
  }
}

在 linux 上运行时,它可以正常运行

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

我认为该命令无关紧要,因为 logs 在两种环境中都会产生“测试”

【问题讨论】:

    标签: typescript postgresql testing connection nestjs


    【解决方案1】:

    它考虑了测试命令中的空格。 它通过改变来解决它

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

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

    【讨论】:

      猜你喜欢
      • 2022-06-17
      • 2019-06-02
      • 2022-08-19
      • 2019-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-06
      • 1970-01-01
      相关资源
      最近更新 更多