CLI: Tests and bugfix for cra5 fix

This commit is contained in:
Michael Shilman 2021-09-30 16:49:17 +08:00
parent 05cd8c3a30
commit bfb16b7795
3 changed files with 153 additions and 2 deletions

View File

@ -0,0 +1,138 @@
/* eslint-disable no-underscore-dangle */
import { JsPackageManager } from '../../js-package-manager';
import { cra5 } from './cra5';
// eslint-disable-next-line global-require, jest/no-mocks-import
jest.mock('fs-extra', () => require('../../../../../__mocks__/fs-extra'));
const checkCra5 = async ({ packageJson, main }) => {
// eslint-disable-next-line global-require
require('fs-extra').__setMockFiles({
'.storybook/main.js': `module.exports = ${JSON.stringify(main)};`,
});
const packageManager = {
retrievePackageJson: () => ({ dependencies: {}, devDependencies: {}, ...packageJson }),
} as JsPackageManager;
return cra5.check({ packageManager });
};
describe('cra5 fix', () => {
describe('sb < 6.3', () => {
describe('cra5 dependency', () => {
const packageJson = {
dependencies: { '@storybook/react': '^6.2.0', 'react-scripts': '^5.0.0' },
};
it('should fail', async () => {
await expect(
checkCra5({
packageJson,
main: {},
})
).rejects.toThrow();
});
});
describe('no cra5 dependency', () => {
const packageJson = { dependencies: { '@storybook/react': '^6.2.0' } };
it('should no-op', async () => {
await expect(
checkCra5({
packageJson,
main: {},
})
).resolves.toBeFalsy();
});
});
});
describe('sb 6.3 - 7.0', () => {
describe('cra5 dependency', () => {
const packageJson = {
dependencies: { '@storybook/react': '^6.3.0', 'react-scripts': '^5.0.0' },
};
describe('webpack5 builder', () => {
it('should no-op', async () => {
await expect(
checkCra5({
packageJson,
main: { core: { builder: 'webpack5' } },
})
).resolves.toBeFalsy();
});
});
describe('custom builder', () => {
it('should no-op', async () => {
await expect(
checkCra5({
packageJson,
main: { core: { builder: 'storybook-builder-vite' } },
})
).resolves.toBeFalsy();
});
});
describe('webpack4 builder', () => {
it('should add webpack5 builder', async () => {
await expect(
checkCra5({
packageJson,
main: { core: { builder: 'webpack4' } },
})
).resolves.toMatchObject({
craVersion: '^5.0.0',
storybookVersion: '^6.3.0',
});
});
});
describe('no builder', () => {
it('should add webpack5 builder', async () => {
await expect(
checkCra5({
packageJson,
main: {},
})
).resolves.toMatchObject({
craVersion: '^5.0.0',
storybookVersion: '^6.3.0',
});
});
});
});
describe('no cra dependency', () => {
it('should no-op', async () => {
await expect(
checkCra5({
packageJson: {},
main: {},
})
).resolves.toBeFalsy();
});
});
describe('cra4 dependency', () => {
it('should no-op', async () => {
await expect(
checkCra5({
packageJson: {
dependencies: {
'react-scripts': '4',
},
},
main: {},
})
).resolves.toBeFalsy();
});
});
});
describe('sb 7.0+', () => {
describe('cra5 dependency', () => {
const packageJson = {
dependencies: { '@storybook/react': '^7.0.0-alpha.0', 'react-scripts': '^5.0.0' },
};
it('should no-op', async () => {
await expect(
checkCra5({
packageJson,
main: {},
})
).resolves.toBeFalsy();
});
});
});
});

View File

@ -27,7 +27,7 @@ export const cra5: Fix<CRA5RunOptions> = {
const craVersion = dependencies['react-scripts'] || devDependencies['react-scripts'];
const craCoerced = semver.coerce(craVersion)?.version;
if (semver.lt(craCoerced, '5.0.0')) {
if (!craCoerced || semver.lt(craCoerced, '5.0.0')) {
return null;
}

View File

@ -64,7 +64,20 @@ describe('webpack5 fix', () => {
).resolves.toBeFalsy();
});
});
describe('no webpack5 builder', () => {
describe('webpack4 builder', () => {
it('should add webpack5 builder', async () => {
await expect(
checkWebpack5({
packageJson,
main: { core: { builder: 'webpack4' } },
})
).resolves.toMatchObject({
webpackVersion: '^5.0.0',
storybookVersion: '^6.3.0',
});
});
});
describe('no builder', () => {
it('should add webpack5 builder', async () => {
await expect(
checkWebpack5({