Files
vercel/packages/build-utils/test/unit.get-prefixed-env-vars.test.ts
Steven 2b57e12ad3 [build-utils] Refactor framework prefixed env vars into shared function (#8166)
This consolidates the logic to get the framework-specific prefixed System Environment Variables into a single shared function so each builder can reuse the same function.

- Related to #7009 
- Related to #8306

In the future, this feature could be added to any other missing builders as well as `vc dev` but we'll save that for a new PR.
2022-08-05 00:51:52 +00:00

88 lines
2.2 KiB
TypeScript
Vendored

import { getPrefixedEnvVars } from '../src';
describe('Test `getPrefixedEnvVars()`', () => {
const cases: Array<{
name: string;
args: Parameters<typeof getPrefixedEnvVars>[0];
want: ReturnType<typeof getPrefixedEnvVars>;
}> = [
{
name: 'should work with NEXT_PUBLIC_',
args: {
envPrefix: 'NEXT_PUBLIC_',
envs: {
VERCEL: '1',
VERCEL_URL: 'example.vercel.sh',
USER_ENV_VAR_NOT_VERCEL: 'example.com',
FOO: 'bar',
},
},
want: {
NEXT_PUBLIC_VERCEL_URL: 'example.vercel.sh',
TURBO_CI_VENDOR_ENV_KEY: 'NEXT_PUBLIC_VERCEL_',
},
},
{
name: 'should work with GATSBY_',
args: {
envPrefix: 'GATSBY_',
envs: {
USER_ENV_VAR_NOT_VERCEL: 'example.com',
FOO: 'bar',
VERCEL_URL: 'example.vercel.sh',
VERCEL_ENV: 'production',
VERCEL_REGION: 'iad1',
VERCEL_GIT_COMMIT_AUTHOR_LOGIN: 'rauchg',
},
},
want: {
GATSBY_VERCEL_URL: 'example.vercel.sh',
GATSBY_VERCEL_ENV: 'production',
GATSBY_VERCEL_REGION: 'iad1',
GATSBY_VERCEL_GIT_COMMIT_AUTHOR_LOGIN: 'rauchg',
TURBO_CI_VENDOR_ENV_KEY: 'GATSBY_VERCEL_',
},
},
{
name: 'should not return anything if no system env vars detected',
args: {
envPrefix: 'GATSBY_',
envs: {
USER_ENV_VAR_NOT_VERCEL: 'example.com',
FOO: 'bar',
BLARG_VERCEL_THING: 'fake',
},
},
want: {},
},
{
name: 'should not return anything if envPrefix is empty string',
args: {
envPrefix: '',
envs: {
VERCEL: '1',
VERCEL_URL: 'example.vercel.sh',
},
},
want: {},
},
{
name: 'should not return anything if envPrefix is undefined',
args: {
envPrefix: undefined,
envs: {
VERCEL: '1',
VERCEL_URL: 'example.vercel.sh',
},
},
want: {},
},
];
for (const { name, args, want } of cases) {
it(name, () => {
expect(getPrefixedEnvVars(args)).toEqual(want);
});
}
});