Skip to content

Commit

Permalink
wip: add utils and support for storybook v7 tests only
Browse files Browse the repository at this point in the history
  • Loading branch information
nilshah98 committed Oct 25, 2023
1 parent c252c87 commit c23158d
Show file tree
Hide file tree
Showing 8 changed files with 9,192 additions and 10,823 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"clean": "git clean -Xdf -e !node_modules -e !node_modules/**",
"lint": "eslint --ignore-path .gitignore .",
"readme": "percy-cli-readme",
"pretest": "node ./test/pretest.js",
"pretest": "storybook build --config-dir=./test/.storybook --output-dir=./test/.storybook-build --loglevel error",
"test": "yarn test:env jasmine --config=./test/jasmine.json",
"test:env": "cross-env NODE_ENV=test NODE_OPTIONS='--loader=./test/loader.js'",
"test:coverage": "nyc yarn test"
Expand All @@ -45,7 +45,7 @@
"@babel/eslint-parser": "^7.19.1",
"@babel/eslint-plugin": "^7.19.1",
"@babel/preset-env": "^7.22.9",
"@storybook/react": "^6.5.13",
"@storybook/react": "^7.5.0",
"@storybook/react-webpack5": "^7.5.0",
"babel-eslint": "^10.0.3",
"babel-plugin-istanbul": "^6.1.1",
Expand Down
7 changes: 4 additions & 3 deletions src/start.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import command from '@percy/cli-command';
import * as common from './common.js';
import { checkStorybookVersion } from './utils.js';

export const start = command('start', {
description: 'Run start-storybook to snapshot stories',
Expand Down Expand Up @@ -31,12 +32,12 @@ export const start = command('start', {
let { takeStorybookSnapshots } = yield import('./snapshots.js');
let { default: { spawn } } = yield import('cross-spawn');
let { host, port } = flags;
let storybookVersion = process.env.STORYBOOK_VERSION || '6';
let storybookVersion = await checkStorybookVersion();

let args = storybookVersion === '7' ? ['dev'] : [];
let args = storybookVersion === 7 ? ['dev'] : [];
args = args.concat(['--ci', `--host=${host}`, `--port=${port}`, ...argv]);

let storybookBinary = storybookVersion === '7' ? 'storybook' : 'start-storybook';
let storybookBinary = storybookVersion === 7 ? 'storybook' : 'start-storybook';

log.info(`Running "${storybookBinary} ${args.join(' ')}"`);

Expand Down
15 changes: 15 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
import { request, createRootResource, yieldTo } from '@percy/cli-command/utils';
import spawn from 'cross-spawn';

// check storybook version
export function checkStorybookVersion() {
return new Promise((resolve, reject) => {
spawn('storybook', ['--version'])
.on('exit', (code) => { if (code === 0) resolve(7); })
.on('error', (err) => {
if (err.code === 'ENOENT') {
resolve(6);
}
reject(err);
});
});
}

// Transforms authorization credentials into a basic auth header and returns all config request
// headers with the additional authorization header if not already set.
Expand Down
14 changes: 4 additions & 10 deletions test/.storybook/main.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
let storybookVersion = process.env.STORYBOOK_VERSION || '6';
let config = {
module.exports = {
stories: ['*.stories.js'],
features: {
postcss: false
}
};

if(storybookVersion === '7'){
config['framework'] = {
},
framework: {
name: '@storybook/react-webpack5',
options: {}
}
}

module.exports = config
};
6 changes: 6 additions & 0 deletions test/.storybook/mainV6.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
stories: ['*.stories.js'],
features: {
postcss: false
}
};
8 changes: 0 additions & 8 deletions test/pretest.js

This file was deleted.

7 changes: 4 additions & 3 deletions test/storybook.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import spawn from 'cross-spawn';
import { request } from '@percy/cli-command/utils';
import { api, logger, setupTest, createTestServer } from '@percy/cli-command/test/helpers';
import { storybook } from '../src/index.js';
import { checkStorybookVersion } from '../src/utils.js';

describe('percy storybook', () => {
let server, proc;
Expand All @@ -16,14 +17,14 @@ describe('percy storybook', () => {
default: () => [200, 'text/html', '<p>Not Storybook</p>']
});

let storybookVersion = process.env.STORYBOOK_VERSION || '6';
let args = storybookVersion === '7' ? ['dev'] : [];
let storybookVersion = await checkStorybookVersion();
let args = storybookVersion === 7 ? ['dev'] : [];
args = args.concat([
'--config-dir=./test/.storybook',
'--port=9000',
'--ci'
]);
let storybookBinary = storybookVersion === '7' ? 'storybook' : 'start-storybook';
let storybookBinary = storybookVersion === 7 ? 'storybook' : 'start-storybook';

proc = spawn(storybookBinary, args, { stdio: 'inherit' });

Expand Down
Loading

0 comments on commit c23158d

Please sign in to comment.