Skip to content

Commit

Permalink
Merge pull request #6 from simplyStyle/main
Browse files Browse the repository at this point in the history
feat: complement unit testing
  • Loading branch information
tianyingchun authored May 27, 2024
2 parents 067ba8e + 697bc81 commit 0f98548
Showing 1 changed file with 102 additions and 12 deletions.
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"/);
});
});

0 comments on commit 0f98548

Please sign in to comment.