Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

different defaults for test and development #130

Open
misha-erm opened this issue Nov 6, 2020 · 5 comments
Open

different defaults for test and development #130

misha-erm opened this issue Nov 6, 2020 · 5 comments

Comments

@misha-erm
Copy link

Hi, bringing up the similar question to #32

The problem I'm trying to solve is to use different URI for mongo in tests because I'm dropping the entire collection after each test.

So what I want to achieve is something like that:

ENV_MONGO_URI: str({ devDefault: 'mongodb://mongo:27017/app', testDefault: 'mongodb://mongo:27017/app-test' })

If I understand correctly I can't use testOnly for that case since in development my uri will be undefined.

Thanks in advance!

@af
Copy link
Owner

af commented Nov 22, 2020

That's correct, testOnly('foo') will only use 'foo' as a default value when NODE_ENV is test. Since you probably can control your test environment, why not just add ENV_MONGO_URI=... as part of your testing command?

@misha-erm
Copy link
Author

misha-erm commented Nov 23, 2020

Yes, that's an option. But then I don't understand why we need devDefault since we can control dev environment too.

Currently I use a helper similar to testOnly, but it accepts two values: one for test and one for dev as a fallback. Maybe this version of testOnly where you can specify a fallback would be a bit more comfortable? What do you think?

@meredian
Copy link

meredian commented Nov 18, 2022

I second request for testOnly. Since NODE_ENV=test is first-class citizen (there's isTest), I think it worth having ability to have different defaults for dev & test will make things more consistent. Things behave somewhat different for "dev" (real service start for local development or staging environments) and "test" (run automated tests, units and integration, does not launch some global components). Simplest practical usecase is e.g logging, which is supressed by default in test, but not in dev.

 cleanEnv(
    inputEnv,
    {
      LOG_LEVEL: str<LevelWithSilent>({
        default: 'info',
        testDefault: 'silent',
        choices: ['fatal', 'error', 'warn', 'info', 'debug', 'trace', 'silent'],
      }),
   }
);

I can take time to implement that if necessary.

@DenisVASI9
Copy link

DenisVASI9 commented Dec 12, 2023

 if (rawValue === undefined) {
            // Use devDefault values only if NODE_ENV was explicitly set, and isn't 'production'
            var usingDevDefault = rawNodeEnv && rawNodeEnv !== 'production' && spec.hasOwnProperty('devDefault');

it's because of this condition in getSanitizedEnv

@DenisVASI9
Copy link

I end up just using class-transformer because I already have it in my nestjs project

 @Expose()
 @IsString()
 @Transform(({ value }) => process.env.NODE_ENV === 'test' ? 'postgresql://testuser:testpassword@localhost:7000/testdb' : value)
 DATABASE_URL!: string;
function loadConfig() {
const config = plainToInstance(EnvironmentConfig, process.env, { enableImplicitConversion: true, strategy: 'excludeAll' });
const errors = validateSync(config, { skipMissingProperties: false });

if (errors.length > 0) {
  throw new Error(`Configuration validation error: ${errors}`);
}

return config;
}

export const ENV = loadConfig();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants