Skip to content

Commit

Permalink
Add support for Storybook 5 in init command (#225)
Browse files Browse the repository at this point in the history
  • Loading branch information
oblador authored Mar 25, 2020
1 parent 7128c7b commit 6ad6dd9
Show file tree
Hide file tree
Showing 11 changed files with 118 additions and 13 deletions.
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ jobs:
- stage: test
name: 'Unit tests'
script: yarn test
- stage: test
name: 'Integration tests: CLI'
services:
- docker
before_script: cd test/cli
script: yarn test
- stage: test
name: 'Visual tests: React DOM'
services:
Expand Down
16 changes: 16 additions & 0 deletions docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,22 @@ yarn add loki --dev
yarn loki init
```

### Manually

Optionally add [loki configuration](/configuration.html) and integrate as follows:

#### React

Add `import 'loki/configure-react'` to `.storybook/preview.js`, create it if it doesn't exist already.

#### Vue

Add `import 'loki/configure-vue'` to `.storybook/preview.js`, create it if it doesn't exist already.

#### React native

Add `import 'loki/configure-react-native'` to `storybook/storybook.js`.

## Running your first tests

### 1. Review configuration
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"loki": "bin/loki"
},
"scripts": {
"format": "prettier '{,packages/**/,examples/**/,website/**/,docs/**/}*.{md,js,json}' --write",
"format": "prettier '{,packages/**/,examples/**/,website/**/,docs/**/,test/**/}*.{md,js,json}' --write",
"lint": "eslint packages/*/src",
"test": "jest"
},
Expand All @@ -31,6 +31,7 @@
"workspaces": {
"packages": [
"packages/*",
"test/*",
"website",
"examples/react",
"examples/renderer-aws-lambda"
Expand Down
2 changes: 1 addition & 1 deletion packages/browser/src/get-stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const getStories = window => {
const getStorybook = window.loki && window.loki.getStorybook;
if (!getStorybook) {
throw new Error(
"Loki addon not registered. Add `import 'loki/configure-react'` to your config.js file."
"Loki addon not registered. Add `import 'loki/configure-react'` to your .storybook/preview.js file."
);
}
return getStorybook().map(component => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ describe('createChromeAWSLambdaRenderer', () => {
'throws if not configured',
async () => {
await expect(fetchStorybookFixture('unconfigured')).rejects.toThrow(
"Loki addon not registered. Add `import 'loki/configure-react'` to your config.js file."
"Loki addon not registered. Add `import 'loki/configure-react'` to your .storybook/preview.js file."
);
},
DOCKER_TEST_TIMEOUT
Expand Down
29 changes: 20 additions & 9 deletions packages/runner/src/commands/init/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const insertAfter = (
)}\n${stringToInsert}${content.substr(insertionIndex)}`;
}
if (fallback === 'append') {
return `${content}\n${stringToInsert}\n`;
return `${content ? `${content}\n` : ''}${stringToInsert}\n`;
}
return `${stringToInsert}\n${content}`;
};
Expand Down Expand Up @@ -88,24 +88,35 @@ function init(args) {
fs.outputFileSync(storybookjsPath, modifiedStorybookjs);
}
} else {
const configjsPath = `${storybookPath}/config.js`;
const configjs = fs.readFileSync(configjsPath, 'utf8');
const projectType = isVueProject ? 'vue' : 'react';
const configPackage = `loki/configure-${projectType}`;
const storybookPackage = `@storybook/${projectType}`;
if (configjs.indexOf(configPackage) !== -1) {

const configjsPath = `${storybookPath}/config.js`;
const previewjsPath = `${storybookPath}/preview.js`;
const destinationPath = fs.existsSync(configjsPath)
? configjsPath
: previewjsPath;

const configContent = fs.existsSync(destinationPath)
? fs.readFileSync(destinationPath, 'utf8')
: '';

if (configContent.indexOf(configPackage) !== -1) {
warn(
`${relative(configjsPath)} already has loki configuration, skipping...`
`${relative(
destinationPath
)} already has loki configuration, skipping...`
);
} else {
info(`Adding loki configuration to ${relative(configjsPath)}`);
const modifiedConfigjs = insertAfter(
configjs,
info(`Adding loki configuration to ${relative(destinationPath)}`);
const modifiedConfigContent = insertAfter(
configContent,
new RegExp(`(require\\(|from )['"]${storybookPackage}['"]\\)?;?[ \t]*`),
`import '${configPackage}';`,
'append'
);
fs.outputFileSync(configjsPath, modifiedConfigjs);
fs.outputFileSync(destinationPath, modifiedConfigContent);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe('createChromeTarget', () => {
'throws if not configured',
async () => {
await expect(fetchStorybookFixture('unconfigured')).rejects.toThrow(
"Loki addon not registered. Add `import 'loki/configure-react'` to your config.js file."
"Loki addon not registered. Add `import 'loki/configure-react'` to your .storybook/preview.js file."
);
},
DOCKER_TEST_TIMEOUT
Expand Down
1 change: 1 addition & 0 deletions test/cli/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
generated
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions test/cli/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "@loki/test-cli",
"version": "0.20.1",
"private": true,
"scripts": {
"test": "bash ./test-cra-init.sh"
},
"devDependencies": {
"execa": "^1.0.0",
"loki": "^0.20.1"
}
}
58 changes: 58 additions & 0 deletions test/cli/test-cra-init.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/bin/bash

set -Eeuo pipefail

assert_contains() {
FILENAME=$1
EXPECTED_CONTENT=$2

if [[ "$(cat $FILENAME)" == *"${EXPECTED_CONTENT}"* ]]; then
return 0
else
echo "Unable to find $EXPECTED_CONTENT in $FILENAME"
return 1
fi
}

# Ensure we're in the correct folder
pushd "$( dirname "${BASH_SOURCE[0]}" )" > /dev/null

# Scaffold a new CRA project with storybook and loki
mkdir -p generated
cd generated
rm -rf create-react-app || true
yarn create react-app create-react-app
cd create-react-app
npx -p @storybook/cli sb init
yarn add loki
../../../../node_modules/.bin/loki init

# Ensure modifications has been made
assert_contains "package.json" "$(cat <<-END
"loki": {
"configurations": {
"chrome.laptop": {
"target": "chrome.docker",
"width": 1366,
"height": 768,
"deviceScaleFactor": 1,
"mobile": false
},
"chrome.iphone7": {
"target": "chrome.docker",
"preset": "iPhone 7"
}
}
}
END
)"
assert_contains ".storybook/preview.js" "import 'loki/configure-react'"

# Ensure we can snap stories
yarn build-storybook
mkdir -p .loki/reference
cp ../../fixtures/chrome_laptop_Welcome_to_Storybook.png ./.loki/reference
../../../../node_modules/.bin/loki test laptop --requireReference --storiesFilter Welcome --reactUri file:./storybook-static

# Peace out
popd > /dev/null

0 comments on commit 6ad6dd9

Please sign in to comment.