|
| 1 | +import chalk from 'chalk'; |
| 2 | + |
1 | 3 | import { makeEnvPublic } from './make-env-public';
|
2 | 4 |
|
3 | 5 | const warnSpy = jest.spyOn(console, 'warn');
|
| 6 | +const infoSpy = jest.spyOn(console, 'info'); |
4 | 7 |
|
5 | 8 | beforeAll(() => {
|
6 | 9 | warnSpy.mockImplementation();
|
| 10 | + infoSpy.mockImplementation(); |
7 | 11 | });
|
8 | 12 |
|
9 | 13 | afterAll(() => {
|
10 | 14 | warnSpy.mockRestore();
|
| 15 | + infoSpy.mockRestore(); |
11 | 16 | });
|
12 | 17 |
|
13 | 18 | describe('makeEnvPublic()', () => {
|
@@ -38,19 +43,50 @@ describe('makeEnvPublic()', () => {
|
38 | 43 |
|
39 | 44 | expect(process.env.FOO).toEqual('foo');
|
40 | 45 | expect(process.env.NEXT_PUBLIC_FOO).toEqual('foo');
|
| 46 | + |
41 | 47 | expect(process.env.BAR).toEqual('bar');
|
42 | 48 | expect(process.env.NEXT_PUBLIC_BAR).toEqual('bar');
|
| 49 | + |
43 | 50 | expect(process.env.BAZ).toEqual('baz');
|
44 | 51 | expect(process.env.NEXT_PUBLIC_BAZ).toEqual('baz');
|
45 | 52 | });
|
46 | 53 |
|
| 54 | + it('should show an info message when env vars are prefixed', () => { |
| 55 | + process.env.FOO = 'foo'; |
| 56 | + process.env.BAR = 'bar'; |
| 57 | + process.env.BAZ = 'baz'; |
| 58 | + |
| 59 | + makeEnvPublic('FOO'); |
| 60 | + makeEnvPublic(['BAR', 'BAZ']); |
| 61 | + |
| 62 | + expect(infoSpy).toHaveBeenCalledWith( |
| 63 | + `${chalk.cyan( |
| 64 | + `info` |
| 65 | + )} - [next-runtime-env] - Prefixed environment variable 'FOO'.` |
| 66 | + ); |
| 67 | + |
| 68 | + expect(infoSpy).toHaveBeenCalledWith( |
| 69 | + `${chalk.cyan( |
| 70 | + `info` |
| 71 | + )} - [next-runtime-env] - Prefixed environment variable 'BAR'.` |
| 72 | + ); |
| 73 | + |
| 74 | + expect(infoSpy).toHaveBeenCalledWith( |
| 75 | + `${chalk.cyan( |
| 76 | + `info` |
| 77 | + )} - [next-runtime-env] - Prefixed environment variable 'BAZ'.` |
| 78 | + ); |
| 79 | + }); |
| 80 | + |
47 | 81 | it('should warn when the env var already starts with NEXT_PUBLIC_', () => {
|
48 | 82 | process.env.NEXT_PUBLIC_FOO = 'foo';
|
49 | 83 |
|
50 | 84 | makeEnvPublic('NEXT_PUBLIC_FOO');
|
51 | 85 |
|
52 | 86 | expect(warnSpy).toHaveBeenCalledWith(
|
53 |
| - '> [next-runtime-env] The environment variable "NEXT_PUBLIC_FOO" is already public.' |
| 87 | + `${chalk.yellow( |
| 88 | + `warn` |
| 89 | + )} - [next-runtime-env] - Environment variable 'NEXT_PUBLIC_FOO' is already public.` |
54 | 90 | );
|
55 | 91 | });
|
56 | 92 | });
|
0 commit comments