Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ghengeveld committed May 24, 2024
1 parent b598422 commit a1375ff
Showing 1 changed file with 39 additions and 24 deletions.
63 changes: 39 additions & 24 deletions node-src/tasks/build.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const command = vi.mocked(execaCommand);

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

afterEach(() => {
mockfs.restore();
Expand Down Expand Up @@ -148,7 +148,7 @@ describe('buildStorybook', () => {
await buildStorybook(ctx);
expect(command).toHaveBeenCalledWith(
ctx.buildCommand,
expect.objectContaining({ env: { NODE_ENV: 'production'} })
expect.objectContaining({ env: { CI: '1', NODE_ENV: 'production' } })
);
});

Expand All @@ -162,7 +162,7 @@ describe('buildStorybook', () => {
await buildStorybook(ctx);
expect(command).toHaveBeenCalledWith(
ctx.buildCommand,
expect.objectContaining({ env: { NODE_ENV: 'test'} })
expect.objectContaining({ env: { CI: '1', NODE_ENV: 'test' } })
);
});
});
Expand All @@ -173,26 +173,36 @@ describe('buildStorybook E2E', () => {
{ name: 'not found 1', error: 'Command not found: build-archive-storybook' },
{ name: 'not found 2', error: 'Command "build-archive-storybook" not found' },
{ name: 'npm not found', error: 'NPM error code E404\n\nMore error info' },
{ name: 'exit code not found', error: 'Command failed with exit code 127: some command\n\nsome error line\n\n' },
{ name: 'single line command failure', error: 'Command failed with exit code 1: npm exec build-archive-storybook --output-dir /tmp/chromatic--4210-0cyodqfYZabe' },
{
name: 'exit code not found',
error: 'Command failed with exit code 127: some command\n\nsome error line\n\n',
},
{
name: 'single line command failure',
error:
'Command failed with exit code 1: npm exec build-archive-storybook --output-dir /tmp/chromatic--4210-0cyodqfYZabe',
},
];

it.each(
missingDependencyErrorMessages
)('fails with missing dependency error when error message is $name', async ({ error }) => {
const ctx = {
buildCommand: 'npm exec build-archive-storybook',
options: { buildScriptName: '', playwright: true },
env: { STORYBOOK_BUILD_TIMEOUT: 0 },
log: { debug: vi.fn(), error: vi.fn() },
} as any;

command.mockRejectedValueOnce(new Error(error));
await expect(buildStorybook(ctx)).rejects.toThrow('Command failed');
expect(ctx.log.error).toHaveBeenCalledWith(expect.stringContaining('Failed to import `@chromatic-com/playwright`'));

ctx.log.error.mockClear();
});
it.each(missingDependencyErrorMessages)(
'fails with missing dependency error when error message is $name',
async ({ error }) => {
const ctx = {
buildCommand: 'npm exec build-archive-storybook',
options: { buildScriptName: '', playwright: true },
env: { STORYBOOK_BUILD_TIMEOUT: 0 },
log: { debug: vi.fn(), error: vi.fn() },
} as any;

command.mockRejectedValueOnce(new Error(error));
await expect(buildStorybook(ctx)).rejects.toThrow('Command failed');
expect(ctx.log.error).toHaveBeenCalledWith(
expect.stringContaining('Failed to import `@chromatic-com/playwright`')
);

ctx.log.error.mockClear();
}
);

it('fails with generic error message when not missing dependency error', async () => {
const ctx = {
Expand All @@ -202,11 +212,16 @@ describe('buildStorybook E2E', () => {
log: { debug: vi.fn(), error: vi.fn() },
} as any;

const errorMessage = 'Command failed with exit code 1: npm exec build-archive-storybook --output-dir /tmp/chromatic--4210-0cyodqfYZabe\n\nMore error message lines\n\nAnd more';
const errorMessage =
'Command failed with exit code 1: npm exec build-archive-storybook --output-dir /tmp/chromatic--4210-0cyodqfYZabe\n\nMore error message lines\n\nAnd more';
command.mockRejectedValueOnce(new Error(errorMessage));
await expect(buildStorybook(ctx)).rejects.toThrow('Command failed');
expect(ctx.log.error).not.toHaveBeenCalledWith(expect.stringContaining('Failed to import `@chromatic-com/playwright`'));
expect(ctx.log.error).toHaveBeenCalledWith(expect.stringContaining('Failed to run `chromatic --playwright`'));
expect(ctx.log.error).not.toHaveBeenCalledWith(
expect.stringContaining('Failed to import `@chromatic-com/playwright`')
);
expect(ctx.log.error).toHaveBeenCalledWith(
expect.stringContaining('Failed to run `chromatic --playwright`')
);
expect(ctx.log.error).toHaveBeenCalledWith(expect.stringContaining(errorMessage));
});
});

0 comments on commit a1375ff

Please sign in to comment.