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

feat: complement unit testing #6

Merged
merged 2 commits into from
May 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 102 additions & 12 deletions tests/dotenv.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,12 @@ describe('test suites of hyper env', () => {
'next',
'build'
);
console.log(stdout);
expect(stderr).toBe('');
expect(stdout).not.toMatch(/foo: bar/);
});

it('parses env without next.env', async () => {
writeEnvFile('.env.test', 'NEXT_PUBLIC_FOO=test');
it('parses env without next .env', async () => {
writeEnvFile('.env', 'NEXT_PUBLIC_FOO=dev');

const { stderr, stdout } = await runTsCliMock(
cliPath,
Expand All @@ -68,27 +67,28 @@ describe('test suites of hyper env', () => {
);
expect(stderr).toBe('');
expect(stdout).toMatch(/node_env: test/);
expect(readNextPage()).toMatch(/"hello:","test"/);
expect(readNextPage()).toMatch(/"hello:","dev"/);
});

it('parses env with --path,-p', async () => {
writeEnvFile('.env.staging', 'NEXT_PUBLIC_FOO=bar');

it('parse env files via --env arg', async () => {
writeEnvFile('.env.staging', 'NEXT_PUBLIC_FOO=env_staging');
// APP_ENV=staging hyper-env --env APP_ENV -- next build
vi.stubEnv('APP_ENV', 'staging');
const { stderr, stdout } = await runTsCliMock(
cliPath,
'--path',
'.env.staging',
'--env',
'APP_ENV',
'--',
'next',
'build'
);
expect(stderr).toBe('');
expect(stdout).toMatch(/node_env: test/);
expect(readNextPage()).toMatch(/"hello:","bar"/);
expect(readNextPage()).toMatch(/"hello:","env_staging"/);
});

it('parses env with --env,-e', async () => {
writeEnvFile('.env.staging2', 'NEXT_PUBLIC_FOO=staging2');
it('parse env files via --e arg', async () => {
writeEnvFile('.env.staging2', 'NEXT_PUBLIC_FOO=e_staging2');
// APP_ENV=staging2 hyper-env --env APP_ENV -- next build
vi.stubEnv('APP_ENV', 'staging2');
const { stderr, stdout } = await runTsCliMock(
Expand All @@ -101,5 +101,95 @@ describe('test suites of hyper env', () => {
);
expect(stderr).toBe('');
expect(stdout).toMatch(/node_env: test/);
expect(readNextPage()).toMatch(/"hello:","e_staging2"/);
});

it('parses env files via --path arg', async () => {
writeEnvFile('.env.staging', 'NEXT_PUBLIC_FOO=path_staging');

const { stderr, stdout } = await runTsCliMock(
cliPath,
'--path',
'.env.staging',
'--',
'next',
'build'
);
expect(stderr).toBe('');
expect(stdout).toMatch(/node_env: test/);
expect(readNextPage()).toMatch(/"hello:","path_staging"/);
});

it('parses env files via --p arg', async () => {
writeEnvFile('.env.staging2', 'NEXT_PUBLIC_FOO=p_staging2');

const { stderr, stdout } = await runTsCliMock(
cliPath,
'--p',
'.env.staging2',
'--',
'next',
'build'
);
expect(stderr).toBe('');
expect(stdout).toMatch(/node_env: test/);
expect(readNextPage()).toMatch(/"hello:","p_staging2"/);
});

it('has order of priority', async () => {
writeEnvFile('.env.staging', 'NEXT_PUBLIC_FOO=staging');
writeEnvFile('.env.local', 'NEXT_PUBLIC_FOO=local');
writeEnvFile('.env.production', 'NEXT_PUBLIC_FOO=production');
writeEnvFile('.env', 'NEXT_PUBLIC_FOO=dev');

vi.stubEnv('APP_ENV', 'production');

const testRes1 = await runTsCliMock(
cliPath,
'--p',
'.env.staging',
'--e',
'APP_ENV',
'--',
'next',
'build'
);

expect(testRes1.stderr).toBe('');
expect(testRes1.stdout).toMatch(/node_env: test/);
expect(readNextPage()).toMatch(/"hello:","staging"/);

const testRes2 = await runTsCliMock(
cliPath,
'--e',
'APP_ENV',
'--p',
'.env.staging',
'--',
'next',
'build'
);

expect(testRes2.stderr).toBe('');
expect(testRes2.stdout).toMatch(/node_env: test/);
expect(readNextPage()).toMatch(/"hello:","staging"/);
});

it('has no args with order of priority', async () => {
writeEnvFile('.env.development', 'NEXT_PUBLIC_FOO=development');
writeEnvFile('.env.production', 'NEXT_PUBLIC_FOO=production');
writeEnvFile('.env.local', 'NEXT_PUBLIC_FOO=local');
writeEnvFile('.env.test', 'NEXT_PUBLIC_FOO=test');
writeEnvFile('.env', 'NEXT_PUBLIC_FOO=dev');

const { stderr, stdout } = await runTsCliMock(
cliPath,
'--',
'next',
'build'
);
expect(stderr).toBe('');
expect(stdout).toMatch(/node_env: test/);
expect(readNextPage()).toMatch(/"hello:","production"/);
});
});