nestjs pg 测试连接映射来自 ConfigService 的 prod db windows

nestjs pg test connection maps prod db from ConfigService windows

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

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

app.module.ts 的构造函数体内记录 process.env 和 它按预期成功返回 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 && jest --config ./test/jest-e2e.json"

并不是说我认为该命令是相关的,因为 logs 在两种环境中都会产生“测试”

它考虑了测试命令中的space。 它通过更改

来修复它
"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"