UNPKG

creevey

Version:

Cross-browser screenshot testing tool for Storybook with fancy UI Runner

74 lines (62 loc) 2.29 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.addTestsFromStories = addTestsFromStories; var _mocha = require("mocha"); var _types = require("../../types"); var _stories = require("../stories"); function findOrCreateSuite(name, parent) { const suite = parent.suites.find(({ title }) => title == name) || new _mocha.Suite(name, parent.ctx); if (!suite.parent) { suite.parent = parent; parent.addSuite(suite); } return suite; } function createTest(name, fn, skip = false) { const test = new _mocha.Test(name, skip ? undefined : fn); test.pending = Boolean(skip); // NOTE Can't define skip reason in mocha https://github.com/mochajs/mocha/issues/2026 test.skipReason = skip; return test; } function addTest(rootSuite, test) { const [testName, ...suitePath] = [...test.storyPath, test.testName].reverse().filter(_types.isDefined); const suite = suitePath.reduceRight((subSuite, suiteName) => findOrCreateSuite(suiteName, subSuite), rootSuite); const mochaTest = createTest(testName, test.fn, test.skip); suite.addTest(mochaTest); mochaTest.ctx = Object.setPrototypeOf({ id: test.id, story: test.story }, suite.ctx); return mochaTest; } function removeTestOrSuite(testOrSuite) { const { parent } = testOrSuite; if (!parent) return; if (testOrSuite instanceof _mocha.Test) parent.tests = parent.tests.filter(test => test != testOrSuite); if (testOrSuite instanceof _mocha.Suite) parent.suites = parent.suites.filter(suite => suite != testOrSuite); if (parent.tests.length == 0 && parent.suites.length == 0) removeTestOrSuite(parent); } async function addTestsFromStories(rootSuite, config, { browser, watch, debug }) { const mochaTestsById = new Map(); const tests = await (0, _stories.loadTestsFromStories)(config, [browser], { watch, debug, update: testsDiff => Object.entries(testsDiff).forEach(([id, newTest]) => { const oldTest = mochaTestsById.get(id); mochaTestsById.delete(id); if (oldTest) removeTestOrSuite(oldTest); if (newTest) mochaTestsById.set(id, addTest(rootSuite, newTest)); }) }); Object.values(tests).filter(_types.isDefined).forEach(test => mochaTestsById.set(test.id, addTest(rootSuite, test))); }