Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
thafryer committed May 2, 2023
1 parent 55a5e90 commit cb54479
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 28 deletions.
70 changes: 43 additions & 27 deletions bin-src/lib/getStorybookInfo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ describe('getStorybookInfo', () => {

it('returns viewLayer and version', async () => {
const ctx = getContext({ packageJson: { dependencies: REACT } });
await expect(getStorybookInfo(ctx)).resolves.toEqual(
const sbInfo = await getStorybookInfo(ctx);
expect(sbInfo).toEqual(
// We're getting the result of tracing chromatic-cli's node_modules here.
expect.objectContaining({
viewLayer: 'react',
Expand Down Expand Up @@ -55,14 +56,19 @@ describe('getStorybookInfo', () => {
);
});

it('throws on missing package', async () => {
it('returns other metadate if missing view layer package', async () => {
const ctx = getContext({ packageJson: { dependencies: VUE } });
await expect(getStorybookInfo(ctx)).resolves.toEqual({
addons: [],
version: null,
viewLayer: null,
builder: null,
});
await expect(getStorybookInfo(ctx)).resolves.toEqual(
expect.objectContaining({
addons: [
{
name: 'viewport',
packageName: '@storybook/addon-viewport',
},
],
builder: { name: 'webpack5', packageVersion: '6.5.6' },
})
);
});

it('looks up package in node_modules on missing dependency', async () => {
Expand All @@ -84,7 +90,7 @@ describe('getStorybookInfo', () => {
const ctx = getContext({
env: { CHROMATIC_STORYBOOK_VERSION: '@storybook/react@3.2.1' },
});
await expect(getStorybookInfo(ctx)).resolves.toEqual(
expect(await getStorybookInfo(ctx)).toEqual(
expect.objectContaining({
viewLayer: 'react',
version: '3.2.1',
Expand All @@ -95,7 +101,7 @@ describe('getStorybookInfo', () => {

it('supports unscoped package name', async () => {
const ctx = getContext({ env: { CHROMATIC_STORYBOOK_VERSION: 'react@3.2.1' } });
await expect(getStorybookInfo(ctx)).resolves.toEqual(
expect(await getStorybookInfo(ctx)).toEqual(
expect.objectContaining({
viewLayer: 'react',
version: '3.2.1',
Expand All @@ -104,24 +110,34 @@ describe('getStorybookInfo', () => {
);
});

it('throws on invalid value', async () => {
it('still returns addons and builder for invalid version value', async () => {
const ctx = getContext({ env: { CHROMATIC_STORYBOOK_VERSION: '3.2.1' } });
await expect(getStorybookInfo(ctx)).resolves.toEqual({
addons: [],
version: null,
viewLayer: null,
builder: null,
});
expect(await getStorybookInfo(ctx)).toEqual(
expect.objectContaining({
addons: [
{
name: 'viewport',
packageName: '@storybook/addon-viewport',
},
],
builder: { name: 'webpack5', packageVersion: '6.5.6' },
})
);
});

it('throws on unsupported viewlayer', async () => {
it('does not include unsupported view layers', async () => {
const ctx = getContext({ env: { CHROMATIC_STORYBOOK_VERSION: '@storybook/native@3.2.1' } });
await expect(getStorybookInfo(ctx)).resolves.toEqual({
addons: [],
version: null,
viewLayer: null,
builder: null,
});
expect(await getStorybookInfo(ctx)).toEqual(
expect.objectContaining({
addons: [
{
name: 'viewport',
packageName: '@storybook/addon-viewport',
},
],
builder: { name: 'webpack5', packageVersion: '6.5.6' },
})
);
});
});

Expand All @@ -131,7 +147,7 @@ describe('getStorybookInfo', () => {
options: { storybookBuildDir: 'bin-src/__mocks__/normalProjectJson' },
packageJson: { dependencies: REACT },
});
await expect(getStorybookInfo(ctx)).resolves.toEqual({
expect(await getStorybookInfo(ctx)).toEqual({
addons: [
{
name: 'viewport',
Expand All @@ -150,7 +166,7 @@ describe('getStorybookInfo', () => {
options: { storybookBuildDir: 'bin-src/__mocks__/malformedProjectJson' },
packageJson: { dependencies: REACT },
});
await expect(getStorybookInfo(ctx)).resolves.toEqual({
expect(await getStorybookInfo(ctx)).toEqual({
addons: [],
version: null,
viewLayer: null,
Expand All @@ -163,7 +179,7 @@ describe('getStorybookInfo', () => {
options: { storybookBuildDir: 'bin-src/__mocks__/unsupportedAddons' },
packageJson: { dependencies: REACT },
});
await expect(getStorybookInfo(ctx)).resolves.toEqual({
expect(await getStorybookInfo(ctx)).toEqual({
addons: [
{
name: 'viewport',
Expand Down
11 changes: 10 additions & 1 deletion bin-src/lib/getStorybookMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,5 +188,14 @@ export const getStorybookMetadata = async (ctx: Context) => {
findBuilder(mainConfig),
]);
ctx.log.debug(info);
return info.reduce((acc, obj) => Object.assign(acc, obj), {});
let metadata = {};
info.forEach((sbItem) => {
if (sbItem.status === 'fulfilled') {
metadata = {
...metadata,
...(sbItem?.value as any),
};
}
});
return metadata;
};

0 comments on commit cb54479

Please sign in to comment.