Skip to content

Commit

Permalink
Allow overriding NODE_ENV with STORYBOOK_NODE_ENV
Browse files Browse the repository at this point in the history
  • Loading branch information
tmeasday committed Dec 20, 2023
1 parent fa2d355 commit aeb31b4
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
3 changes: 3 additions & 0 deletions node-src/lib/getEnv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface Env {
STORYBOOK_BUILD_TIMEOUT: number;
STORYBOOK_CLI_FLAGS_BY_VERSION: typeof STORYBOOK_CLI_FLAGS_BY_VERSION;
STORYBOOK_VERIFY_TIMEOUT: number;
STORYBOOK_NODE_ENV: string;
}

const {
Expand All @@ -33,6 +34,7 @@ const {
LOGGLY_CUSTOMER_TOKEN = 'b5e26204-cdc5-4c78-a9cc-c69eb7fabad3',
STORYBOOK_BUILD_TIMEOUT = String(10 * 60 * 1000),
STORYBOOK_VERIFY_TIMEOUT = String(3 * 60 * 1000),
STORYBOOK_NODE_ENV = 'production',
} = process.env;

const ENVIRONMENT_WHITELIST = [/^GERRIT/, /^TRAVIS/];
Expand Down Expand Up @@ -70,4 +72,5 @@ export default () =>
STORYBOOK_BUILD_TIMEOUT: parseInt(STORYBOOK_BUILD_TIMEOUT, 10),
STORYBOOK_CLI_FLAGS_BY_VERSION,
STORYBOOK_VERIFY_TIMEOUT: parseInt(STORYBOOK_VERIFY_TIMEOUT, 10),
STORYBOOK_NODE_ENV,
} as Env);
33 changes: 33 additions & 0 deletions node-src/tasks/build.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@ import mockfs from 'mock-fs';
import { afterEach, describe, expect, it, vi } from 'vitest';

import { buildStorybook, setSourceDir, setBuildCommand } from './build';
import { beforeEach } from 'node:test';

vi.mock('execa');

const command = vi.mocked(execaCommand);

beforeEach(() => {
command.mockClear();
})

afterEach(() => {
mockfs.restore();
});
Expand Down Expand Up @@ -132,4 +137,32 @@ describe('buildStorybook', () => {
await expect(buildStorybook(ctx)).rejects.toThrow('Command failed');
expect(ctx.log.error).toHaveBeenCalledWith(expect.stringContaining('Operation timed out'));
});

it('passes NODE_ENV=production', async () => {
const ctx = {
buildCommand: 'npm run build:storybook --script-args',
env: { STORYBOOK_BUILD_TIMEOUT: 1000 },
log: { debug: vi.fn() },
options: { storybookLogFile: 'build-storybook.log' },
} as any;
await buildStorybook(ctx);
expect(command).toHaveBeenCalledWith(
ctx.buildCommand,
expect.objectContaining({ env: { NODE_ENV: 'production'} })
);
});

it('allows overriding NODE_ENV with STORYBOOK_NODE_ENV', async () => {
const ctx = {
buildCommand: 'npm run build:storybook --script-args',
env: { STORYBOOK_BUILD_TIMEOUT: 1000, STORYBOOK_NODE_ENV: 'test' },
log: { debug: vi.fn() },
options: { storybookLogFile: 'build-storybook.log' },
} as any;
await buildStorybook(ctx);
expect(command).toHaveBeenCalledWith(
ctx.buildCommand,
expect.objectContaining({ env: { NODE_ENV: 'test'} })
);
});
});
2 changes: 1 addition & 1 deletion node-src/tasks/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export const buildStorybook = async (ctx: Context) => {
const subprocess = execaCommand(ctx.buildCommand, {
stdio: [null, logFile, logFile],
signal,
env: { NODE_ENV: 'production' },
env: { NODE_ENV: ctx.env.STORYBOOK_NODE_ENV || 'production' },
});
await Promise.race([subprocess, timeoutAfter(ctx.env.STORYBOOK_BUILD_TIMEOUT)]);
} catch (e) {
Expand Down

0 comments on commit aeb31b4

Please sign in to comment.