【发布时间】: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 && jest --config ./test/jest-e2e.json"
我认为该命令无关紧要,因为 logs 在两种环境中都会产生“测试”
【问题讨论】:
标签: typescript postgresql testing connection nestjs