Skip to content

Commit

Permalink
update criteria for package install
Browse files Browse the repository at this point in the history
  • Loading branch information
thafryer committed Jan 19, 2024
1 parent f9f38e2 commit 234f50a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 25 deletions.
26 changes: 11 additions & 15 deletions bin-src/init.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { execaCommand } from "execa"
import {findUp} from "find-up"
import { writeFile } from "jsonfile";
import { afterEach, describe, expect, it, vi } from "vitest";

Expand All @@ -15,13 +14,6 @@ vi.mock('jsonfile', async (importOriginal) => {
});

vi.mock('execa');
vi.mock('find-up', async (importOriginal) => {
return {
// @ts-expect-error TS does not think actual is an object, but it's fine.
...await importOriginal(),
findUp: vi.fn(() => Promise.resolve(undefined)),
};
});

describe('addChromaticScriptToPackageJson', () => {
afterEach(() => {
Expand Down Expand Up @@ -76,25 +68,29 @@ describe('installArchiveDependencies', () => {
vi.clearAllMocks()
vi.resetModules()
})
it('successfully installs complete list of dependencies for Playwright if SB package is not found and SB config is not found', async () => {
it('successfully installs complete list of dependencies for Playwright if SB package is not found and Essentials is not found', async () => {
await installArchiveDependencies({}, 'playwright')
expect(execaCommand).toHaveBeenCalledOnce()
expect(execaCommand).toHaveBeenCalledWith('yarn add -D chromatic chromatic-playwright storybook@next @storybook/addon-essentials@next @storybook/server-webpack5@next')
})
it('successfully installs complete list of dependencies for Cypress if SB package is not found and SB config is not found', async () => {
it('successfully installs complete list of dependencies for Cypress if SB package is not found and Essentials is not found', async () => {
await installArchiveDependencies({}, 'cypress')
expect(execaCommand).toHaveBeenCalledOnce()
expect(execaCommand).toHaveBeenCalledWith('yarn add -D chromatic chromatic-cypress storybook@next @storybook/addon-essentials@next @storybook/server-webpack5@next')
})
it('successfully installs complete list of dependencies if SB package is found and SB config is not found', async () => {
it('successfully installs complete list of dependencies if SB package is found and Essentials is not found', async () => {
await installArchiveDependencies({devDependencies: {'storybook': 'latest'}}, 'playwright')
expect(execaCommand).toHaveBeenCalledOnce()
expect(execaCommand).toHaveBeenCalledWith('yarn add -D chromatic chromatic-playwright storybook@next @storybook/addon-essentials@next @storybook/server-webpack5@next')
})
it('successfully installs complete list of dependencies if SB package is found and SB config is found', async () => {
vi.mocked(findUp).mockResolvedValue('./project/main.ts')
await installArchiveDependencies({devDependencies: {storybook: 'latest'}}, 'playwright')
expect(findUp).toHaveBeenCalledOnce()
it('successfully installs smaller list of dependencies if SB package is found and Essentials is found in devDependencies', async () => {
await installArchiveDependencies({devDependencies: {storybook: 'latest', '@storybook/addon-essentials': 'latest'}}, 'playwright')
expect(execaCommand).toHaveBeenCalledOnce()
expect(execaCommand).toHaveBeenCalledWith('yarn add -D chromatic chromatic-playwright @storybook/server-webpack5@latest')
vi.clearAllMocks()
})
it('successfully installs smaller list of dependencies if SB package is found and Essentials is found in dependencies', async () => {
await installArchiveDependencies({dependencies: {storybook: 'latest', '@storybook/addon-essentials': 'latest'}}, 'playwright')
expect(execaCommand).toHaveBeenCalledOnce()
expect(execaCommand).toHaveBeenCalledWith('yarn add -D chromatic chromatic-playwright @storybook/server-webpack5@latest')
vi.clearAllMocks()
Expand Down
14 changes: 4 additions & 10 deletions bin-src/init.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import boxen from "boxen";
import { execaCommand } from 'execa';
import { findUp } from 'find-up';
import { writeFile } from 'jsonfile';
import meow from 'meow';
import prompts from 'prompts';
Expand All @@ -21,11 +20,6 @@ export const getPackageManagerInstallCommand = async (args: string[]) => {
return await getCliCommand(parseNi, args, { programmatic: true });
};

const getSBConfigFilePath = async () => {
// Walks up directory tree to find nearest Storybook config file.
return await findUp(['.storybook/main.ts', '.storybook/main.js', '.storybook/main.tsx', '.storybook/main.jsx', '.storybook/main.mjs', '.storybook/main.cjs'])
}

export const addChromaticScriptToPackageJson = async ({ packageJson, packagePath }) => {
try {
const json = {
Expand All @@ -52,10 +46,10 @@ export const createChromaticConfigFile = async ({configFile, buildScriptName = n

export const installArchiveDependencies = async (packageJson: PackageJson, testFramework: TestFrameworkType) => {
let installArgs = ['-D', 'chromatic',`chromatic-${testFramework}`, 'storybook@next', '@storybook/addon-essentials@next', '@storybook/server-webpack5@next']
const sbConfigPath = await getSBConfigFilePath()
const sbVersion = packageJson?.devDependencies?.storybook || packageJson?.dependencies?.storybook
if(sbConfigPath && sbVersion) {
installArgs = ['-D', 'chromatic',`chromatic-${testFramework}`, `@storybook/server-webpack5@${sbVersion}`]
const storybookVersion = packageJson?.devDependencies?.storybook || packageJson?.dependencies?.storybook
const essentialsVersion = packageJson?.devDependencies?.['@storybook/addon-essentials'] || packageJson?.dependencies?.['@storybook/addon-essentials']
if(storybookVersion && essentialsVersion) {
installArgs = ['-D', 'chromatic',`chromatic-${testFramework}`, `@storybook/server-webpack5@${storybookVersion}`]
}
const installCommand = await getPackageManagerInstallCommand(installArgs)
await execaCommand(installCommand)
Expand Down

0 comments on commit 234f50a

Please sign in to comment.