diff --git a/bin-src/init.test.ts b/bin-src/init.test.ts index 6fdfa171b..a46f8333c 100644 --- a/bin-src/init.test.ts +++ b/bin-src/init.test.ts @@ -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"; @@ -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(() => { @@ -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() diff --git a/bin-src/init.ts b/bin-src/init.ts index 95691862b..876d5ed45 100644 --- a/bin-src/init.ts +++ b/bin-src/init.ts @@ -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'; @@ -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 = { @@ -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)