diff --git a/package.json b/package.json index cb2858e8180e..094168f3b16d 100644 --- a/package.json +++ b/package.json @@ -44,6 +44,7 @@ "babel-polyfill": "^6.13.0", "babel-runtime": "6.x.x", "chalk": "^1.1.3", + "chokidar": "^1.6.0", "jest-diff": "^15.1.0", "jest-snapshot": "^15.1.1", "promptly": "^2.1.0", diff --git a/src/cli.js b/src/cli.js index 8c6db727993e..9e3923b6ffc6 100644 --- a/src/cli.js +++ b/src/cli.js @@ -2,6 +2,7 @@ import runTests from './test_runner'; import { getStorybook } from '@kadira/storybook'; import path from 'path'; import program from 'commander'; +import chokidar from 'chokidar'; const { jasmine } = global; @@ -12,44 +13,33 @@ program .option('-i, --update-interactive [boolean]', 'Update saved story snapshots interactively') .option('-g, --grep [string]', 'only test stories matching regexp') + .option('-w, --watch [boolean]', 'watch file changes and rerun tests') .parse(process.argv); const configDir = program.configDir || './.storybook'; const configPath = path.resolve(`${configDir}`, 'config'); -require(configPath); - -let storybook = getStorybook(); - -const { update, updateInteractive:updateI, grep } = program; - -if(grep) { - const filteredStorybook = []; - for (const group of storybook) { - const re = new RegExp(grep); - if(re.test(group.kind)){ - filteredStorybook.push(group); - continue; - } - const filteredGroup = { - kind: group.kind, - stories: [] - }; - - for (const story of group.stories) { - if(re.test(story.name)){ - filteredGroup.stories.push(story); - continue; - } - } - - if(filteredGroup.stories.length > 0){ - filteredStorybook.push(filteredGroup); - } - } - - storybook = filteredStorybook; +if(program.watch) { + var watcher = chokidar.watch('.', { + ignored: 'node_modules', // TODO: Should node_modules also be watched? + persistent: true + }); + + watcher.on('ready', () => { + console.log('storybook-snapshot-test is in watch mode.'); + watcher.on('all', () => { + // Need to remove the require cache. Because it containes modules before + // changes were made. + Object.keys(require.cache).forEach(key => { + delete require.cache[key]; + }) + require(configPath); + const changedStorybook = require('@kadira/storybook').getStorybook() + runTests(changedStorybook, program); + }); + }) } -runTests(storybook, {configDir, update, updateI}); +require(configPath); +runTests(getStorybook(), program); diff --git a/src/test_runner.js b/src/test_runner.js index 967db2a1b3fe..36029c3e9ad4 100644 --- a/src/test_runner.js +++ b/src/test_runner.js @@ -4,8 +4,16 @@ import diff from 'jest-diff'; import chalk from 'chalk'; import promptly from 'promptly'; import path from 'path'; +import { filterStorybook } from './util'; + +export default async function runTests(storybook, options) { + const { + configDir = './.storybook', + update, + updateInteractive: updateI, + grep + } = options; -export default async function runTests(storybook, {configDir, update, updateI}) { const allTestState = { added: 0, matched: 0, @@ -14,7 +22,7 @@ export default async function runTests(storybook, {configDir, update, updateI}) obsolete: 0, } - for (const group of storybook) { + for (const group of filterStorybook(storybook, grep)) { const filePath = path.resolve(`${configDir}`, `${group.kind}`); console.log(chalk.underline(`${group.kind}`)); @@ -138,7 +146,7 @@ function logAllState(state) { } } -async function confirmUpate(callback) { +async function confirmUpate() { process.stdout.write('\nReceived story is different from stored snapshot.\n'); let ans = await promptly.prompt('Should this snapshot be updated?(y/n)'); diff --git a/src/util.js b/src/util.js new file mode 100644 index 000000000000..336e37eabfd5 --- /dev/null +++ b/src/util.js @@ -0,0 +1,32 @@ +export function filterStorybook(storybook, grep) { + if(!grep || grep.length === 0 ){ + return storybook + } + + const filteredStorybook = []; + for (const group of storybook) { + const re = new RegExp(grep); + if(re.test(group.kind)){ + filteredStorybook.push(group); + continue; + } + + const filteredGroup = { + kind: group.kind, + stories: [] + }; + + for (const story of group.stories) { + if(re.test(story.name)){ + filteredGroup.stories.push(story); + continue; + } + } + + if(filteredGroup.stories.length > 0){ + filteredStorybook.push(filteredGroup); + } + } + + return filteredStorybook; +} \ No newline at end of file