Skip to content

Commit

Permalink
Add jest-storybook to lib
Browse files Browse the repository at this point in the history
  • Loading branch information
abdallahalsamman committed Dec 16, 2018
1 parent 46c430f commit 917d80a
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/jest-storybook/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# jest-storybook

Jest implementation for Storybook.
28 changes: 28 additions & 0 deletions lib/jest-storybook/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "jest-storybook",
"version": "1.0.0",
"description": "Jest implementation for Storybook",
"main": "src/jest.storybook.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "https://github.com/storybooks/storybook"
},
"keywords": [
"jest",
"storybook"
],
"author": "evexoio (Abdullah Samman)",
"license": "ISC",
"bugs": {
"url": "https://github.com/storybooks/storybook/issues"
},
"homepage": "https://github.com/storybooks/storybook",
"devDependencies": {
"babel-core": "^6.26.3",
"babel-preset-jest": "^23.2.0",
"falafel": "^2.1.0"
}
}
6 changes: 6 additions & 0 deletions lib/jest-storybook/src/jest.config.js.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
testMatch: ['**/*.stories.js'],
transform: {
'^.+\\.js$': 'jest-storybook',
},
};
32 changes: 32 additions & 0 deletions lib/jest-storybook/src/jest.storybook.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const falafel = require('falafel'); // eslint-disable-line import/no-extraneous-dependencies
const babel = require('babel-core');
const jestPreset = require('babel-preset-jest');

module.exports.process = (src, filename) => {
let output = falafel(src, { sourceType: 'module' }, (node) => { // AST just to import getStorybook. overkill?
if (node.type === 'ImportSpecifier' && node.imported.name === 'storiesOf') {
node.update(`${node.source()}, getStorybook`);
}
});

output = `
import addons, { mockChannel } from '@storybook/addons';
addons.setChannel(mockChannel());
${output.toString()}
const storybook = getStorybook();
storybook.forEach((story) => {
story.stories.forEach((subStory) => {
describe(story.kind, () => {
it(subStory.name, () => {
expect(subStory.render()).toMatchSnapshot();
}); }); }); });`;

if (babel.util.canCompile(filename)) {
src = babel.transform(output, {
filename,
presets: [jestPreset],
});
}

return src;
};

0 comments on commit 917d80a

Please sign in to comment.