Skip to content

Commit

Permalink
test(): unexpected stringification of variables assigned to process.env
Browse files Browse the repository at this point in the history
After validation, environment variables are assigned back to
process.env. This leads to stringification of the values. This
stringification is deprecated and might result in a runtime error in
the future. See https://nodejs.org/api/process.html#processenv.

A special case is when an environment variable has the value
`undefined`. This causes `ConfigService.get` to fall back on
process.env thereby returning the stringified form `'undefined'`.
  • Loading branch information
MatthiasKunnen committed Jun 9, 2023
1 parent 0a2ca57 commit 5ee253c
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions tests/e2e/optional.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Test } from '@nestjs/testing';
import { AppModule } from '../src/app.module';
import { ConfigService } from '../../lib';

describe('Optional environment variables', () => {
it('should return undefined for optional variables', async () => {
const module = await Test.createTestingModule({
imports: [AppModule.withValidateFunction(() => ({
optional: undefined,
}))],
}).compile();

const app = module.createNestApplication();
await app.init();

const optional = module.get(ConfigService).get('optional')

expect(optional).toEqual(undefined)
});

it('should not assign complex objects back to process.env', async () => {
const module = await Test.createTestingModule({
imports: [AppModule.withValidateFunction(() => ({
complex: {hello: 'there'},
}))],
}).compile();

const app = module.createNestApplication();
await app.init();

expect(process.env.complex).toEqual(undefined)
});
});

0 comments on commit 5ee253c

Please sign in to comment.