Skip to content

Commit

Permalink
Fix tests to detect proper package manager
Browse files Browse the repository at this point in the history
  • Loading branch information
ethriel3695 committed May 29, 2024
1 parent 4c019f7 commit d11e44a
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 48 deletions.
111 changes: 74 additions & 37 deletions node-src/tasks/build.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
import { execaCommand } from 'execa';
import mockfs from 'mock-fs';
import { afterEach, describe, expect, it, vi } from 'vitest';
import { getCliCommand as getCliCommandDefault } from '@antfu/ni';
import { describe, expect, it, vi } from 'vitest';

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

vi.mock('execa');
vi.mock('@antfu/ni');

const command = vi.mocked(execaCommand);
const getCliCommand = vi.mocked(getCliCommandDefault);

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

afterEach(() => {
mockfs.restore();
});

describe('setSourceDir', () => {
Expand Down Expand Up @@ -45,7 +43,7 @@ describe('setSourceDir', () => {

describe('setBuildCommand', () => {
it('sets the build command on the context', async () => {
mockfs({ './package.json': JSON.stringify({ engines: {'npm': ">10"} }) });
getCliCommand.mockReturnValue(Promise.resolve('npm run build:storybook'));

const ctx = {
sourceDir: './source-dir/',
Expand All @@ -55,16 +53,16 @@ describe('setBuildCommand', () => {
} as any;
await setBuildCommand(ctx);

expect(ctx.buildCommand).toEqual(
'npm run build:storybook -- --output-dir ./source-dir/ --webpack-stats-json ./source-dir/'
expect(getCliCommand).toHaveBeenCalledWith(
expect.anything(),
['build:storybook', '--output-dir', './source-dir/', '--webpack-stats-json', './source-dir/'],
{ programmatic: true }
);
expect(ctx.buildCommand).toEqual('npm run build:storybook');
});

it('supports yarn', async () => {
mockfs({
'./package.json': JSON.stringify({ packageManager: 'yarn' }),
'./yarn.lock': '',
});
getCliCommand.mockReturnValue(Promise.resolve('yarn run build:storybook'));

const ctx = {
sourceDir: './source-dir/',
Expand All @@ -74,7 +72,31 @@ describe('setBuildCommand', () => {
} as any;
await setBuildCommand(ctx);

expect(ctx.buildCommand).toEqual('yarn run build:storybook --output-dir ./source-dir/');
expect(getCliCommand).toHaveBeenCalledWith(
expect.anything(),
['build:storybook', '--output-dir', './source-dir/', '--webpack-stats-json', './source-dir/'],
{ programmatic: true }
);
expect(ctx.buildCommand).toEqual('yarn run build:storybook');
});

it('supports pnpm', async () => {
getCliCommand.mockReturnValue(Promise.resolve('pnpm run build:storybook'));

const ctx = {
sourceDir: './source-dir/',
options: { buildScriptName: 'build:storybook' },
storybook: { version: '6.1.0' },
git: {},
} as any;
await setBuildCommand(ctx);

expect(getCliCommand).toHaveBeenCalledWith(
expect.anything(),
['build:storybook', '--output-dir', './source-dir/', '--webpack-stats-json', './source-dir/'],
{ programmatic: true }
);
expect(ctx.buildCommand).toEqual('pnpm run build:storybook');
});

it('warns if --only-changes is not supported', async () => {
Expand Down Expand Up @@ -131,7 +153,7 @@ describe('buildStorybook', () => {
await buildStorybook(ctx);
expect(command).toHaveBeenCalledWith(
ctx.buildCommand,
expect.objectContaining({ env: { NODE_ENV: 'production'} })
expect.objectContaining({ env: { NODE_ENV: 'production' } })
);
});

Expand All @@ -145,7 +167,7 @@ describe('buildStorybook', () => {
await buildStorybook(ctx);
expect(command).toHaveBeenCalledWith(
ctx.buildCommand,
expect.objectContaining({ env: { NODE_ENV: 'test'} })
expect.objectContaining({ env: { NODE_ENV: 'test' } })
);
});
});
Expand All @@ -156,26 +178,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 @@ -185,11 +217,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));
});
});
36 changes: 25 additions & 11 deletions node-src/tasks/initialize.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import { execa as execaDefault, execaCommand } from 'execa';
import mockfs from 'mock-fs';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { getCliCommand as getCliCommandDefault } from '@antfu/ni';
import { beforeEach, describe, expect, it, vi } from 'vitest';

import { announceBuild, setEnvironment, setRuntimeMetadata } from './initialize';

vi.mock('execa');
vi.mock('@antfu/ni');

const execa = vi.mocked(execaDefault);
const command = vi.mocked(execaCommand);

afterEach(() => {
mockfs.restore();
});
const getCliCommand = vi.mocked(getCliCommandDefault);

process.env.GERRIT_BRANCH = 'foo/bar';
process.env.TRAVIS_EVENT_TYPE = 'pull_request';
Expand All @@ -37,7 +35,7 @@ describe('setRuntimeMetadata', () => {
});

it('sets the build command on the context', async () => {
mockfs({ './package.json': JSON.stringify({ engines: {'npm': ">10"} }) });
getCliCommand.mockReturnValue(Promise.resolve('npm'));

const ctx = {
sourceDir: './source-dir/',
Expand All @@ -56,10 +54,7 @@ describe('setRuntimeMetadata', () => {
});

it('supports yarn', async () => {
mockfs({
'./package.json': JSON.stringify({ packageManager: 'yarn' }),
'./yarn.lock': '',
});
getCliCommand.mockReturnValue(Promise.resolve('yarn'));

const ctx = {
sourceDir: './source-dir/',
Expand All @@ -76,6 +71,25 @@ describe('setRuntimeMetadata', () => {
packageManagerVersion: '1.2.3',
});
});

it('supports pnpm', async () => {
getCliCommand.mockReturnValue(Promise.resolve('pnpm'));

const ctx = {
sourceDir: './source-dir/',
options: { buildScriptName: 'build:storybook' },
storybook: { version: '6.1.0' },
git: {},
} as any;
await setRuntimeMetadata(ctx);

expect(ctx.runtimeMetadata).toEqual({
nodePlatform: expect.stringMatching(/darwin|linux|win32/),
nodeVersion: process.versions.node,
packageManager: 'pnpm',
packageManagerVersion: '1.2.3',
});
});
});

describe('announceBuild', () => {
Expand Down

0 comments on commit d11e44a

Please sign in to comment.