creevey 0.9.0-beta.1 → 0.9.0-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (145) hide show
  1. package/lib/cjs/cli.js +5 -0
  2. package/lib/cjs/client/addon/Manager.js +412 -0
  3. package/lib/cjs/client/addon/components/Addon.js +76 -0
  4. package/lib/cjs/client/addon/components/Icons.js +42 -0
  5. package/lib/cjs/client/addon/components/Panel.js +68 -0
  6. package/lib/cjs/client/addon/components/TestSelect.js +63 -0
  7. package/lib/cjs/client/addon/components/Tools.js +114 -0
  8. package/lib/cjs/client/addon/decorator.js +11 -0
  9. package/lib/cjs/client/addon/preset.js +81 -0
  10. package/lib/cjs/client/addon/readyForCapture.js +12 -0
  11. package/lib/cjs/client/addon/register.js +96 -0
  12. package/lib/cjs/client/addon/utils.js +38 -0
  13. package/lib/cjs/client/addon/withCreevey.js +531 -0
  14. package/lib/cjs/client/shared/components/ImagesView/BlendView.js +85 -0
  15. package/lib/cjs/client/shared/components/ImagesView/ImagesView.js +88 -0
  16. package/lib/cjs/client/shared/components/ImagesView/SideBySideView.js +176 -0
  17. package/lib/cjs/client/shared/components/ImagesView/SlideView.js +179 -0
  18. package/lib/cjs/client/shared/components/ImagesView/SwapView.js +110 -0
  19. package/lib/cjs/client/shared/components/ImagesView/index.js +45 -0
  20. package/lib/cjs/client/shared/components/PageFooter/PageFooter.js +46 -0
  21. package/lib/cjs/client/shared/components/PageFooter/Paging.js +98 -0
  22. package/lib/cjs/client/shared/components/PageHeader/ImagePreview.js +78 -0
  23. package/lib/cjs/client/shared/components/PageHeader/PageHeader.js +144 -0
  24. package/lib/cjs/client/shared/components/ResultsPage.js +173 -0
  25. package/lib/cjs/client/shared/creeveyClientApi.js +103 -0
  26. package/lib/cjs/client/shared/helpers.js +482 -0
  27. package/lib/cjs/client/shared/viewMode.js +17 -0
  28. package/lib/cjs/client/web/index.html +19 -0
  29. package/lib/cjs/creevey.js +71 -0
  30. package/lib/cjs/index.js +62 -0
  31. package/lib/cjs/server/config.js +96 -0
  32. package/lib/cjs/server/docker.js +150 -0
  33. package/lib/cjs/server/extract.js +50 -0
  34. package/lib/cjs/server/index.js +83 -0
  35. package/lib/cjs/server/loaders/babel/creevey-plugin.js +88 -0
  36. package/lib/cjs/server/loaders/babel/helpers.js +479 -0
  37. package/lib/cjs/server/loaders/babel/register.js +126 -0
  38. package/lib/cjs/server/loaders/hooks/mdx.js +30 -0
  39. package/lib/cjs/server/loaders/hooks/svelte.js +65 -0
  40. package/lib/cjs/server/loaders/webpack/compile.js +286 -0
  41. package/lib/cjs/server/loaders/webpack/creevey-loader.js +174 -0
  42. package/lib/cjs/server/loaders/webpack/dummy-hmr.js +44 -0
  43. package/lib/cjs/server/loaders/webpack/mdx-loader.js +72 -0
  44. package/lib/cjs/server/loaders/webpack/start.js +41 -0
  45. package/lib/cjs/server/logger.js +47 -0
  46. package/lib/cjs/server/master/api.js +71 -0
  47. package/lib/cjs/server/master/index.js +146 -0
  48. package/lib/cjs/server/master/master.js +57 -0
  49. package/lib/cjs/server/master/pool.js +206 -0
  50. package/lib/cjs/server/master/runner.js +294 -0
  51. package/lib/cjs/server/master/server.js +129 -0
  52. package/lib/cjs/server/messages.js +266 -0
  53. package/lib/cjs/server/selenium/browser.js +680 -0
  54. package/lib/cjs/server/selenium/index.js +31 -0
  55. package/lib/cjs/server/selenium/selenoid.js +174 -0
  56. package/lib/cjs/server/stories.js +170 -0
  57. package/lib/cjs/server/storybook/entry.js +68 -0
  58. package/lib/cjs/server/storybook/helpers.js +165 -0
  59. package/lib/cjs/server/storybook/providers/browser.js +78 -0
  60. package/lib/cjs/server/storybook/providers/hybrid.js +84 -0
  61. package/lib/cjs/server/storybook/providers/nodejs.js +239 -0
  62. package/lib/cjs/server/testsFiles/parser.js +72 -0
  63. package/lib/cjs/server/testsFiles/register.js +48 -0
  64. package/lib/cjs/server/update.js +83 -0
  65. package/lib/cjs/server/utils.js +185 -0
  66. package/lib/cjs/server/worker/chai-image.js +142 -0
  67. package/lib/cjs/server/worker/helpers.js +69 -0
  68. package/lib/cjs/server/worker/index.js +15 -0
  69. package/lib/cjs/server/worker/reporter.js +120 -0
  70. package/lib/cjs/server/worker/worker.js +278 -0
  71. package/lib/cjs/shared.js +124 -0
  72. package/lib/cjs/types.js +74 -0
  73. package/lib/esm/cli.js +4 -0
  74. package/lib/esm/client/addon/Manager.js +396 -0
  75. package/lib/esm/client/addon/components/Addon.js +58 -0
  76. package/lib/esm/client/addon/components/Icons.js +27 -0
  77. package/lib/esm/client/addon/components/Panel.js +49 -0
  78. package/lib/esm/client/addon/components/TestSelect.js +49 -0
  79. package/lib/esm/client/addon/components/Tools.js +91 -0
  80. package/lib/esm/client/addon/decorator.js +2 -0
  81. package/lib/esm/client/addon/preset.js +56 -0
  82. package/lib/esm/client/addon/readyForCapture.js +5 -0
  83. package/lib/esm/client/addon/register.js +75 -0
  84. package/lib/esm/client/addon/utils.js +31 -0
  85. package/lib/esm/client/addon/withCreevey.js +509 -0
  86. package/lib/esm/client/shared/components/ImagesView/BlendView.js +63 -0
  87. package/lib/esm/client/shared/components/ImagesView/ImagesView.js +65 -0
  88. package/lib/esm/client/shared/components/ImagesView/SideBySideView.js +151 -0
  89. package/lib/esm/client/shared/components/ImagesView/SlideView.js +154 -0
  90. package/lib/esm/client/shared/components/ImagesView/SwapView.js +88 -0
  91. package/lib/esm/client/shared/components/ImagesView/index.js +5 -0
  92. package/lib/esm/client/shared/components/PageFooter/PageFooter.js +32 -0
  93. package/lib/esm/client/shared/components/PageFooter/Paging.js +84 -0
  94. package/lib/esm/client/shared/components/PageHeader/ImagePreview.js +64 -0
  95. package/lib/esm/client/shared/components/PageHeader/PageHeader.js +120 -0
  96. package/lib/esm/client/shared/components/ResultsPage.js +143 -0
  97. package/lib/esm/client/shared/creeveyClientApi.js +94 -0
  98. package/lib/esm/client/shared/helpers.js +424 -0
  99. package/lib/esm/client/shared/viewMode.js +6 -0
  100. package/lib/esm/creevey.js +56 -0
  101. package/lib/esm/index.js +7 -0
  102. package/lib/esm/server/config.js +73 -0
  103. package/lib/esm/server/docker.js +123 -0
  104. package/lib/esm/server/extract.js +34 -0
  105. package/lib/esm/server/index.js +64 -0
  106. package/lib/esm/server/loaders/babel/creevey-plugin.js +74 -0
  107. package/lib/esm/server/loaders/babel/helpers.js +462 -0
  108. package/lib/esm/server/loaders/babel/register.js +105 -0
  109. package/lib/esm/server/loaders/hooks/mdx.js +15 -0
  110. package/lib/esm/server/loaders/hooks/svelte.js +49 -0
  111. package/lib/esm/server/loaders/webpack/compile.js +263 -0
  112. package/lib/esm/server/loaders/webpack/creevey-loader.js +153 -0
  113. package/lib/esm/server/loaders/webpack/dummy-hmr.js +36 -0
  114. package/lib/esm/server/loaders/webpack/mdx-loader.js +58 -0
  115. package/lib/esm/server/loaders/webpack/start.js +27 -0
  116. package/lib/esm/server/logger.js +20 -0
  117. package/lib/esm/server/master/api.js +60 -0
  118. package/lib/esm/server/master/index.js +125 -0
  119. package/lib/esm/server/master/master.js +38 -0
  120. package/lib/esm/server/master/pool.js +187 -0
  121. package/lib/esm/server/master/runner.js +272 -0
  122. package/lib/esm/server/master/server.js +105 -0
  123. package/lib/esm/server/messages.js +234 -0
  124. package/lib/esm/server/selenium/browser.js +647 -0
  125. package/lib/esm/server/selenium/index.js +2 -0
  126. package/lib/esm/server/selenium/selenoid.js +151 -0
  127. package/lib/esm/server/stories.js +151 -0
  128. package/lib/esm/server/storybook/entry.js +44 -0
  129. package/lib/esm/server/storybook/helpers.js +106 -0
  130. package/lib/esm/server/storybook/providers/browser.js +61 -0
  131. package/lib/esm/server/storybook/providers/hybrid.js +64 -0
  132. package/lib/esm/server/storybook/providers/nodejs.js +217 -0
  133. package/lib/esm/server/testsFiles/parser.js +50 -0
  134. package/lib/esm/server/testsFiles/register.js +35 -0
  135. package/lib/esm/server/update.js +65 -0
  136. package/lib/esm/server/utils.js +146 -0
  137. package/lib/esm/server/worker/chai-image.js +130 -0
  138. package/lib/esm/server/worker/helpers.js +60 -0
  139. package/lib/esm/server/worker/index.js +1 -0
  140. package/lib/esm/server/worker/reporter.js +98 -0
  141. package/lib/esm/server/worker/worker.js +248 -0
  142. package/lib/esm/shared.js +93 -0
  143. package/lib/esm/types.js +43 -0
  144. package/lib/types/index.d.ts +2 -4
  145. package/package.json +1 -1
@@ -0,0 +1,151 @@
1
+ import path from 'path';
2
+ import { promisify } from 'util';
3
+ import { mkdir, writeFile, copyFile, lstatSync, existsSync } from 'fs';
4
+ import { downloadBinary, getCreeveyCache } from '../utils';
5
+ import { pullImages, runImage } from '../docker';
6
+ import { Octokit } from '@octokit/core';
7
+ import { subscribeOn } from '../messages';
8
+ import { isWorker } from 'cluster';
9
+ import { chmod, exec } from 'shelljs';
10
+ const mkdirAsync = promisify(mkdir);
11
+ const writeFileAsync = promisify(writeFile);
12
+ const copyFileAsync = promisify(copyFile);
13
+
14
+ async function createSelenoidConfig(browsers, {
15
+ useDocker
16
+ }) {
17
+ const selenoidConfig = {};
18
+ const selenoidConfigDir = path.join(getCreeveyCache(), 'selenoid');
19
+ browsers.forEach(({
20
+ browserName,
21
+ version = 'latest',
22
+ dockerImage = `selenoid/${browserName}:${version}`,
23
+ webdriverCommand = []
24
+ }) => {
25
+ if (!selenoidConfig[browserName]) selenoidConfig[browserName] = {
26
+ default: version,
27
+ versions: {}
28
+ };
29
+ if (!useDocker && webdriverCommand.length == 0) throw new Error('Please specify "webdriverCommand" browser option with path to browser webdriver');
30
+ selenoidConfig[browserName].versions[version] = {
31
+ image: useDocker ? dockerImage : webdriverCommand,
32
+ port: '4444',
33
+ path: !useDocker || ['chrome', 'opera', 'webkit', 'MicrosoftEdge'].includes(browserName) ? '/' : '/wd/hub'
34
+ };
35
+ });
36
+ await mkdirAsync(selenoidConfigDir, {
37
+ recursive: true
38
+ });
39
+ await writeFileAsync(path.join(selenoidConfigDir, 'browsers.json'), JSON.stringify(selenoidConfig));
40
+ return selenoidConfigDir;
41
+ }
42
+
43
+ async function downloadSelenoidBinary(destination) {
44
+ var _assets$find;
45
+
46
+ const platformNameMapping = {
47
+ darwin: 'selenoid_darwin_amd64',
48
+ linux: 'selenoid_linux_amd64',
49
+ win32: 'selenoid_windows_amd64.exe'
50
+ };
51
+ const octokit = new Octokit();
52
+ const response = await octokit.request('GET /repos/{owner}/{repo}/releases/latest', {
53
+ owner: 'aerokube',
54
+ repo: 'selenoid'
55
+ });
56
+ const {
57
+ assets
58
+ } = response.data;
59
+ const {
60
+ browser_download_url: downloadUrl,
61
+ size: binarySize
62
+ } = (_assets$find = assets.find(({
63
+ name
64
+ }) => platformNameMapping[process.platform] == name)) !== null && _assets$find !== void 0 ? _assets$find : {};
65
+ if (existsSync(destination) && lstatSync(destination).size == binarySize) return;
66
+
67
+ if (!downloadUrl) {
68
+ throw new Error(`Couldn't get download url for selenoid binary. Please download it manually from "https://github.com/aerokube/selenoid/releases/latest" and define "selenoidPath" option in the Creevey config`);
69
+ }
70
+
71
+ return downloadBinary(downloadUrl, destination);
72
+ }
73
+
74
+ export async function startSelenoidStandalone(config, debug) {
75
+ config.gridUrl = 'http://localhost:4444/wd/hub';
76
+ if (isWorker) return;
77
+ const browsers = Object.values(config.browsers).filter(browser => !browser.gridUrl);
78
+ const selenoidConfigDir = await createSelenoidConfig(browsers, {
79
+ useDocker: false
80
+ });
81
+ const binaryPath = path.join(selenoidConfigDir, process.platform == 'win32' ? 'selenoid.exe' : 'selenoid');
82
+
83
+ if (config.selenoidPath) {
84
+ await copyFileAsync(path.resolve(config.selenoidPath), binaryPath);
85
+ } else {
86
+ await downloadSelenoidBinary(binaryPath);
87
+ } // TODO Download browser webdrivers
88
+
89
+
90
+ try {
91
+ if (process.platform != 'win32') chmod('+x', binaryPath);
92
+ } catch (_) {
93
+ /* noop */
94
+ }
95
+
96
+ const selenoidProcess = exec(`${binaryPath} -conf ./browsers.json -disable-docker`, {
97
+ async: true,
98
+ cwd: selenoidConfigDir
99
+ });
100
+
101
+ if (debug) {
102
+ var _selenoidProcess$stdo, _selenoidProcess$stde;
103
+
104
+ (_selenoidProcess$stdo = selenoidProcess.stdout) === null || _selenoidProcess$stdo === void 0 ? void 0 : _selenoidProcess$stdo.pipe(process.stdout);
105
+ (_selenoidProcess$stde = selenoidProcess.stderr) === null || _selenoidProcess$stde === void 0 ? void 0 : _selenoidProcess$stde.pipe(process.stderr);
106
+ }
107
+
108
+ subscribeOn('shutdown', () => selenoidProcess.kill());
109
+ }
110
+ export async function startSelenoidContainer(config, debug) {
111
+ const browsers = Object.values(config.browsers).filter(browser => !browser.gridUrl);
112
+ const images = [];
113
+ let limit = 0;
114
+ browsers.forEach(({
115
+ browserName,
116
+ version = 'latest',
117
+ limit: browserLimit = 1,
118
+ dockerImage = `selenoid/${browserName}:${version}`
119
+ }) => {
120
+ limit += browserLimit;
121
+ images.push(dockerImage);
122
+ });
123
+ const selenoidImage = config.dockerImage;
124
+ const pullOptions = {
125
+ auth: config.dockerAuth,
126
+ platform: config.dockerImagePlatform
127
+ };
128
+
129
+ if (config.pullImages) {
130
+ await pullImages([selenoidImage], pullOptions);
131
+ await pullImages(images, pullOptions);
132
+ } // TODO Allow pass custom options
133
+
134
+
135
+ const selenoidOptions = {
136
+ ExposedPorts: {
137
+ '4444/tcp': {}
138
+ },
139
+ HostConfig: {
140
+ PortBindings: {
141
+ '4444/tcp': [{
142
+ HostPort: '4444'
143
+ }]
144
+ },
145
+ Binds: ['/var/run/docker.sock:/var/run/docker.sock', `${await createSelenoidConfig(browsers, {
146
+ useDocker: true
147
+ })}:/etc/selenoid/:ro`]
148
+ }
149
+ };
150
+ return runImage(selenoidImage, ['-limit', String(limit)], selenoidOptions, debug);
151
+ }
@@ -0,0 +1,151 @@
1
+ import path from 'path';
2
+ import { mkdirSync, writeFileSync } from 'fs';
3
+ import { createHash } from 'crypto';
4
+ import { mapValues, pick } from 'lodash';
5
+ import { isDefined, isFunction, isObject } from '../types';
6
+ import { shouldSkip, removeProps } from './utils';
7
+ import { isStorybookVersionLessThan } from './storybook/helpers';
8
+
9
+ function storyTestFabric(delay, testFn) {
10
+ return async function storyTest() {
11
+ var _testFn$call;
12
+
13
+ delay ? await new Promise(resolve => setTimeout(resolve, delay)) : void 0;
14
+ await ((_testFn$call = testFn === null || testFn === void 0 ? void 0 : testFn.call(this)) !== null && _testFn$call !== void 0 ? _testFn$call : this.screenshots.length > 0 ? this.expect(this.screenshots.reduce((screenshots, {
15
+ imageName,
16
+ screenshot
17
+ }, index) => ({ ...screenshots,
18
+ [imageName !== null && imageName !== void 0 ? imageName : `screenshot_${index}`]: screenshot
19
+ }), {})).to.matchImages() : this.expect(await this.takeScreenshot()).to.matchImage());
20
+ };
21
+ }
22
+
23
+ function createCreeveyTest(browser, storyMeta, skipOptions, testName) {
24
+ const {
25
+ kind,
26
+ name: story,
27
+ id: storyId
28
+ } = storyMeta;
29
+ const path = [kind, story, testName, browser].filter(isDefined);
30
+ const skip = skipOptions ? shouldSkip(browser, {
31
+ kind,
32
+ story
33
+ }, skipOptions, testName) : false;
34
+ const id = createHash('sha1').update(path.join('/')).digest('hex');
35
+ return {
36
+ id,
37
+ skip,
38
+ browser,
39
+ testName,
40
+ storyPath: [...kind.split('/').map(x => x.trim()), story],
41
+ storyId
42
+ };
43
+ }
44
+
45
+ function convertStories(browserName, stories) {
46
+ const tests = {};
47
+ (Array.isArray(stories) ? stories : Object.values(stories)).forEach(storyMeta => {
48
+ var _storyMeta$parameters;
49
+
50
+ // TODO Skip docsOnly stories for now
51
+ if (storyMeta.parameters.docsOnly) return;
52
+ const {
53
+ delay: delayParam,
54
+ tests: storyTests,
55
+ skip
56
+ } = (_storyMeta$parameters = storyMeta.parameters.creevey) !== null && _storyMeta$parameters !== void 0 ? _storyMeta$parameters : {};
57
+ const delay = typeof delayParam == 'number' ? delayParam : delayParam !== null && delayParam !== void 0 && delayParam.for.includes(browserName) ? delayParam.ms : 0; // typeof tests === "undefined" => rootSuite -> kindSuite -> storyTest -> [browsers.png]
58
+ // typeof tests === "function" => rootSuite -> kindSuite -> storyTest -> browser -> [images.png]
59
+ // typeof tests === "object" => rootSuite -> kindSuite -> storySuite -> test -> [browsers.png]
60
+ // typeof tests === "object" => rootSuite -> kindSuite -> storySuite -> test -> browser -> [images.png]
61
+
62
+ if (!storyTests) {
63
+ const test = createCreeveyTest(browserName, storyMeta, skip);
64
+ tests[test.id] = { ...test,
65
+ storyId: storyMeta.id,
66
+ story: storyMeta,
67
+ fn: storyTestFabric(delay)
68
+ };
69
+ return;
70
+ }
71
+
72
+ Object.entries(storyTests).forEach(([testName, testFn]) => {
73
+ const test = createCreeveyTest(browserName, storyMeta, skip, testName);
74
+ tests[test.id] = { ...test,
75
+ storyId: storyMeta.id,
76
+ story: storyMeta,
77
+ fn: storyTestFabric(delay, testFn)
78
+ };
79
+ });
80
+ });
81
+ return tests;
82
+ }
83
+
84
+ export async function loadTestsFromStories(browsers, provider, update) {
85
+ const testIdsByFiles = new Map();
86
+ const stories = await provider(storiesByFiles => {
87
+ const testsDiff = {};
88
+ const tests = {};
89
+ browsers.forEach(browser => {
90
+ Array.from(storiesByFiles.entries()).forEach(([filename, stories]) => {
91
+ var _testIdsByFiles$get$f, _testIdsByFiles$get;
92
+
93
+ Object.assign(tests, convertStories(browser, stories));
94
+ const changed = Object.keys(tests);
95
+ const removed = (_testIdsByFiles$get$f = (_testIdsByFiles$get = testIdsByFiles.get(filename)) === null || _testIdsByFiles$get === void 0 ? void 0 : _testIdsByFiles$get.filter(testId => !tests[testId])) !== null && _testIdsByFiles$get$f !== void 0 ? _testIdsByFiles$get$f : [];
96
+ if (changed.length == 0) testIdsByFiles.delete(filename);else testIdsByFiles.set(filename, changed);
97
+ Object.assign(testsDiff, tests);
98
+ removed.forEach(testId => testsDiff[testId] = undefined);
99
+ });
100
+ });
101
+ update === null || update === void 0 ? void 0 : update(testsDiff);
102
+ });
103
+ const tests = browsers.reduce((tests, browser) => Object.assign(tests, convertStories(browser, stories)), {});
104
+ Object.values(tests).filter(isDefined).forEach(({
105
+ id,
106
+ story: {
107
+ parameters: {
108
+ fileName
109
+ }
110
+ }
111
+ }) => {
112
+ var _testIdsByFiles$get2;
113
+
114
+ return (// TODO Don't use filename as a key, due possible collisions if two require.context with same structure of modules are defined
115
+ testIdsByFiles.set(fileName, [...((_testIdsByFiles$get2 = testIdsByFiles.get(fileName)) !== null && _testIdsByFiles$get2 !== void 0 ? _testIdsByFiles$get2 : []), id])
116
+ );
117
+ });
118
+ return tests;
119
+ }
120
+ export function saveStoriesJson(storiesData, extract) {
121
+ var _storiesData$stories;
122
+
123
+ const outputDir = typeof extract == 'boolean' ? 'storybook-static' : extract;
124
+
125
+ if (!isStorybookVersionLessThan(6)) {
126
+ // NOTE Copy-pasted from Storybook's `getStoriesJsonData` method
127
+ const allowed = ['fileName', 'docsOnly', 'framework', '__id', '__isArgsStory'];
128
+ storiesData.globalParameters = pick(storiesData.globalParameters, allowed); // @ts-expect-error ignore error
129
+
130
+ storiesData.kindParameters = mapValues(storiesData.kindParameters, v => pick(v, allowed)); // @ts-expect-error ignore error
131
+
132
+ storiesData.stories = mapValues(storiesData.stories, v => ({ ...pick(v, ['id', 'name', 'kind', 'story']),
133
+ parameters: pick(v.parameters, allowed)
134
+ }));
135
+ } // TODO Fix args stories
136
+
137
+
138
+ removeProps(storiesData !== null && storiesData !== void 0 ? storiesData : {}, ['stories', () => true, 'parameters', '__isArgsStory']);
139
+ Object.values((_storiesData$stories = storiesData === null || storiesData === void 0 ? void 0 : storiesData.stories) !== null && _storiesData$stories !== void 0 ? _storiesData$stories : {}).forEach(story => isObject(story) && 'parameters' in story && isObject(story.parameters) && delete story.parameters.__isArgsStory);
140
+ mkdirSync(outputDir, {
141
+ recursive: true
142
+ });
143
+ writeFileSync(path.join(outputDir, 'stories.json'), JSON.stringify(storiesData, null, 2));
144
+ }
145
+ export function saveTestsJson(tests, dstPath = process.cwd()) {
146
+ mkdirSync(dstPath, {
147
+ recursive: true
148
+ });
149
+ writeFileSync(path.join(dstPath, 'tests.json'), // eslint-disable-next-line @typescript-eslint/no-unsafe-return
150
+ JSON.stringify(tests, (_, value) => isFunction(value) ? value.toString() : value, 2));
151
+ }
@@ -0,0 +1,44 @@
1
+ var _api$channel, _api$context;
2
+
3
+ import { addons } from '@storybook/addons';
4
+ import { getStorybookFramework, isStorybookVersionLessThan, resolveFromStorybook } from './helpers';
5
+ const framework = getStorybookFramework(); // eslint-disable-next-line @typescript-eslint/no-var-requires
6
+
7
+ const core = require(resolveFromStorybook('@storybook/core')); //@ts-expect-error: 6.2 use named exports
8
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
9
+
10
+
11
+ const start = isStorybookVersionLessThan(6, 2) ? core.default.start : core.start;
12
+ const api = start(() => void 0);
13
+ export const channel = isStorybookVersionLessThan(6, 4) ? (_api$channel = api.channel) !== null && _api$channel !== void 0 ? _api$channel : (_api$context = api.context) === null || _api$context === void 0 ? void 0 : _api$context.channel : addons.getChannel();
14
+ export const clientApi = api.clientApi;
15
+ export const forceReRender = api.forceReRender;
16
+ export const storiesOf = (kind, m) => {
17
+ return clientApi.storiesOf(kind, m).addParameters({
18
+ framework
19
+ });
20
+ };
21
+ export const configure = (...args) => {
22
+ if (isStorybookVersionLessThan(5, 2)) {
23
+ //NOTE: Storybook <= 5.1 pass args as is
24
+ //@ts-expect-error: ignore it
25
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
26
+ return api.configApi.configure(...args);
27
+ }
28
+
29
+ if (isStorybookVersionLessThan(6)) {
30
+ //NOTE: Storybook <= 5.3 pass `framework` as last argument
31
+ //@ts-expect-error: ignore it
32
+ return api.configure(...args, framework);
33
+ } //NOTE Storybook 6.x pass `framework` as first argument
34
+ //@ts-expect-error: ignore it
35
+
36
+
37
+ return api.configure(framework, ...args);
38
+ };
39
+ export const addDecorator = clientApi.addDecorator;
40
+ export const addParameters = clientApi.addParameters;
41
+ export const clearDecorators = clientApi.clearDecorators;
42
+ export const setAddon = clientApi.setAddon;
43
+ export const getStorybook = clientApi.getStorybook;
44
+ export const raw = clientApi.raw;
@@ -0,0 +1,106 @@
1
+ import path from 'path';
2
+ import resolveFrom from 'resolve-from';
3
+ const supportedFrameworks = ['react', 'vue', 'vue3', 'angular', 'marionette', 'mithril', 'marko', 'html', 'svelte', 'riot', 'ember', 'preact', 'rax', 'aurelia', 'server', 'web-components'];
4
+ export const storybookDirRef = {
5
+ current: path.resolve('.storybook')
6
+ };
7
+ export const resolveFromStorybook = modulePath => resolveFrom(storybookDirRef.current, modulePath);
8
+ export const resolveFromStorybookAddonDocs = modulePath => resolveFrom(resolveFromStorybook('@storybook/addon-docs'), modulePath);
9
+ export const resolveFromStorybookBuilderWebpack4 = modulePath => resolveFrom(resolveFromStorybook('@storybook/builder-webpack4'), modulePath);
10
+ export const resolveFromStorybookCore = modulePath => resolveFrom(resolveFromStorybook('@storybook/core'), modulePath);
11
+ export const resolveFromStorybookCoreServer = modulePath => resolveFrom(resolveFromStorybook('@storybook/core-server'), modulePath);
12
+
13
+ const importFromStorybook = modulePath => import(resolveFromStorybook(modulePath));
14
+
15
+ export const importStorybookClientLogger = () => importFromStorybook('@storybook/client-logger');
16
+ export const importStorybookCoreCommon = () => importFromStorybook('@storybook/core-common');
17
+ export const importStorybookCoreEvents = () => importFromStorybook('@storybook/core-events');
18
+ export function hasDocsAddon() {
19
+ try {
20
+ resolveFromStorybook('@storybook/addon-docs');
21
+ return true;
22
+ } catch (_) {
23
+ return false;
24
+ }
25
+ }
26
+ export function hasSvelteCSFAddon() {
27
+ try {
28
+ resolveFromStorybook('@storybook/addon-svelte-csf');
29
+ return true;
30
+ } catch (_) {
31
+ return false;
32
+ }
33
+ }
34
+ export function getStorybookVersion() {
35
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
36
+ const {
37
+ version
38
+ } = require(resolveFromStorybook('@storybook/core/package.json'));
39
+
40
+ return version;
41
+ }
42
+ export function isStorybookVersionLessThan(major, minor) {
43
+ var _process$env$__CREEVE;
44
+
45
+ const [sbMajor, sbMinor] = ((_process$env$__CREEVE = process.env.__CREEVEY_STORYBOOK_VERSION__) !== null && _process$env$__CREEVE !== void 0 ? _process$env$__CREEVE : getStorybookVersion()).split('.');
46
+ return Number(sbMajor) < major || minor != undefined && Number(sbMajor) == major && Number(sbMinor) < minor;
47
+ }
48
+ export function isStorybookVersionGreaterThan(major, minor) {
49
+ var _process$env$__CREEVE2;
50
+
51
+ const [sbMajor, sbMinor] = ((_process$env$__CREEVE2 = process.env.__CREEVEY_STORYBOOK_VERSION__) !== null && _process$env$__CREEVE2 !== void 0 ? _process$env$__CREEVE2 : getStorybookVersion()).split('.');
52
+ return Number(sbMajor) > major || minor != undefined && Number(sbMajor) == major && Number(sbMinor) > minor;
53
+ }
54
+ export function isStorybookVersion(major, minor) {
55
+ var _process$env$__CREEVE3;
56
+
57
+ const [sbMajor, sbMinor] = ((_process$env$__CREEVE3 = process.env.__CREEVEY_STORYBOOK_VERSION__) !== null && _process$env$__CREEVE3 !== void 0 ? _process$env$__CREEVE3 : getStorybookVersion()).split('.');
58
+ return Number(sbMajor) == major || minor != undefined && Number(sbMajor) == major && Number(sbMinor) == minor;
59
+ }
60
+ export function getStorybookFramework() {
61
+ var _process$env$__CREEVE4;
62
+
63
+ const framework = (_process$env$__CREEVE4 = process.env.__CREEVEY_STORYBOOK_FRAMEWORK__) !== null && _process$env$__CREEVE4 !== void 0 ? _process$env$__CREEVE4 : supportedFrameworks.find(framework => {
64
+ try {
65
+ return require.resolve(resolveFromStorybook(`@storybook/${framework}`));
66
+ } catch (_) {
67
+ return false;
68
+ }
69
+ });
70
+ if (!framework) throw new Error("Couldn't detect used Storybook framework. Please ensure that you install `@storybook/<framework>` package");
71
+ return framework;
72
+ }
73
+ export const storybookConfigRef = {
74
+ current: {
75
+ stories: []
76
+ }
77
+ };
78
+ export async function importStorybookConfig() {
79
+ const configPath = `${storybookDirRef.current}/main`;
80
+
81
+ try {
82
+ return storybookConfigRef.current = (await import(require.resolve(configPath))).default;
83
+ } catch (_) {
84
+ const storybookUtilsPath = isStorybookVersionLessThan(6, 2) ? '@storybook/core/dist/server/utils' : '@storybook/core-common/dist/cjs/utils';
85
+ const serverRequireModule = isStorybookVersionLessThan(6, 2) ? 'server-require' : 'interpret-require'; // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
86
+
87
+ const {
88
+ getInterpretedFile
89
+ } = await import(resolveFromStorybook(`${storybookUtilsPath}/interpret-files`)); // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
90
+
91
+ const {
92
+ default: serverRequireFallback,
93
+ serverRequire = serverRequireFallback
94
+ } = await import(resolveFromStorybook(`${storybookUtilsPath}/${serverRequireModule}`)); // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
95
+
96
+ const mainConfigFile = isStorybookVersionLessThan(6, 1) ? configPath : // eslint-disable-next-line @typescript-eslint/no-unsafe-call
97
+ getInterpretedFile(configPath); // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call
98
+
99
+ return storybookConfigRef.current = serverRequire(mainConfigFile);
100
+ }
101
+ }
102
+ export async function isCSFv3Enabled() {
103
+ var _await$importStoryboo, _await$importStoryboo2, _await$importStoryboo3;
104
+
105
+ return (_await$importStoryboo = (_await$importStoryboo2 = await importStorybookConfig()) === null || _await$importStoryboo2 === void 0 ? void 0 : (_await$importStoryboo3 = _await$importStoryboo2.features) === null || _await$importStoryboo3 === void 0 ? void 0 : _await$importStoryboo3.previewCsfV3) !== null && _await$importStoryboo !== void 0 ? _await$importStoryboo : false;
106
+ }
@@ -0,0 +1,61 @@
1
+ import cluster, { isMaster } from 'cluster';
2
+ import { loadStoriesFromBrowser } from '../../selenium';
3
+ import { emitStoriesMessage, sendStoriesMessage, subscribeOn, subscribeOnWorker } from '../../messages';
4
+ import { deserializeRawStories } from '../../../shared';
5
+ import { isDefined } from '../../../types';
6
+ import { logger } from '../../logger';
7
+ export async function loadStories(_config, {
8
+ port
9
+ }, storiesListener) {
10
+ if (isMaster) {
11
+ return new Promise(resolve => {
12
+ const worker = Object.values(cluster.workers).filter(isDefined).find(worker => worker.isConnected());
13
+
14
+ if (worker) {
15
+ const unsubscribe = subscribeOnWorker(worker, 'stories', message => {
16
+ if (message.type == 'set') {
17
+ const {
18
+ stories,
19
+ oldTests
20
+ } = message.payload;
21
+ if (oldTests.length > 0) logger.warn(`If you use browser stories provider of CSFv3 Storybook feature\n` + `Creevey will not load tests defined in story parameters from following stories:\n` + oldTests.join('\n'));
22
+ unsubscribe();
23
+ resolve(stories);
24
+ }
25
+ });
26
+ sendStoriesMessage(worker, {
27
+ type: 'get'
28
+ });
29
+ }
30
+
31
+ subscribeOn('stories', message => {
32
+ // TODO updates only one browser :(
33
+ if (message.type == 'update') storiesListener(new Map(message.payload));
34
+ });
35
+ });
36
+ } else {
37
+ subscribeOn('stories', message => {
38
+ if (message.type == 'get') emitStoriesMessage({
39
+ type: 'set',
40
+ payload: {
41
+ stories,
42
+ oldTests: storiesWithOldTests
43
+ }
44
+ });
45
+ if (message.type == 'update') storiesListener(new Map(message.payload));
46
+ });
47
+ const stories = deserializeRawStories(await loadStoriesFromBrowser(port));
48
+ const storiesWithOldTests = [];
49
+ Object.values(stories).forEach(story => {
50
+ var _parameters, _parameters$creevey;
51
+
52
+ if ((_parameters = story.parameters) !== null && _parameters !== void 0 && (_parameters$creevey = _parameters.creevey) !== null && _parameters$creevey !== void 0 && _parameters$creevey.tests) {
53
+ var _parameters2, _parameters2$creevey;
54
+
55
+ (_parameters2 = story.parameters) === null || _parameters2 === void 0 ? true : (_parameters2$creevey = _parameters2.creevey) === null || _parameters2$creevey === void 0 ? true : delete _parameters2$creevey.tests;
56
+ storiesWithOldTests.push(`${story.kind}/${story.name}`);
57
+ }
58
+ });
59
+ return stories;
60
+ }
61
+ }
@@ -0,0 +1,64 @@
1
+ import chokidar from 'chokidar';
2
+ import { loadStories as browserProvider } from './browser';
3
+ import { logger } from '../../logger';
4
+ import parse from '../../testsFiles/parser';
5
+ import { readDirRecursive } from '../../../server/utils';
6
+ import { combineParameters } from '../../../shared';
7
+ export async function loadStories(_config, {
8
+ port
9
+ }, storiesListener) {
10
+ let creeveyParamsByStoryId = {};
11
+
12
+ const mergeParamsFromTestsToStory = (story, creeveyParams) => {
13
+ if (story.parameters) {
14
+ story.parameters.creevey = combineParameters(story.parameters.creevey || {}, creeveyParams);
15
+ }
16
+ };
17
+
18
+ const stories = await browserProvider(_config, {
19
+ port
20
+ }, updatedStoriesByFiles => {
21
+ Array.from(updatedStoriesByFiles.entries()).forEach(([, storiesArray]) => {
22
+ storiesArray.forEach(story => {
23
+ const creeveyParams = creeveyParamsByStoryId[story.id];
24
+
25
+ if (creeveyParams) {
26
+ mergeParamsFromTestsToStory(story, creeveyParams);
27
+ }
28
+ });
29
+ });
30
+ storiesListener(updatedStoriesByFiles);
31
+ }); // TODO fix test files hot reloading
32
+
33
+ creeveyParamsByStoryId = await parseParams(_config
34
+ /*, (data) => console.log(data) */
35
+ );
36
+ Object.entries(stories).forEach(([storyId, story]) => {
37
+ mergeParamsFromTestsToStory(story, creeveyParamsByStoryId[storyId]);
38
+ });
39
+ return stories;
40
+ }
41
+
42
+ async function parseParams(config, listener) {
43
+ if (!config.testsDir) {
44
+ return Promise.resolve({});
45
+ }
46
+
47
+ const testFiles = readDirRecursive(config.testsDir).filter(file => {
48
+ var _config$testsRegex;
49
+
50
+ return (_config$testsRegex = config.testsRegex) === null || _config$testsRegex === void 0 ? void 0 : _config$testsRegex.test(file);
51
+ });
52
+ await (await import('../../testsFiles/register')).default(config);
53
+
54
+ if (listener) {
55
+ chokidar.watch(testFiles).on('change', filePath => {
56
+ logger.debug(`changed: ${filePath}`); // doesn't work, always returns {} due modules caching
57
+ // see https://github.com/nodejs/modules/issues/307
58
+
59
+ void parse(testFiles).then(data => listener(data));
60
+ });
61
+ }
62
+
63
+ return parse(testFiles);
64
+ }