Skip to content

Commit

Permalink
Extend tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ghengeveld committed Jun 3, 2024
1 parent e8938a1 commit 9483a03
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 37 deletions.
63 changes: 63 additions & 0 deletions bin-src/__mocks__/sb6ProjectJsonMissingBuilder/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{
"generatedAt": 1717341501873,
"hasCustomBabel": false,
"hasCustomWebpack": true,
"hasStaticDirs": true,
"hasStorybookEslint": false,
"refCount": 0,
"monorepo": "Turborepo",
"packageManager": {
"type": "yarn",
"version": "1.22.19"
},
"features": {
"postcss": false,
"interactionsDebugger": true,
"warnOnLegacyHierarchySeparator": false
},
"storybookVersion": "6.5.16",
"language": "javascript",
"storybookPackages": {
"@storybook/addon-actions": {
"version": "6.5.16"
},
"@storybook/addons": {
"version": "6.5.16"
},
"@storybook/builder-webpack4": {
"version": "6.5.16"
},
"@storybook/manager-webpack4": {
"version": "6.5.16"
},
"@storybook/react": {
"version": "6.5.16"
},
"@storybook/testing-library": {
"version": "0.0.13"
},
"@storybook/theming": {
"version": "6.5.16"
},
"msw-storybook-addon": {
"version": "1.7.0"
},
"storybook-mock-date-decorator": {
"version": "1.0.0"
}
},
"framework": {
"name": "react"
},
"addons": {
"@storybook/addon-links": {
"version": "6.5.16"
},
"@storybook/addon-essentials": {
"version": "6.5.16"
},
"@storybook/addon-interactions": {
"version": "6.5.16"
}
}
}
106 changes: 69 additions & 37 deletions node-src/lib/getPrebuiltStorybookMetadata.test.ts
Original file line number Diff line number Diff line change
@@ -1,61 +1,93 @@
import { getStorybookMetadataFromProjectJson } from './getPrebuiltStorybookMetadata';

import { readFile } from 'jsonfile';
import { afterEach, describe, expect, it, vi } from 'vitest';

afterEach(() => {
vi.restoreAllMocks();
});

vi.mock('jsonfile', async (importOriginal) => {
return {
// @ts-expect-error TS does not think actual is an object, but it's fine.
...(await importOriginal()),
readFile: vi.fn(() =>
Promise.resolve({
addons: {
'@storybook/addon-essentials': { version: '8.1.0' },
'@storybook/addon-links': { version: '8.1.0' },
},
builder: 'webpack5',
framework: { name: 'react' },
storybookVersion: '8.1.0',
storybookPackages: {
'@storybook/react': { version: '8.1.0' },
'@storybook/builder-webpack5': { version: '8.1.0' },
'@storybook/addon-essentials': { version: '8.1.0' },
'@storybook/addon-links': { version: '8.1.0' },
},
})
),
};
});
import { describe, expect, it } from 'vitest';

Check failure on line 3 in node-src/lib/getPrebuiltStorybookMetadata.test.ts

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

node-src/lib/getPrebuiltStorybookMetadata.test.ts#L3

Unable to resolve path to module 'vitest'.

describe('getStorybookMetadataFromProjectJson', () => {
it('should return the metadata from the project.json file', async () => {
const projectJsonPath = 'path/to/project.json';
const projectJsonPath = 'bin-src/__mocks__/normalProjectJson/project.json';
const metadata = await getStorybookMetadataFromProjectJson(projectJsonPath);

expect(metadata).toEqual({
viewLayer: '@storybook/react-webpack5',
version: '8.1.5',
builder: {
name: '@storybook/builder-webpack5',
packageVersion: '8.1.5',
},
addons: [
{
name: 'essentials',
packageName: '@storybook/addon-essentials',
packageVersion: '8.1.5',
},
{
name: 'compiler-swc',
packageName: '@storybook/addon-webpack5-compiler-swc',
packageVersion: '1.0.2',
},
],
});
});

it('should return the metadata from a Storybook 6 project.json file', async () => {
const projectJsonPath = 'bin-src/__mocks__/sb6ProjectJson/project.json';
const metadata = await getStorybookMetadataFromProjectJson(projectJsonPath);

expect(metadata).toEqual({
viewLayer: 'react',
version: '8.1.0',
version: '6.5.16',
builder: {
name: 'webpack5',
packageVersion: '8.1.0',
name: 'webpack4',
packageVersion: '6.5.16',
},
addons: [
{
name: 'links',
packageName: '@storybook/addon-links',
packageVersion: '6.5.16',
},
{
name: 'essentials',
packageName: '@storybook/addon-essentials',
packageVersion: '8.1.0',
packageVersion: '6.5.16',
},
{
name: 'interactions',
packageName: '@storybook/addon-interactions',
packageVersion: '6.5.16',
},
],
});
});

it('should return the metadata from the project.json file when the builder is missing', async () => {
const projectJsonPath = 'bin-src/__mocks__/sb6ProjectJsonMissingBuilder/project.json';
const metadata = await getStorybookMetadataFromProjectJson(projectJsonPath);

expect(metadata).toEqual({
viewLayer: 'react',
version: '6.5.16',
builder: {
name: 'webpack4',
packageVersion: '6.5.16',
},
addons: [
{
name: 'links',
packageName: '@storybook/addon-links',
packageVersion: '8.1.0',
packageVersion: '6.5.16',
},
{
name: 'essentials',
packageName: '@storybook/addon-essentials',
packageVersion: '6.5.16',
},
{
name: 'interactions',
packageName: '@storybook/addon-interactions',
packageVersion: '6.5.16',
},
],
});
expect(readFile).toHaveBeenCalledWith(projectJsonPath);
});
});

0 comments on commit 9483a03

Please sign in to comment.