creevey 0.9.0-beta.1 → 0.9.0-non-webpack.0

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 (267) hide show
  1. package/AUTHORS +15 -15
  2. package/CHANGELOG.md +1275 -1275
  3. package/LICENSE +21 -21
  4. package/README.md +7 -0
  5. package/addon/README.md +3 -0
  6. package/addon/package.json +4 -0
  7. package/docs/config.md +212 -212
  8. package/docs/grid.md +10 -10
  9. package/docs/tests.md +63 -63
  10. package/jest.config.js +6 -0
  11. package/lib/cjs/cli.js +5 -0
  12. package/lib/cjs/client/addon/Manager.js +418 -0
  13. package/lib/cjs/client/addon/components/Addon.js +76 -0
  14. package/lib/cjs/client/addon/components/Icons.js +42 -0
  15. package/lib/cjs/client/addon/components/Panel.js +68 -0
  16. package/lib/cjs/client/addon/components/TestSelect.js +63 -0
  17. package/lib/cjs/client/addon/components/Tools.js +114 -0
  18. package/lib/cjs/client/addon/decorator.js +11 -0
  19. package/lib/cjs/client/addon/index.js +31 -0
  20. package/lib/cjs/client/addon/preset.js +81 -0
  21. package/lib/cjs/client/addon/readyForCapture.js +12 -0
  22. package/lib/cjs/client/addon/register.js +100 -0
  23. package/lib/cjs/client/addon/utils.js +38 -0
  24. package/lib/cjs/client/addon/withCreevey.js +558 -0
  25. package/lib/cjs/client/shared/components/ImagesView/BlendView.js +85 -0
  26. package/lib/cjs/client/shared/components/ImagesView/ImagesView.js +88 -0
  27. package/lib/cjs/client/shared/components/ImagesView/SideBySideView.js +176 -0
  28. package/lib/cjs/client/shared/components/ImagesView/SlideView.js +179 -0
  29. package/lib/cjs/client/shared/components/ImagesView/SwapView.js +110 -0
  30. package/lib/cjs/client/shared/components/ImagesView/index.js +45 -0
  31. package/lib/cjs/client/shared/components/PageFooter/PageFooter.js +46 -0
  32. package/lib/cjs/client/shared/components/PageFooter/Paging.js +98 -0
  33. package/lib/cjs/client/shared/components/PageHeader/ImagePreview.js +78 -0
  34. package/lib/cjs/client/shared/components/PageHeader/PageHeader.js +144 -0
  35. package/lib/cjs/client/shared/components/ResultsPage.js +173 -0
  36. package/lib/cjs/client/shared/creeveyClientApi.js +107 -0
  37. package/lib/cjs/client/shared/helpers.js +482 -0
  38. package/lib/cjs/client/shared/viewMode.js +17 -0
  39. package/lib/cjs/client/web/142.js +2 -0
  40. package/lib/cjs/client/web/142.js.LICENSE.txt +12 -0
  41. package/lib/cjs/client/web/32.js +1 -0
  42. package/lib/cjs/client/web/551.js +1 -0
  43. package/lib/cjs/client/web/566.js +2 -0
  44. package/lib/cjs/client/web/566.js.LICENSE.txt +31 -0
  45. package/lib/cjs/client/web/691.js +2 -0
  46. package/lib/cjs/client/web/691.js.LICENSE.txt +8 -0
  47. package/lib/cjs/client/web/725.js +1 -0
  48. package/lib/cjs/client/web/index.html +19 -0
  49. package/lib/cjs/client/web/main.js +2 -38
  50. package/lib/cjs/client/web/main.js.LICENSE.txt +49 -0
  51. package/lib/cjs/creevey.js +69 -0
  52. package/lib/cjs/index.js +62 -0
  53. package/lib/cjs/server/config.js +96 -0
  54. package/lib/cjs/server/docker.js +146 -0
  55. package/lib/cjs/server/extract.js +50 -0
  56. package/lib/cjs/server/index.js +83 -0
  57. package/lib/cjs/server/loaders/babel/creevey-plugin.js +86 -0
  58. package/lib/cjs/server/loaders/babel/helpers.js +469 -0
  59. package/lib/cjs/server/loaders/babel/register.js +124 -0
  60. package/lib/cjs/server/loaders/hooks/mdx.js +30 -0
  61. package/lib/cjs/server/loaders/hooks/svelte.js +65 -0
  62. package/lib/cjs/server/loaders/webpack/compile.js +293 -0
  63. package/lib/cjs/server/loaders/webpack/creevey-loader.js +179 -0
  64. package/lib/cjs/server/loaders/webpack/dummy-hmr.js +39 -0
  65. package/lib/cjs/server/loaders/webpack/mdx-loader.js +72 -0
  66. package/lib/cjs/server/loaders/webpack/start.js +41 -0
  67. package/lib/cjs/server/logger.js +48 -0
  68. package/lib/cjs/server/master/api.js +71 -0
  69. package/lib/cjs/server/master/index.js +146 -0
  70. package/lib/cjs/server/master/master.js +57 -0
  71. package/lib/cjs/server/master/pool.js +197 -0
  72. package/lib/cjs/server/master/runner.js +281 -0
  73. package/lib/cjs/server/master/server.js +129 -0
  74. package/lib/cjs/server/messages.js +264 -0
  75. package/lib/cjs/server/selenium/browser.js +672 -0
  76. package/lib/cjs/server/selenium/index.js +31 -0
  77. package/lib/cjs/server/selenium/selenoid.js +172 -0
  78. package/lib/cjs/server/stories.js +159 -0
  79. package/lib/cjs/server/storybook/entry.js +70 -0
  80. package/lib/cjs/server/storybook/helpers.js +159 -0
  81. package/lib/cjs/server/storybook/providers/browser.js +74 -0
  82. package/lib/cjs/server/storybook/providers/hybrid.js +84 -0
  83. package/lib/cjs/server/storybook/providers/nodejs.js +239 -0
  84. package/lib/cjs/server/testsFiles/parser.js +72 -0
  85. package/lib/cjs/server/testsFiles/register.js +48 -0
  86. package/lib/cjs/server/update.js +79 -0
  87. package/lib/cjs/server/utils.js +183 -0
  88. package/lib/cjs/server/worker/chai-image.js +142 -0
  89. package/lib/cjs/server/worker/helpers.js +69 -0
  90. package/lib/cjs/server/worker/index.js +15 -0
  91. package/lib/cjs/server/worker/reporter.js +108 -0
  92. package/lib/cjs/server/worker/worker.js +268 -0
  93. package/lib/cjs/shared/index.js +89 -0
  94. package/lib/cjs/shared/serializeRegExp.js +41 -0
  95. package/lib/cjs/types.js +74 -0
  96. package/lib/esm/cli.js +4 -0
  97. package/lib/esm/client/addon/Manager.js +402 -0
  98. package/lib/esm/client/addon/components/Addon.js +58 -0
  99. package/lib/esm/client/addon/components/Icons.js +27 -0
  100. package/lib/esm/client/addon/components/Panel.js +49 -0
  101. package/lib/esm/client/addon/components/TestSelect.js +49 -0
  102. package/lib/esm/client/addon/components/Tools.js +91 -0
  103. package/lib/esm/client/addon/decorator.js +2 -0
  104. package/lib/esm/client/addon/index.js +2 -0
  105. package/lib/esm/client/addon/preset.js +56 -0
  106. package/lib/esm/client/addon/readyForCapture.js +5 -0
  107. package/lib/esm/client/addon/register.js +79 -0
  108. package/lib/esm/client/addon/utils.js +31 -0
  109. package/lib/esm/client/addon/withCreevey.js +534 -0
  110. package/lib/esm/client/shared/components/ImagesView/BlendView.js +63 -0
  111. package/lib/esm/client/shared/components/ImagesView/ImagesView.js +65 -0
  112. package/lib/esm/client/shared/components/ImagesView/SideBySideView.js +151 -0
  113. package/lib/esm/client/shared/components/ImagesView/SlideView.js +154 -0
  114. package/lib/esm/client/shared/components/ImagesView/SwapView.js +88 -0
  115. package/lib/esm/client/shared/components/ImagesView/index.js +5 -0
  116. package/lib/esm/client/shared/components/PageFooter/PageFooter.js +32 -0
  117. package/lib/esm/client/shared/components/PageFooter/Paging.js +84 -0
  118. package/lib/esm/client/shared/components/PageHeader/ImagePreview.js +64 -0
  119. package/lib/esm/client/shared/components/PageHeader/PageHeader.js +120 -0
  120. package/lib/esm/client/shared/components/ResultsPage.js +143 -0
  121. package/lib/esm/client/shared/creeveyClientApi.js +98 -0
  122. package/lib/esm/client/shared/helpers.js +424 -0
  123. package/lib/esm/client/shared/viewMode.js +6 -0
  124. package/lib/esm/creevey.js +54 -0
  125. package/lib/esm/index.js +5 -0
  126. package/lib/esm/server/config.js +73 -0
  127. package/lib/esm/server/docker.js +123 -0
  128. package/lib/esm/server/extract.js +34 -0
  129. package/lib/esm/server/index.js +64 -0
  130. package/lib/esm/server/loaders/babel/creevey-plugin.js +72 -0
  131. package/lib/esm/server/loaders/babel/helpers.js +452 -0
  132. package/lib/esm/server/loaders/babel/register.js +103 -0
  133. package/lib/esm/server/loaders/hooks/mdx.js +15 -0
  134. package/lib/esm/server/loaders/hooks/svelte.js +49 -0
  135. package/lib/esm/server/loaders/webpack/compile.js +270 -0
  136. package/lib/esm/server/loaders/webpack/creevey-loader.js +158 -0
  137. package/lib/esm/server/loaders/webpack/dummy-hmr.js +32 -0
  138. package/lib/esm/server/loaders/webpack/mdx-loader.js +58 -0
  139. package/lib/esm/server/loaders/webpack/start.js +27 -0
  140. package/lib/esm/server/logger.js +20 -0
  141. package/lib/esm/server/master/api.js +60 -0
  142. package/lib/esm/server/master/index.js +125 -0
  143. package/lib/esm/server/master/master.js +38 -0
  144. package/lib/esm/server/master/pool.js +176 -0
  145. package/lib/esm/server/master/runner.js +259 -0
  146. package/lib/esm/server/master/server.js +105 -0
  147. package/lib/esm/server/messages.js +232 -0
  148. package/lib/esm/server/selenium/browser.js +639 -0
  149. package/lib/esm/server/selenium/index.js +2 -0
  150. package/lib/esm/server/selenium/selenoid.js +149 -0
  151. package/lib/esm/server/stories.js +140 -0
  152. package/lib/esm/server/storybook/entry.js +46 -0
  153. package/lib/esm/server/storybook/helpers.js +98 -0
  154. package/lib/esm/server/storybook/providers/browser.js +60 -0
  155. package/lib/esm/server/storybook/providers/hybrid.js +64 -0
  156. package/lib/esm/server/storybook/providers/nodejs.js +217 -0
  157. package/lib/esm/server/testsFiles/parser.js +50 -0
  158. package/lib/esm/server/testsFiles/register.js +35 -0
  159. package/lib/esm/server/update.js +61 -0
  160. package/lib/esm/server/utils.js +142 -0
  161. package/lib/esm/server/worker/chai-image.js +130 -0
  162. package/lib/esm/server/worker/helpers.js +60 -0
  163. package/lib/esm/server/worker/index.js +1 -0
  164. package/lib/esm/server/worker/reporter.js +86 -0
  165. package/lib/esm/server/worker/worker.js +238 -0
  166. package/lib/esm/shared/index.js +66 -0
  167. package/lib/esm/shared/serializeRegExp.js +23 -0
  168. package/lib/esm/types.js +43 -0
  169. package/lib/types/cli.d.ts +1 -1
  170. package/lib/types/client/addon/Manager.d.ts +37 -37
  171. package/lib/types/client/addon/components/Addon.d.ts +8 -8
  172. package/lib/types/client/addon/components/Icons.d.ts +7 -7
  173. package/lib/types/client/addon/components/Panel.d.ts +9 -9
  174. package/lib/types/client/addon/components/TestSelect.d.ts +8 -9
  175. package/lib/types/client/addon/components/Tools.d.ts +6 -6
  176. package/lib/types/client/addon/decorator.d.ts +1 -1
  177. package/lib/types/client/addon/index.d.ts +2 -0
  178. package/lib/types/client/addon/preset.d.ts +23 -24
  179. package/lib/types/client/addon/readyForCapture.d.ts +6 -6
  180. package/lib/types/client/addon/register.d.ts +3 -3
  181. package/lib/types/client/addon/utils.d.ts +2 -2
  182. package/lib/types/client/addon/withCreevey.d.ts +24 -24
  183. package/lib/types/client/shared/components/ImagesView/BlendView.d.ts +3 -3
  184. package/lib/types/client/shared/components/ImagesView/ImagesView.d.ts +24 -25
  185. package/lib/types/client/shared/components/ImagesView/SideBySideView.d.ts +3 -3
  186. package/lib/types/client/shared/components/ImagesView/SlideView.d.ts +3 -3
  187. package/lib/types/client/shared/components/ImagesView/SwapView.d.ts +3 -3
  188. package/lib/types/client/shared/components/ImagesView/index.d.ts +5 -5
  189. package/lib/types/client/shared/components/PageFooter/PageFooter.d.ts +8 -9
  190. package/lib/types/client/shared/components/PageFooter/Paging.d.ts +7 -8
  191. package/lib/types/client/shared/components/PageHeader/ImagePreview.d.ts +12 -12
  192. package/lib/types/client/shared/components/PageHeader/PageHeader.d.ts +16 -17
  193. package/lib/types/client/shared/components/ResultsPage.d.ts +18 -18
  194. package/lib/types/client/shared/creeveyClientApi.d.ts +9 -9
  195. package/lib/types/client/shared/helpers.d.ts +46 -46
  196. package/lib/types/client/shared/viewMode.d.ts +4 -4
  197. package/lib/types/client/web/CreeveyApp.d.ts +11 -12
  198. package/lib/types/client/web/CreeveyContext.d.ts +11 -11
  199. package/lib/types/client/web/CreeveyLoader.d.ts +2 -3
  200. package/lib/types/client/web/CreeveyView/SideBar/Checkbox.d.ts +19 -19
  201. package/lib/types/client/web/CreeveyView/SideBar/Search.d.ts +6 -6
  202. package/lib/types/client/web/CreeveyView/SideBar/SideBar.d.ts +14 -14
  203. package/lib/types/client/web/CreeveyView/SideBar/SideBarHeader.d.ts +12 -13
  204. package/lib/types/client/web/CreeveyView/SideBar/SuiteLink.d.ts +33 -33
  205. package/lib/types/client/web/CreeveyView/SideBar/TestLink.d.ts +7 -8
  206. package/lib/types/client/web/CreeveyView/SideBar/TestStatusIcon.d.ts +10 -10
  207. package/lib/types/client/web/CreeveyView/SideBar/TestsStatus.d.ts +9 -9
  208. package/lib/types/client/web/CreeveyView/SideBar/Toggle.d.ts +6 -6
  209. package/lib/types/client/web/CreeveyView/SideBar/index.d.ts +1 -1
  210. package/lib/types/client/web/KeyboardEventsContext.d.ts +13 -13
  211. package/lib/types/client/web/index.d.ts +4 -4
  212. package/lib/types/creevey.d.ts +1 -1
  213. package/lib/types/index.d.ts +1 -4
  214. package/lib/types/server/config.d.ts +4 -4
  215. package/lib/types/server/docker.d.ts +7 -7
  216. package/lib/types/server/extract.d.ts +2 -2
  217. package/lib/types/server/index.d.ts +2 -2
  218. package/lib/types/server/loaders/babel/creevey-plugin.d.ts +1 -1
  219. package/lib/types/server/loaders/babel/helpers.d.ts +19 -19
  220. package/lib/types/server/loaders/babel/register.d.ts +5 -5
  221. package/lib/types/server/loaders/hooks/mdx.d.ts +1 -1
  222. package/lib/types/server/loaders/hooks/svelte.d.ts +1 -1
  223. package/lib/types/server/loaders/webpack/compile.d.ts +2 -2
  224. package/lib/types/server/loaders/webpack/creevey-loader.d.ts +4 -2
  225. package/lib/types/server/loaders/webpack/dummy-hmr.d.ts +10 -10
  226. package/lib/types/server/loaders/webpack/mdx-loader.d.ts +6 -6
  227. package/lib/types/server/loaders/webpack/start.d.ts +1 -1
  228. package/lib/types/server/logger.d.ts +10 -6
  229. package/lib/types/server/master/api.d.ts +7 -7
  230. package/lib/types/server/master/index.d.ts +3 -3
  231. package/lib/types/server/master/master.d.ts +7 -7
  232. package/lib/types/server/master/pool.d.ts +31 -31
  233. package/lib/types/server/master/runner.d.ts +26 -26
  234. package/lib/types/server/master/server.d.ts +2 -2
  235. package/lib/types/server/messages.d.ts +27 -27
  236. package/lib/types/server/selenium/browser.d.ts +17 -17
  237. package/lib/types/server/selenium/index.d.ts +2 -2
  238. package/lib/types/server/selenium/selenoid.d.ts +3 -3
  239. package/lib/types/server/stories.d.ts +8 -8
  240. package/lib/types/server/storybook/entry.d.ts +18 -18
  241. package/lib/types/server/storybook/helpers.d.ts +24 -24
  242. package/lib/types/server/storybook/providers/browser.d.ts +4 -4
  243. package/lib/types/server/storybook/providers/hybrid.d.ts +4 -4
  244. package/lib/types/server/storybook/providers/nodejs.d.ts +9 -9
  245. package/lib/types/server/testsFiles/parser.d.ts +12 -12
  246. package/lib/types/server/testsFiles/register.d.ts +2 -2
  247. package/lib/types/server/update.d.ts +2 -2
  248. package/lib/types/server/utils.d.ts +20 -20
  249. package/lib/types/server/worker/chai-image.d.ts +6 -6
  250. package/lib/types/server/worker/helpers.d.ts +8 -8
  251. package/lib/types/server/worker/index.d.ts +1 -1
  252. package/lib/types/server/worker/reporter.d.ts +8 -8
  253. package/lib/types/server/worker/worker.d.ts +4 -4
  254. package/lib/types/{shared.d.ts → shared/index.d.ts} +7 -16
  255. package/lib/types/shared/serializeRegExp.d.ts +9 -0
  256. package/lib/types/types.d.ts +490 -489
  257. package/package.json +115 -102
  258. package/preset.js +9 -9
  259. package/types/babel__register.d.ts +1 -1
  260. package/types/chai.d.ts +12 -12
  261. package/types/event-source-polyfill.d.ts +6 -6
  262. package/types/mdx.d.ts +3 -2
  263. package/types/mocha.d.ts +20 -20
  264. package/types/png.d.ts +4 -4
  265. package/lib/cjs/client/web/1.js +0 -13
  266. package/lib/cjs/client/web/2.js +0 -1
  267. package/storybook-static/stories.json +0 -21
@@ -0,0 +1,217 @@
1
+ /* eslint-disable @typescript-eslint/no-var-requires, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-member-access,@typescript-eslint/ban-ts-comment */
2
+ import path from 'path';
3
+ import cluster from 'cluster';
4
+ import chokidar from 'chokidar';
5
+ import { noop } from '../../../types';
6
+ import { getCreeveyCache } from '../../utils';
7
+ import { subscribeOn } from '../../messages';
8
+ import { importStorybookClientLogger, importStorybookConfig, importStorybookCoreCommon, importStorybookCoreEvents, isStorybookVersionGreaterThan, isStorybookVersionLessThan } from '../helpers';
9
+ import { logger } from '../../logger';
10
+ import { denormalizeStoryParameters } from '../../../shared';
11
+
12
+ async function initStorybookEnvironment() {
13
+ // @ts-expect-error There is no @types/global-jsdom package
14
+ (await import('global-jsdom')).default(undefined, {
15
+ url: 'http://localhost'
16
+ }); // NOTE Cutoff `jsdom` part from userAgent, because storybook check enviroment and create events channel if runs in browser
17
+ // https://github.com/storybookjs/storybook/blob/v5.2.8/lib/core/src/client/preview/start.js#L98
18
+ // Example: "Mozilla/5.0 (linux) AppleWebKit/537.36 (KHTML, like Gecko) jsdom/15.2.1"
19
+
20
+ Object.defineProperty(window.navigator, 'userAgent', {
21
+ value: window.navigator.userAgent.split(' ').filter(token => !token.startsWith('jsdom')).join(' ')
22
+ }); // TODO Look at creevey debug flag
23
+
24
+ const {
25
+ logger
26
+ } = await importStorybookClientLogger(); // NOTE: Disable duplication warnings for >=6.2 storybook
27
+
28
+ if (cluster.isWorker) logger.warn = noop; // NOTE: disable logger for 5.x storybook
29
+
30
+ logger.debug = noop;
31
+ return import('../entry');
32
+ }
33
+
34
+ function watchStories(channel, watcher, initialFiles) {
35
+ const watchingFiles = initialFiles;
36
+ let storiesByFiles = new Map();
37
+ subscribeOn('shutdown', () => void watcher.close());
38
+ watcher.add(Array.from(watchingFiles));
39
+ watcher.on('change', filePath => storiesByFiles.set(path.isAbsolute(filePath) ? filePath : `./${filePath.replace(/\\/g, '/')}`, []));
40
+ watcher.on('unlink', filePath => storiesByFiles.set(path.isAbsolute(filePath) ? filePath : `./${filePath.replace(/\\/g, '/')}`, []));
41
+ return data => {
42
+ const stories = isStorybookVersionLessThan(6) || isStorybookVersionGreaterThan(6, 3) ? data.stories : denormalizeStoryParameters(data);
43
+ const files = new Set(Object.values(stories).map(story => story.parameters.fileName));
44
+ const addedFiles = Array.from(files).filter(filePath => !watchingFiles.has(filePath));
45
+ const removedFiles = Array.from(watchingFiles).filter(filePath => !files.has(filePath));
46
+ watcher.add(addedFiles);
47
+ addedFiles.forEach(filePath => {
48
+ watchingFiles.add(filePath);
49
+ storiesByFiles.set(filePath, []);
50
+ });
51
+ removedFiles.forEach(filePath => watchingFiles.delete(filePath));
52
+ Object.values(stories).forEach(story => {
53
+ var _storiesByFiles$get;
54
+
55
+ return (_storiesByFiles$get = storiesByFiles.get(story.parameters.fileName)) === null || _storiesByFiles$get === void 0 ? void 0 : _storiesByFiles$get.push(story);
56
+ });
57
+ channel.emit('storiesUpdated', storiesByFiles);
58
+ storiesByFiles = new Map();
59
+ };
60
+ }
61
+
62
+ function loadStoriesFromBundle(watch) {
63
+ const bundlePath = path.join(getCreeveyCache(), 'storybook/main.js');
64
+
65
+ if (watch) {
66
+ subscribeOn('webpack', message => {
67
+ if (message.type != 'rebuild succeeded') return;
68
+ Object.values(global.__CREEVEY_HMR_DATA__).filter(({
69
+ callback
70
+ }) => callback).forEach(({
71
+ data,
72
+ callback
73
+ }) => callback(data));
74
+ delete require.cache[bundlePath];
75
+ import(bundlePath);
76
+ });
77
+ }
78
+
79
+ import(bundlePath);
80
+ }
81
+
82
+ async function loadStoriesDirectly(config, {
83
+ watcher,
84
+ debug
85
+ }) {
86
+ const {
87
+ toRequireContext,
88
+ normalizeStoriesEntry
89
+ } = await importStorybookCoreCommon();
90
+ const {
91
+ addParameters,
92
+ configure
93
+ } = await import('../entry');
94
+ const requireContext = await (await import('../../loaders/babel/register')).default(config, debug);
95
+
96
+ const preview = (() => {
97
+ try {
98
+ return require.resolve(`${config.storybookDir}/preview`);
99
+ } catch (_) {
100
+ /* noop */
101
+ }
102
+ })();
103
+
104
+ const {
105
+ stories
106
+ } = await importStorybookConfig();
107
+ const contexts = stories.map(entry => {
108
+ const normalizedEntry = isStorybookVersionLessThan(6, 4) ? entry : normalizeStoriesEntry(entry, {
109
+ configDir: config.storybookDir,
110
+ workingDir: process.cwd()
111
+ });
112
+ const {
113
+ path: storiesPath,
114
+ recursive,
115
+ match
116
+ } = toRequireContext(normalizedEntry);
117
+ watcher === null || watcher === void 0 ? void 0 : watcher.add(path.resolve(config.storybookDir, storiesPath));
118
+ return () => requireContext(storiesPath, recursive, new RegExp(match));
119
+ });
120
+
121
+ let disposeCallback = data => void data;
122
+
123
+ Object.assign(module, {
124
+ hot: {
125
+ data: {},
126
+
127
+ accept() {
128
+ /* noop */
129
+ },
130
+
131
+ dispose(callback) {
132
+ disposeCallback = callback;
133
+ }
134
+
135
+ }
136
+ });
137
+
138
+ async function startStorybook() {
139
+ if (preview) {
140
+ const {
141
+ parameters,
142
+ globals,
143
+ globalTypes
144
+ } = await import(preview);
145
+ if (parameters) addParameters(parameters);
146
+ if (globals) addParameters({
147
+ globals
148
+ });
149
+ if (globalTypes) addParameters({
150
+ globalTypes
151
+ });
152
+ }
153
+
154
+ try {
155
+ configure(contexts.map(ctx => ctx()), module, false);
156
+ } catch (error) {
157
+ if (cluster.isPrimary) logger.error(error);
158
+ }
159
+ }
160
+
161
+ watcher === null || watcher === void 0 ? void 0 : watcher.add(config.storybookDir);
162
+ watcher === null || watcher === void 0 ? void 0 : watcher.on('all', (_event, filename) => {
163
+ var _module$hot;
164
+
165
+ disposeCallback((_module$hot = module.hot) === null || _module$hot === void 0 ? void 0 : _module$hot.data);
166
+ delete require.cache[filename];
167
+ void startStorybook();
168
+ });
169
+ void startStorybook();
170
+ } // TODO Do we need to support multiple storybooks here?
171
+
172
+
173
+ export async function loadStories(config, {
174
+ watch,
175
+ debug
176
+ }, storiesListener) {
177
+ const storybookApi = await initStorybookEnvironment();
178
+ const Events = await importStorybookCoreEvents();
179
+ const {
180
+ channel
181
+ } = storybookApi;
182
+ channel.removeAllListeners(Events.CURRENT_STORY_WAS_SET);
183
+ channel.on('storiesUpdated', storiesListener);
184
+ let watcher;
185
+ if (watch) watcher = chokidar.watch([], {
186
+ ignoreInitial: true
187
+ });
188
+ const loadPromise = new Promise(resolve => {
189
+ channel.once(Events.SET_STORIES, data => {
190
+ const stories = isStorybookVersionLessThan(6) || isStorybookVersionGreaterThan(6, 3) ? data.stories : denormalizeStoryParameters(data);
191
+ const files = new Set(Object.values(stories).map(story => story.parameters.fileName));
192
+ if (watcher) channel.on(Events.SET_STORIES, watchStories(channel, watcher, files));
193
+ resolve(stories);
194
+ });
195
+ });
196
+ if (config.useWebpackToExtractTests) loadStoriesFromBundle(watch);else void loadStoriesDirectly(config, {
197
+ watcher,
198
+ debug
199
+ });
200
+ return loadPromise;
201
+ }
202
+ export async function extractStoriesData(config, {
203
+ watch,
204
+ debug
205
+ }) {
206
+ const storybookApi = await initStorybookEnvironment();
207
+ const Events = await importStorybookCoreEvents();
208
+ const {
209
+ channel
210
+ } = storybookApi;
211
+ channel.removeAllListeners(Events.CURRENT_STORY_WAS_SET);
212
+ const loadPromise = new Promise(resolve => channel.once(Events.SET_STORIES, resolve));
213
+ if (config.useWebpackToExtractTests) loadStoriesFromBundle(watch);else void loadStoriesDirectly(config, {
214
+ debug
215
+ });
216
+ return loadPromise;
217
+ }
@@ -0,0 +1,50 @@
1
+ import { toId, storyNameFromExport } from '@storybook/csf';
2
+ export default async function parse(files) {
3
+ result = {};
4
+ await Promise.all(files.map(async file => import(file)));
5
+ return result;
6
+ }
7
+ let result = {};
8
+ let kindTitle = '';
9
+ let storyTitle = '';
10
+ let storyParams = null;
11
+
12
+ const setStoryParameters = params => {
13
+ storyParams = params;
14
+ };
15
+
16
+ const getStoryId = (kindTitle, storyTitle) => {
17
+ return toId(kindTitle, storyNameFromExport(storyTitle));
18
+ };
19
+
20
+ export const kind = (title, kindFn) => {
21
+ kindTitle = title;
22
+ kindFn();
23
+ kindTitle = '';
24
+ };
25
+ export const story = (title, storyFn) => {
26
+ var _result$storyId;
27
+
28
+ storyTitle = title;
29
+ storyParams = null;
30
+ storyFn({
31
+ setStoryParameters
32
+ });
33
+ const storyId = getStoryId(kindTitle, storyTitle);
34
+ result[storyId] = Object.assign({}, storyParams, {
35
+ tests: (_result$storyId = result[storyId]) === null || _result$storyId === void 0 ? void 0 : _result$storyId.tests
36
+ });
37
+ storyTitle = '';
38
+ storyParams = null;
39
+ };
40
+ export const test = (title, testFn) => {
41
+ const storyId = getStoryId(kindTitle, storyTitle);
42
+
43
+ if (!result[storyId]) {
44
+ result[storyId] = {};
45
+ }
46
+
47
+ result[storyId].tests = Object.assign({}, result[storyId].tests, {
48
+ [title]: testFn
49
+ });
50
+ };
@@ -0,0 +1,35 @@
1
+ import { addHook } from 'pirates';
2
+ import { getTsconfig } from 'get-tsconfig';
3
+ export default async function register(config) {
4
+ addHook(() => '', {
5
+ exts: ['.jpg', '.jpeg', '.png', '.gif', '.eot', '.otf', '.svg', '.ttf', '.woff', '.woff2', '.css', '.less', '.scss', '.styl'],
6
+ ignoreNodeModules: false
7
+ });
8
+ const {
9
+ path: tsConfigPath
10
+ } = getTsconfig(config.tsConfig) || {}; // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
11
+
12
+ (await import('@babel/register')).default(config.babelOptions({
13
+ babelrc: false,
14
+ rootMode: 'upward-optional',
15
+ ignore: [/node_modules/],
16
+ extensions: ['.js', '.jsx', '.ts', '.tsx'],
17
+ parserOpts: {
18
+ sourceType: 'module',
19
+ plugins: ['jsx', 'typescript']
20
+ },
21
+ presets: ['@babel/preset-typescript', ['@babel/preset-env', {
22
+ targets: {
23
+ node: '10'
24
+ },
25
+ modules: 'commonjs'
26
+ }]],
27
+ plugins: [['@babel/plugin-transform-runtime'], ['babel-plugin-tsconfig-paths', {
28
+ tsconfig: tsConfigPath
29
+ }]]
30
+ }));
31
+ (await import('ts-node')).register({
32
+ project: tsConfigPath,
33
+ transpileOnly: true
34
+ });
35
+ }
@@ -0,0 +1,61 @@
1
+ import path from 'path';
2
+ import fs, { mkdirSync } from 'fs';
3
+ import micromatch from 'micromatch';
4
+ import { isDefined } from '../types';
5
+
6
+ function tryToLoadTestsData(filename) {
7
+ try {
8
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
9
+ return require(filename);
10
+ } catch (_) {
11
+ /* noop */
12
+ }
13
+ }
14
+
15
+ const actualRegex = /^(.*)-actual-(\d+)\.png$/i;
16
+
17
+ function approve(dirents, srcPath, dstPath, testPaths, isMatch) {
18
+ dirents.filter(dirent => dirent.isFile()).map(dirent => actualRegex.exec(dirent.name)).filter(isDefined).filter(([fileName, imageName]) => !testPaths || testPaths.find(([token]) => token == imageName) && isMatch(path.join(srcPath, fileName))).reduce((images, [, imageName, retry]) => Number(retry) > (images.get(imageName) ?? -1) ? images.set(imageName, Number(retry)) : images, new Map()).forEach((retry, imageName) => {
19
+ mkdirSync(dstPath, {
20
+ recursive: true
21
+ });
22
+ fs.copyFileSync(path.join(srcPath, `${imageName}-actual-${retry}.png`), path.join(dstPath, `${imageName}.png`));
23
+ });
24
+ }
25
+
26
+ function traverse(srcPath, dstPath, testPaths, isMatch) {
27
+ const dirents = fs.readdirSync(srcPath, {
28
+ withFileTypes: true
29
+ });
30
+ approve(dirents, srcPath, dstPath, testPaths, isMatch);
31
+ dirents.filter(dirent => dirent.isDirectory()).map(dirent => [dirent.name, testPaths === null || testPaths === void 0 ? void 0 : testPaths.map(([token, ...restPath]) => token == dirent.name ? restPath : null).filter(isDefined)]).filter(([, paths]) => !paths || paths.length > 0).forEach(([dirname, paths]) => traverse(path.join(srcPath, dirname), path.join(dstPath, dirname), paths, isMatch));
32
+ }
33
+
34
+ export default function update(config, grepPattern) {
35
+ const {
36
+ reportDir,
37
+ screenDir
38
+ } = config;
39
+ const isMatch = grepPattern ? micromatch.matcher(grepPattern, {
40
+ contains: true
41
+ }) : () => true;
42
+ const testsMeta = tryToLoadTestsData(`${reportDir}/tests.json`);
43
+ const testsReport = tryToLoadTestsData(`${reportDir}/data`);
44
+ let testPaths = null;
45
+
46
+ if (testsMeta && testsReport) {
47
+ testPaths = Object.values(testsMeta).filter(isDefined).filter(({
48
+ id
49
+ }) => {
50
+ var _testsReport$id;
51
+
52
+ return ((_testsReport$id = testsReport[id]) === null || _testsReport$id === void 0 ? void 0 : _testsReport$id.status) == 'failed';
53
+ }).map(({
54
+ storyPath,
55
+ testName,
56
+ browser
57
+ }) => [...storyPath, ...(testName ? [testName] : []), browser]);
58
+ }
59
+
60
+ traverse(reportDir, screenDir, testPaths, value => isMatch(path.relative(reportDir, value)));
61
+ }
@@ -0,0 +1,142 @@
1
+ import { createWriteStream, existsSync, readFileSync, readdirSync, unlink } from 'fs';
2
+ import cluster from 'cluster';
3
+ import { isDefined, noop, isFunction } from '../types';
4
+ import { emitShutdownMessage, sendShutdownMessage } from './messages';
5
+ import findCacheDir from 'find-cache-dir';
6
+ import { get } from 'https';
7
+ export const isShuttingDown = {
8
+ current: false
9
+ };
10
+ export const LOCALHOST_REGEXP = /(localhost|127\.0\.0\.1)/i;
11
+ export const extensions = ['.js', '.jsx', '.ts', '.tsx'];
12
+ export const skipOptionKeys = ['in', 'kinds', 'stories', 'tests', 'reason'];
13
+
14
+ function matchBy(pattern, value) {
15
+ return typeof pattern == 'string' && pattern == value || Array.isArray(pattern) && pattern.includes(value) || pattern instanceof RegExp && pattern.test(value) || !isDefined(pattern);
16
+ }
17
+
18
+ export function shouldSkip(browser, meta, skipOptions, test) {
19
+ if (typeof skipOptions != 'object') {
20
+ return skipOptions;
21
+ }
22
+
23
+ if (Array.isArray(skipOptions)) {
24
+ for (const skip of skipOptions) {
25
+ const reason = shouldSkip(browser, meta, skip, test);
26
+ if (reason) return reason;
27
+ }
28
+
29
+ return false;
30
+ }
31
+
32
+ let hasSkipOptionKeys = false;
33
+
34
+ for (const skipKey in skipOptions) {
35
+ if (skipOptionKeys.includes(skipKey)) {
36
+ hasSkipOptionKeys = true;
37
+ continue;
38
+ }
39
+
40
+ const reason = shouldSkip(browser, meta, {
41
+ reason: skipKey,
42
+ ...skipOptions[skipKey]
43
+ }, test);
44
+ if (reason) return reason;
45
+ }
46
+
47
+ if (!hasSkipOptionKeys) return false;
48
+ const {
49
+ in: browsers,
50
+ kinds,
51
+ stories,
52
+ tests,
53
+ reason = true
54
+ } = skipOptions;
55
+ const {
56
+ kind,
57
+ story
58
+ } = meta;
59
+ const skipByBrowser = matchBy(browsers, browser);
60
+ const skipByKind = matchBy(kinds, kind);
61
+ const skipByStory = matchBy(stories, story);
62
+ const skipByTest = !isDefined(test) || matchBy(tests, test);
63
+ return skipByBrowser && skipByKind && skipByStory && skipByTest && reason;
64
+ }
65
+ export async function shutdownWorkers() {
66
+ isShuttingDown.current = true;
67
+ await Promise.all(Object.values(cluster.workers ?? {}).filter(isDefined).filter(worker => worker.isConnected()).map(worker => new Promise(resolve => {
68
+ const timeout = setTimeout(() => worker.kill(), 10000);
69
+ worker.on('exit', () => {
70
+ clearTimeout(timeout);
71
+ resolve();
72
+ });
73
+ sendShutdownMessage(worker);
74
+ })));
75
+ emitShutdownMessage();
76
+ }
77
+ export function shutdown() {
78
+ // eslint-disable-next-line no-process-exit
79
+ process.exit();
80
+ }
81
+ export function getCreeveyCache() {
82
+ return findCacheDir({
83
+ name: 'creevey',
84
+ cwd: __dirname
85
+ });
86
+ }
87
+ export async function runSequence(seq, predicate) {
88
+ for (const fn of seq) {
89
+ if (predicate()) await fn();
90
+ }
91
+ }
92
+ export function testsToImages(tests) {
93
+ return new Set([].concat(...tests.filter(isDefined).map(({
94
+ browser,
95
+ testName,
96
+ storyPath,
97
+ results
98
+ }) => {
99
+ var _results$slice$;
100
+
101
+ return Object.keys((results === null || results === void 0 ? void 0 : (_results$slice$ = results.slice(-1)[0]) === null || _results$slice$ === void 0 ? void 0 : _results$slice$.images) ?? {}).map(image => `${[...storyPath, testName, browser, browser == image ? undefined : image].filter(isDefined).join('/')}.png`);
102
+ })));
103
+ } // https://tuhrig.de/how-to-know-you-are-inside-a-docker-container/
104
+
105
+ export const isInsideDocker = existsSync('/proc/1/cgroup') && /docker/.test(readFileSync('/proc/1/cgroup', 'utf8'));
106
+ export const downloadBinary = (downloadUrl, destination) => new Promise((resolve, reject) => get(downloadUrl, response => {
107
+ if (response.statusCode == 302) {
108
+ const {
109
+ location
110
+ } = response.headers;
111
+ if (!location) return reject(new Error(`Couldn't download selenoid. Status code: ${response.statusCode ?? 'UNKNOWN'}`));
112
+ return resolve(downloadBinary(location, destination));
113
+ }
114
+
115
+ if (response.statusCode != 200) return reject(new Error(`Couldn't download selenoid. Status code: ${response.statusCode ?? 'UNKNOWN'}`));
116
+ const fileStream = createWriteStream(destination);
117
+ response.pipe(fileStream);
118
+ fileStream.on('finish', () => {
119
+ fileStream.close();
120
+ resolve();
121
+ });
122
+ fileStream.on('error', error => {
123
+ unlink(destination, noop);
124
+ reject(error);
125
+ });
126
+ }));
127
+ export function removeProps(obj, propPath) {
128
+ const [prop, ...restPath] = propPath;
129
+
130
+ if (restPath.length > 0) {
131
+ if (typeof prop == 'string') obj[prop] && removeProps(obj[prop], restPath);
132
+ if (isFunction(prop)) Object.keys(obj).filter(prop).forEach(key => obj[key] && removeProps(obj[key], restPath));
133
+ } else {
134
+ if (typeof prop == 'string') delete obj[prop];
135
+ if (isFunction(prop)) Object.keys(obj).filter(prop).forEach(key => delete obj[key]);
136
+ }
137
+ }
138
+ export function readDirRecursive(dirPath) {
139
+ return [].concat(...readdirSync(dirPath, {
140
+ withFileTypes: true
141
+ }).map(dirent => dirent.isDirectory() ? readDirRecursive(`${dirPath}/${dirent.name}`) : [`${dirPath}/${dirent.name}`]));
142
+ }
@@ -0,0 +1,130 @@
1
+ import { PNG } from 'pngjs';
2
+ import pixelmatch from 'pixelmatch';
3
+
4
+ function normalizeImageSize(image, width, height) {
5
+ const normalizedImage = Buffer.alloc(4 * width * height);
6
+
7
+ for (let y = 0; y < height; y++) {
8
+ for (let x = 0; x < width; x++) {
9
+ const i = (y * width + x) * 4;
10
+
11
+ if (x < image.width && y < image.height) {
12
+ const j = (y * image.width + x) * 4;
13
+ normalizedImage[i + 0] = image.data[j + 0];
14
+ normalizedImage[i + 1] = image.data[j + 1];
15
+ normalizedImage[i + 2] = image.data[j + 2];
16
+ normalizedImage[i + 3] = image.data[j + 3];
17
+ } else {
18
+ normalizedImage[i + 0] = 0;
19
+ normalizedImage[i + 1] = 0;
20
+ normalizedImage[i + 2] = 0;
21
+ normalizedImage[i + 3] = 0;
22
+ }
23
+ }
24
+ }
25
+
26
+ return normalizedImage;
27
+ }
28
+
29
+ function hasDiffPixels(diff) {
30
+ for (let i = 0; i < diff.length; i += 4) {
31
+ if (diff[i + 0] == 255 && diff[i + 1] == 0 && diff[i + 2] == 0 && diff[i + 3] == 255) return true;
32
+ }
33
+
34
+ return false;
35
+ }
36
+
37
+ function compareImages(expect, actual, diffOptions) {
38
+ const expectImage = PNG.sync.read(expect);
39
+ const actualImage = PNG.sync.read(actual);
40
+ const width = Math.max(actualImage.width, expectImage.width);
41
+ const height = Math.max(actualImage.height, expectImage.height);
42
+ const diffImage = new PNG({
43
+ width,
44
+ height
45
+ });
46
+ let actualImageData = actualImage.data;
47
+
48
+ if (actualImage.width < width || actualImage.height < height) {
49
+ actualImageData = normalizeImageSize(actualImage, width, height);
50
+ }
51
+
52
+ let expectImageData = expectImage.data;
53
+
54
+ if (expectImage.width < width || expectImage.height < height) {
55
+ expectImageData = normalizeImageSize(expectImage, width, height);
56
+ }
57
+
58
+ pixelmatch(expectImageData, actualImageData, diffImage.data, width, height, diffOptions);
59
+ return {
60
+ isEqual: !hasDiffPixels(diffImage.data),
61
+ diff: PNG.sync.write(diffImage)
62
+ };
63
+ }
64
+
65
+ export default function (getExpected, diffOptions) {
66
+ return function chaiImage({
67
+ Assertion
68
+ }, utils) {
69
+ async function assertImage(actual, imageName) {
70
+ let onCompare = () => Promise.resolve();
71
+
72
+ let expected = await getExpected(imageName);
73
+ if (!(expected instanceof Buffer) && expected != null) ({
74
+ expected,
75
+ onCompare
76
+ } = expected);
77
+
78
+ if (expected == null) {
79
+ await onCompare(actual);
80
+ return imageName ? `Expected image '${imageName}' does not exists` : 'Expected image does not exists';
81
+ }
82
+
83
+ if (actual.equals(expected)) return await onCompare(actual);
84
+ const {
85
+ isEqual,
86
+ diff
87
+ } = compareImages(expected, actual, diffOptions);
88
+ if (isEqual) return await onCompare(actual);
89
+ await onCompare(actual, expected, diff);
90
+ return imageName ? `Expected image '${imageName}' to match` : 'Expected image to match';
91
+ }
92
+
93
+ utils.addMethod(Assertion.prototype, 'matchImage', async function matchImage(imageName) {
94
+ const actual = utils.flag(this, 'object');
95
+ const errorMessage = await assertImage(typeof actual == 'string' ? Buffer.from(actual, 'base64') : actual, imageName);
96
+
97
+ if (errorMessage) {
98
+ throw createImageError(imageName ? {
99
+ [imageName]: errorMessage
100
+ } : errorMessage);
101
+ }
102
+ });
103
+ utils.addMethod(Assertion.prototype, 'matchImages', async function matchImages() {
104
+ const errors = {};
105
+ await Promise.all(Object.entries(utils.flag(this, 'object')).map(async ([imageName, imageOrBase64]) => {
106
+ let errorMessage;
107
+
108
+ try {
109
+ errorMessage = await assertImage(typeof imageOrBase64 == 'string' ? Buffer.from(imageOrBase64, 'base64') : imageOrBase64, imageName);
110
+ } catch (error) {
111
+ errorMessage = error.stack;
112
+ }
113
+
114
+ if (errorMessage) {
115
+ errors[imageName] = errorMessage;
116
+ }
117
+ }));
118
+
119
+ if (Object.keys(errors).length > 0) {
120
+ throw createImageError(errors);
121
+ }
122
+ });
123
+ };
124
+ }
125
+
126
+ function createImageError(imageErrors) {
127
+ const error = new Error('Expected image to match');
128
+ error.images = imageErrors;
129
+ return error;
130
+ }
@@ -0,0 +1,60 @@
1
+ import { Suite, Test } from 'mocha';
2
+ import { isDefined } from '../../types';
3
+ import { loadTestsFromStories } from '../stories';
4
+
5
+ function findOrCreateSuite(name, parent) {
6
+ const suite = parent.suites.find(({
7
+ title
8
+ }) => title == name) || new Suite(name, parent.ctx);
9
+
10
+ if (!suite.parent) {
11
+ suite.parent = parent;
12
+ parent.addSuite(suite);
13
+ }
14
+
15
+ return suite;
16
+ }
17
+
18
+ function createTest(name, fn, skip = false) {
19
+ const test = new Test(name, skip ? undefined : fn);
20
+ test.pending = Boolean(skip); // NOTE Can't define skip reason in mocha https://github.com/mochajs/mocha/issues/2026
21
+
22
+ test.skipReason = skip;
23
+ return test;
24
+ }
25
+
26
+ function addTest(rootSuite, test) {
27
+ const [testName, ...suitePath] = [...test.storyPath, test.testName].reverse().filter(isDefined);
28
+ const suite = suitePath.reduceRight((subSuite, suiteName) => findOrCreateSuite(suiteName, subSuite), rootSuite);
29
+ const mochaTest = createTest(testName, test.fn, test.skip);
30
+ suite.addTest(mochaTest);
31
+ mochaTest.ctx = Object.setPrototypeOf({
32
+ id: test.id,
33
+ story: test.story
34
+ }, suite.ctx);
35
+ return mochaTest;
36
+ }
37
+
38
+ function removeTestOrSuite(testOrSuite) {
39
+ const {
40
+ parent
41
+ } = testOrSuite;
42
+ if (!parent) return;
43
+ if (testOrSuite instanceof Test) parent.tests = parent.tests.filter(test => test != testOrSuite);
44
+ if (testOrSuite instanceof Suite) parent.suites = parent.suites.filter(suite => suite != testOrSuite);
45
+ if (parent.tests.length == 0 && parent.suites.length == 0) removeTestOrSuite(parent);
46
+ }
47
+
48
+ export async function addTestsFromStories(rootSuite, config, {
49
+ browser,
50
+ ...options
51
+ }) {
52
+ const mochaTestsById = new Map();
53
+ const tests = await loadTestsFromStories([browser], listener => config.storiesProvider(config, options, listener), testsDiff => Object.entries(testsDiff).forEach(([id, newTest]) => {
54
+ const oldTest = mochaTestsById.get(id);
55
+ mochaTestsById.delete(id);
56
+ if (oldTest) removeTestOrSuite(oldTest);
57
+ if (newTest) mochaTestsById.set(id, addTest(rootSuite, newTest));
58
+ }));
59
+ Object.values(tests).filter(isDefined).forEach(test => mochaTestsById.set(test.id, addTest(rootSuite, test)));
60
+ }