creevey 0.9.0-beta.1 → 0.9.0-beta.10

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 (221) hide show
  1. package/CHANGELOG.md +51 -0
  2. package/README.md +9 -1
  3. package/addon/README.md +3 -0
  4. package/addon/package.json +5 -0
  5. package/docs/config.md +29 -26
  6. package/jest.config.js +6 -0
  7. package/lib/cjs/cli.js +5 -0
  8. package/lib/cjs/client/addon/Manager.js +264 -0
  9. package/lib/cjs/client/addon/components/Addon.js +55 -0
  10. package/lib/cjs/client/addon/components/Icons.js +46 -0
  11. package/lib/cjs/client/addon/components/Panel.js +72 -0
  12. package/lib/cjs/client/addon/components/TestSelect.js +65 -0
  13. package/lib/cjs/client/addon/components/Tools.js +95 -0
  14. package/lib/cjs/client/addon/decorator.js +11 -0
  15. package/lib/cjs/client/addon/index.js +31 -0
  16. package/lib/cjs/client/addon/preset.ie11.js +74 -0
  17. package/lib/cjs/client/addon/preset.js +17 -0
  18. package/lib/cjs/client/addon/preset.sb7.js +19 -0
  19. package/lib/cjs/client/addon/preview.js +14 -0
  20. package/lib/cjs/client/addon/readyForCapture.js +12 -0
  21. package/lib/cjs/client/addon/register.js +72 -0
  22. package/lib/cjs/client/addon/utils.js +42 -0
  23. package/lib/cjs/client/addon/withCreevey.js +351 -0
  24. package/lib/cjs/client/shared/components/ImagesView/BlendView.js +87 -0
  25. package/lib/cjs/client/shared/components/ImagesView/ImagesView.js +92 -0
  26. package/lib/cjs/client/shared/components/ImagesView/SideBySideView.js +154 -0
  27. package/lib/cjs/client/shared/components/ImagesView/SlideView.js +166 -0
  28. package/lib/cjs/client/shared/components/ImagesView/SwapView.js +91 -0
  29. package/lib/cjs/client/shared/components/ImagesView/index.js +45 -0
  30. package/lib/cjs/client/shared/components/PageFooter/PageFooter.js +50 -0
  31. package/lib/cjs/client/shared/components/PageFooter/Paging.js +94 -0
  32. package/lib/cjs/client/shared/components/PageHeader/ImagePreview.js +82 -0
  33. package/lib/cjs/client/shared/components/PageHeader/PageHeader.js +119 -0
  34. package/lib/cjs/client/shared/components/ResultsPage.js +143 -0
  35. package/lib/cjs/client/shared/creeveyClientApi.js +76 -0
  36. package/lib/cjs/client/shared/helpers.js +411 -0
  37. package/lib/cjs/client/shared/viewMode.js +17 -0
  38. package/lib/cjs/client/web/142.js +2 -0
  39. package/lib/cjs/client/web/142.js.LICENSE.txt +12 -0
  40. package/lib/cjs/client/web/32.js +1 -0
  41. package/lib/cjs/client/web/551.js +1 -0
  42. package/lib/cjs/client/web/566.js +2 -0
  43. package/lib/cjs/client/web/566.js.LICENSE.txt +31 -0
  44. package/lib/cjs/client/web/691.js +2 -0
  45. package/lib/cjs/client/web/691.js.LICENSE.txt +8 -0
  46. package/lib/cjs/client/web/725.js +1 -0
  47. package/lib/cjs/client/web/index.html +19 -0
  48. package/lib/cjs/client/web/main.js +2 -38
  49. package/lib/cjs/client/web/main.js.LICENSE.txt +49 -0
  50. package/lib/cjs/creevey.js +69 -0
  51. package/lib/cjs/index.js +62 -0
  52. package/lib/cjs/server/config.js +94 -0
  53. package/lib/cjs/server/docker.js +146 -0
  54. package/lib/cjs/server/extract.js +46 -0
  55. package/lib/cjs/server/index.js +83 -0
  56. package/lib/cjs/server/loaders/babel/creevey-plugin.js +86 -0
  57. package/lib/cjs/server/loaders/babel/helpers.js +469 -0
  58. package/lib/cjs/server/loaders/babel/register.js +124 -0
  59. package/lib/cjs/server/loaders/hooks/mdx.js +30 -0
  60. package/lib/cjs/server/loaders/hooks/svelte.js +65 -0
  61. package/lib/cjs/server/loaders/webpack/compile.js +269 -0
  62. package/lib/cjs/server/loaders/webpack/creevey-loader.js +172 -0
  63. package/lib/cjs/server/loaders/webpack/dummy-hmr.js +39 -0
  64. package/lib/cjs/server/loaders/webpack/mdx-loader.js +72 -0
  65. package/lib/cjs/server/loaders/webpack/start.js +41 -0
  66. package/lib/cjs/server/logger.js +48 -0
  67. package/lib/cjs/server/master/api.js +71 -0
  68. package/lib/cjs/server/master/index.js +146 -0
  69. package/lib/cjs/server/master/master.js +57 -0
  70. package/lib/cjs/server/master/pool.js +197 -0
  71. package/lib/cjs/server/master/runner.js +281 -0
  72. package/lib/cjs/server/master/server.js +131 -0
  73. package/lib/cjs/server/messages.js +264 -0
  74. package/lib/cjs/server/selenium/browser.js +668 -0
  75. package/lib/cjs/server/selenium/index.js +31 -0
  76. package/lib/cjs/server/selenium/selenoid.js +172 -0
  77. package/lib/cjs/server/stories.js +153 -0
  78. package/lib/cjs/server/storybook/entry.js +53 -0
  79. package/lib/cjs/server/storybook/helpers.js +158 -0
  80. package/lib/cjs/server/storybook/providers/browser.js +74 -0
  81. package/lib/cjs/server/storybook/providers/hybrid.js +82 -0
  82. package/lib/cjs/server/storybook/providers/nodejs.js +239 -0
  83. package/lib/cjs/server/testsFiles/parser.js +72 -0
  84. package/lib/cjs/server/testsFiles/register.js +44 -0
  85. package/lib/cjs/server/update.js +79 -0
  86. package/lib/cjs/server/utils.js +176 -0
  87. package/lib/cjs/server/worker/chai-image.js +142 -0
  88. package/lib/cjs/server/worker/helpers.js +69 -0
  89. package/lib/cjs/server/worker/index.js +15 -0
  90. package/lib/cjs/server/worker/reporter.js +108 -0
  91. package/lib/cjs/server/worker/worker.js +268 -0
  92. package/lib/cjs/shared/index.js +101 -0
  93. package/lib/cjs/shared/serializeRegExp.js +42 -0
  94. package/lib/cjs/types.js +75 -0
  95. package/lib/esm/cli.js +4 -0
  96. package/lib/esm/client/addon/Manager.js +248 -0
  97. package/lib/esm/client/addon/components/Addon.js +39 -0
  98. package/lib/esm/client/addon/components/Icons.js +31 -0
  99. package/lib/esm/client/addon/components/Panel.js +53 -0
  100. package/lib/esm/client/addon/components/TestSelect.js +51 -0
  101. package/lib/esm/client/addon/components/Tools.js +74 -0
  102. package/lib/esm/client/addon/decorator.js +2 -0
  103. package/lib/esm/client/addon/index.js +2 -0
  104. package/lib/esm/client/addon/preset.ie11.js +59 -0
  105. package/lib/esm/client/addon/preset.js +8 -0
  106. package/lib/esm/client/addon/preset.sb7.js +8 -0
  107. package/lib/esm/client/addon/preview.js +5 -0
  108. package/lib/esm/client/addon/readyForCapture.js +5 -0
  109. package/lib/esm/client/addon/register.js +51 -0
  110. package/lib/esm/client/addon/utils.js +32 -0
  111. package/lib/esm/client/addon/withCreevey.js +325 -0
  112. package/lib/esm/client/shared/components/ImagesView/BlendView.js +67 -0
  113. package/lib/esm/client/shared/components/ImagesView/ImagesView.js +69 -0
  114. package/lib/esm/client/shared/components/ImagesView/SideBySideView.js +131 -0
  115. package/lib/esm/client/shared/components/ImagesView/SlideView.js +143 -0
  116. package/lib/esm/client/shared/components/ImagesView/SwapView.js +71 -0
  117. package/lib/esm/client/shared/components/ImagesView/index.js +5 -0
  118. package/lib/esm/client/shared/components/PageFooter/PageFooter.js +36 -0
  119. package/lib/esm/client/shared/components/PageFooter/Paging.js +80 -0
  120. package/lib/esm/client/shared/components/PageHeader/ImagePreview.js +68 -0
  121. package/lib/esm/client/shared/components/PageHeader/PageHeader.js +97 -0
  122. package/lib/esm/client/shared/components/ResultsPage.js +115 -0
  123. package/lib/esm/client/shared/creeveyClientApi.js +67 -0
  124. package/lib/esm/client/shared/helpers.js +353 -0
  125. package/lib/esm/client/shared/viewMode.js +6 -0
  126. package/lib/esm/creevey.js +54 -0
  127. package/lib/esm/index.js +5 -0
  128. package/lib/esm/server/config.js +71 -0
  129. package/lib/esm/server/docker.js +123 -0
  130. package/lib/esm/server/extract.js +32 -0
  131. package/lib/esm/server/index.js +64 -0
  132. package/lib/esm/server/loaders/babel/creevey-plugin.js +72 -0
  133. package/lib/esm/server/loaders/babel/helpers.js +452 -0
  134. package/lib/esm/server/loaders/babel/register.js +103 -0
  135. package/lib/esm/server/loaders/hooks/mdx.js +15 -0
  136. package/lib/esm/server/loaders/hooks/svelte.js +49 -0
  137. package/lib/esm/server/loaders/webpack/compile.js +246 -0
  138. package/lib/esm/server/loaders/webpack/creevey-loader.js +152 -0
  139. package/lib/esm/server/loaders/webpack/dummy-hmr.js +32 -0
  140. package/lib/esm/server/loaders/webpack/mdx-loader.js +58 -0
  141. package/lib/esm/server/loaders/webpack/start.js +27 -0
  142. package/lib/esm/server/logger.js +20 -0
  143. package/lib/esm/server/master/api.js +60 -0
  144. package/lib/esm/server/master/index.js +125 -0
  145. package/lib/esm/server/master/master.js +38 -0
  146. package/lib/esm/server/master/pool.js +176 -0
  147. package/lib/esm/server/master/runner.js +259 -0
  148. package/lib/esm/server/master/server.js +107 -0
  149. package/lib/esm/server/messages.js +232 -0
  150. package/lib/esm/server/selenium/browser.js +635 -0
  151. package/lib/esm/server/selenium/index.js +2 -0
  152. package/lib/esm/server/selenium/selenoid.js +149 -0
  153. package/lib/esm/server/stories.js +135 -0
  154. package/lib/esm/server/storybook/entry.js +27 -0
  155. package/lib/esm/server/storybook/helpers.js +97 -0
  156. package/lib/esm/server/storybook/providers/browser.js +58 -0
  157. package/lib/esm/server/storybook/providers/hybrid.js +60 -0
  158. package/lib/esm/server/storybook/providers/nodejs.js +216 -0
  159. package/lib/esm/server/testsFiles/parser.js +50 -0
  160. package/lib/esm/server/testsFiles/register.js +31 -0
  161. package/lib/esm/server/update.js +61 -0
  162. package/lib/esm/server/utils.js +133 -0
  163. package/lib/esm/server/worker/chai-image.js +130 -0
  164. package/lib/esm/server/worker/helpers.js +60 -0
  165. package/lib/esm/server/worker/index.js +1 -0
  166. package/lib/esm/server/worker/reporter.js +86 -0
  167. package/lib/esm/server/worker/worker.js +238 -0
  168. package/lib/esm/shared/index.js +78 -0
  169. package/lib/esm/shared/serializeRegExp.js +24 -0
  170. package/lib/esm/types.js +44 -0
  171. package/lib/types/client/addon/Manager.d.ts +2 -2
  172. package/lib/types/client/addon/components/TestSelect.d.ts +0 -1
  173. package/lib/types/client/addon/index.d.ts +2 -0
  174. package/lib/types/client/addon/preset.d.ts +0 -22
  175. package/lib/types/client/addon/preset.ie11.d.ts +10 -0
  176. package/lib/types/client/addon/preset.sb7.d.ts +2 -0
  177. package/lib/types/client/addon/preview.d.ts +4 -0
  178. package/lib/types/client/addon/utils.d.ts +1 -0
  179. package/lib/types/client/addon/withCreevey.d.ts +1 -1
  180. package/lib/types/client/shared/components/ImagesView/BlendView.d.ts +1 -1
  181. package/lib/types/client/shared/components/ImagesView/ImagesView.d.ts +0 -1
  182. package/lib/types/client/shared/components/ImagesView/SideBySideView.d.ts +1 -1
  183. package/lib/types/client/shared/components/ImagesView/SlideView.d.ts +1 -1
  184. package/lib/types/client/shared/components/ImagesView/SwapView.d.ts +1 -1
  185. package/lib/types/client/shared/components/PageFooter/PageFooter.d.ts +0 -1
  186. package/lib/types/client/shared/components/PageFooter/Paging.d.ts +0 -1
  187. package/lib/types/client/shared/components/PageHeader/ImagePreview.d.ts +1 -1
  188. package/lib/types/client/shared/components/PageHeader/PageHeader.d.ts +0 -1
  189. package/lib/types/client/shared/components/ResultsPage.d.ts +1 -1
  190. package/lib/types/client/web/CreeveyApp.d.ts +0 -1
  191. package/lib/types/client/web/CreeveyLoader.d.ts +1 -2
  192. package/lib/types/client/web/CreeveyView/SideBar/Checkbox.d.ts +1 -1
  193. package/lib/types/client/web/CreeveyView/SideBar/SideBarHeader.d.ts +0 -1
  194. package/lib/types/client/web/CreeveyView/SideBar/SuiteLink.d.ts +4 -4
  195. package/lib/types/client/web/CreeveyView/SideBar/TestLink.d.ts +0 -1
  196. package/lib/types/client/web/CreeveyView/SideBar/TestStatusIcon.d.ts +1 -1
  197. package/lib/types/client/web/CreeveyView/SideBar/TestsStatus.d.ts +1 -1
  198. package/lib/types/index.d.ts +1 -0
  199. package/lib/types/server/loaders/babel/register.d.ts +1 -1
  200. package/lib/types/server/loaders/webpack/creevey-loader.d.ts +4 -2
  201. package/lib/types/server/logger.d.ts +6 -2
  202. package/lib/types/server/messages.d.ts +14 -12
  203. package/lib/types/server/selenium/browser.d.ts +5 -3
  204. package/lib/types/server/storybook/entry.d.ts +2 -3
  205. package/lib/types/server/storybook/helpers.d.ts +1 -1
  206. package/lib/types/server/storybook/providers/browser.d.ts +2 -4
  207. package/lib/types/server/storybook/providers/hybrid.d.ts +2 -4
  208. package/lib/types/server/storybook/providers/nodejs.d.ts +3 -3
  209. package/lib/types/server/utils.d.ts +5 -1
  210. package/lib/types/{shared.d.ts → shared/index.d.ts} +1 -10
  211. package/lib/types/shared/serializeRegExp.d.ts +9 -0
  212. package/lib/types/types.d.ts +4 -7
  213. package/package.json +117 -103
  214. package/preset/ie11.js +5 -0
  215. package/{preset.js → preset/index.js} +2 -2
  216. package/preset/sb7.js +5 -0
  217. package/types/global.d.ts +5 -0
  218. package/types/mdx.d.ts +3 -2
  219. package/lib/cjs/client/web/1.js +0 -13
  220. package/lib/cjs/client/web/2.js +0 -1
  221. package/storybook-static/stories.json +0 -21
@@ -0,0 +1,216 @@
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 } from '../helpers';
9
+ import { logger } from '../../logger';
10
+
11
+ async function initStorybookEnvironment() {
12
+ // @ts-expect-error There is no @types/global-jsdom package
13
+ (await import('global-jsdom')).default(undefined, {
14
+ url: 'http://localhost'
15
+ }); // NOTE Cutoff `jsdom` part from userAgent, because storybook check enviroment and create events channel if runs in browser
16
+ // https://github.com/storybookjs/storybook/blob/v5.2.8/lib/core/src/client/preview/start.js#L98
17
+ // Example: "Mozilla/5.0 (linux) AppleWebKit/537.36 (KHTML, like Gecko) jsdom/15.2.1"
18
+
19
+ Object.defineProperty(window.navigator, 'userAgent', {
20
+ value: window.navigator.userAgent.split(' ').filter(token => !token.startsWith('jsdom')).join(' ')
21
+ }); // TODO Look at creevey debug flag
22
+
23
+ const {
24
+ logger
25
+ } = await importStorybookClientLogger(); // NOTE: Disable duplication warnings for >=6.2 storybook
26
+
27
+ if (cluster.isWorker) logger.warn = noop; // NOTE: disable logger for 5.x storybook
28
+
29
+ logger.debug = noop;
30
+ return import('../entry');
31
+ }
32
+
33
+ function watchStories(channel, watcher, initialFiles) {
34
+ const watchingFiles = initialFiles;
35
+ let storiesByFiles = new Map();
36
+ subscribeOn('shutdown', () => void watcher.close());
37
+ watcher.add(Array.from(watchingFiles));
38
+ watcher.on('change', filePath => storiesByFiles.set(path.isAbsolute(filePath) ? filePath : `./${filePath.replace(/\\/g, '/')}`, []));
39
+ watcher.on('unlink', filePath => storiesByFiles.set(path.isAbsolute(filePath) ? filePath : `./${filePath.replace(/\\/g, '/')}`, []));
40
+ return data => {
41
+ const stories = data.stories;
42
+ const files = new Set(Object.values(stories).map(story => story.parameters.fileName));
43
+ const addedFiles = Array.from(files).filter(filePath => !watchingFiles.has(filePath));
44
+ const removedFiles = Array.from(watchingFiles).filter(filePath => !files.has(filePath));
45
+ watcher.add(addedFiles);
46
+ addedFiles.forEach(filePath => {
47
+ watchingFiles.add(filePath);
48
+ storiesByFiles.set(filePath, []);
49
+ });
50
+ removedFiles.forEach(filePath => watchingFiles.delete(filePath));
51
+ Object.values(stories).forEach(story => {
52
+ var _storiesByFiles$get;
53
+
54
+ return (_storiesByFiles$get = storiesByFiles.get(story.parameters.fileName)) === null || _storiesByFiles$get === void 0 ? void 0 : _storiesByFiles$get.push(story);
55
+ });
56
+ channel.emit('storiesUpdated', storiesByFiles);
57
+ storiesByFiles = new Map();
58
+ };
59
+ }
60
+
61
+ function loadStoriesFromBundle(watch) {
62
+ const bundlePath = path.join(getCreeveyCache(), 'storybook/main.js');
63
+
64
+ if (watch) {
65
+ subscribeOn('webpack', message => {
66
+ if (message.type != 'rebuild succeeded') return;
67
+ Object.values(global.__CREEVEY_HMR_DATA__).filter(({
68
+ callback
69
+ }) => callback).forEach(({
70
+ data,
71
+ callback
72
+ }) => callback(data));
73
+ delete require.cache[bundlePath];
74
+ import(bundlePath);
75
+ });
76
+ }
77
+
78
+ import(bundlePath);
79
+ }
80
+
81
+ async function loadStoriesDirectly(config, {
82
+ watcher,
83
+ debug
84
+ }) {
85
+ const {
86
+ toRequireContext,
87
+ normalizeStoriesEntry
88
+ } = await importStorybookCoreCommon();
89
+ const {
90
+ addParameters,
91
+ configure
92
+ } = await import('../entry');
93
+ const requireContext = await (await import('../../loaders/babel/register')).default(config, debug);
94
+
95
+ const preview = (() => {
96
+ try {
97
+ return require.resolve(`${config.storybookDir}/preview`);
98
+ } catch (_) {
99
+ /* noop */
100
+ }
101
+ })();
102
+
103
+ const {
104
+ stories
105
+ } = await importStorybookConfig();
106
+ const contexts = stories.map(entry => {
107
+ const normalizedEntry = normalizeStoriesEntry(entry, {
108
+ configDir: config.storybookDir,
109
+ workingDir: process.cwd()
110
+ });
111
+ const {
112
+ path: storiesPath,
113
+ recursive,
114
+ match
115
+ } = toRequireContext(normalizedEntry);
116
+ watcher === null || watcher === void 0 ? void 0 : watcher.add(path.resolve(config.storybookDir, storiesPath));
117
+ return () => requireContext(storiesPath, recursive, new RegExp(match));
118
+ });
119
+
120
+ let disposeCallback = data => void data;
121
+
122
+ Object.assign(module, {
123
+ hot: {
124
+ data: {},
125
+
126
+ accept() {
127
+ /* noop */
128
+ },
129
+
130
+ dispose(callback) {
131
+ disposeCallback = callback;
132
+ }
133
+
134
+ }
135
+ });
136
+
137
+ async function startStorybook() {
138
+ if (preview) {
139
+ const {
140
+ parameters,
141
+ globals,
142
+ globalTypes
143
+ } = await import(preview);
144
+ if (parameters) addParameters(parameters);
145
+ if (globals) addParameters({
146
+ globals
147
+ });
148
+ if (globalTypes) addParameters({
149
+ globalTypes
150
+ });
151
+ }
152
+
153
+ try {
154
+ configure(contexts.map(ctx => ctx()), module, false);
155
+ } catch (error) {
156
+ if (cluster.isPrimary) logger.error(error);
157
+ }
158
+ }
159
+
160
+ watcher === null || watcher === void 0 ? void 0 : watcher.add(config.storybookDir);
161
+ watcher === null || watcher === void 0 ? void 0 : watcher.on('all', (_event, filename) => {
162
+ var _module$hot;
163
+
164
+ disposeCallback((_module$hot = module.hot) === null || _module$hot === void 0 ? void 0 : _module$hot.data);
165
+ delete require.cache[filename];
166
+ void startStorybook();
167
+ });
168
+ void startStorybook();
169
+ } // TODO Do we need to support multiple storybooks here?
170
+
171
+
172
+ export const loadStories = async (config, {
173
+ watch,
174
+ debug
175
+ }, storiesListener) => {
176
+ const storybookApi = await initStorybookEnvironment();
177
+ const Events = await importStorybookCoreEvents();
178
+ const {
179
+ channel
180
+ } = storybookApi;
181
+ channel.removeAllListeners(Events.CURRENT_STORY_WAS_SET);
182
+ channel.on('storiesUpdated', storiesListener);
183
+ let watcher;
184
+ if (watch) watcher = chokidar.watch([], {
185
+ ignoreInitial: true
186
+ });
187
+ const loadPromise = new Promise(resolve => {
188
+ channel.once(Events.SET_STORIES, data => {
189
+ const stories = data.stories;
190
+ const files = new Set(Object.values(stories).map(story => story.parameters.fileName));
191
+ if (watcher) channel.on(Events.SET_STORIES, watchStories(channel, watcher, files));
192
+ resolve(stories);
193
+ });
194
+ });
195
+ if (config.useWebpackToExtractTests) loadStoriesFromBundle(watch);else void loadStoriesDirectly(config, {
196
+ watcher,
197
+ debug
198
+ });
199
+ return loadPromise;
200
+ };
201
+ export async function extractStoriesData(config, {
202
+ watch,
203
+ debug
204
+ }) {
205
+ const storybookApi = await initStorybookEnvironment();
206
+ const Events = await importStorybookCoreEvents();
207
+ const {
208
+ channel
209
+ } = storybookApi;
210
+ channel.removeAllListeners(Events.CURRENT_STORY_WAS_SET);
211
+ const loadPromise = new Promise(resolve => channel.once(Events.SET_STORIES, resolve));
212
+ if (config.useWebpackToExtractTests) loadStoriesFromBundle(watch);else void loadStoriesDirectly(config, {
213
+ debug
214
+ });
215
+ return loadPromise;
216
+ }
@@ -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,31 @@
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'], ...(tsConfigPath ? [['babel-plugin-tsconfig-paths', {
28
+ tsconfig: tsConfigPath
29
+ }]] : [])]
30
+ })); // (await import('ts-node')).register({ project: tsConfigPath, transpileOnly: true });
31
+ }
@@ -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,133 @@
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
+ for (const skipKey in skipOptions) {
24
+ const reason = shouldSkipByOption(browser, meta, skipOptions[skipKey], skipKey, test);
25
+ if (reason) return reason;
26
+ }
27
+
28
+ return false;
29
+ }
30
+ export function shouldSkipByOption(browser, meta, skipOption, reason, test) {
31
+ if (Array.isArray(skipOption)) {
32
+ for (const skip of skipOption) {
33
+ const result = shouldSkipByOption(browser, meta, skip, reason, test);
34
+ if (result) return result;
35
+ }
36
+
37
+ return false;
38
+ }
39
+
40
+ const {
41
+ in: browsers,
42
+ kinds,
43
+ stories,
44
+ tests
45
+ } = skipOption;
46
+ const {
47
+ kind,
48
+ story
49
+ } = meta;
50
+ const skipByBrowser = matchBy(browsers, browser);
51
+ const skipByKind = matchBy(kinds, kind);
52
+ const skipByStory = matchBy(stories, story);
53
+ const skipByTest = !isDefined(test) || matchBy(tests, test);
54
+ return skipByBrowser && skipByKind && skipByStory && skipByTest && reason;
55
+ }
56
+ export async function shutdownWorkers() {
57
+ isShuttingDown.current = true;
58
+ await Promise.all(Object.values(cluster.workers ?? {}).filter(isDefined).filter(worker => worker.isConnected()).map(worker => new Promise(resolve => {
59
+ const timeout = setTimeout(() => worker.kill(), 10000);
60
+ worker.on('exit', () => {
61
+ clearTimeout(timeout);
62
+ resolve();
63
+ });
64
+ sendShutdownMessage(worker);
65
+ })));
66
+ emitShutdownMessage();
67
+ }
68
+ export function shutdown() {
69
+ // eslint-disable-next-line no-process-exit
70
+ process.exit();
71
+ }
72
+ export function getCreeveyCache() {
73
+ return findCacheDir({
74
+ name: 'creevey',
75
+ cwd: __dirname
76
+ });
77
+ }
78
+ export async function runSequence(seq, predicate) {
79
+ for (const fn of seq) {
80
+ if (predicate()) await fn();
81
+ }
82
+ }
83
+ export function testsToImages(tests) {
84
+ return new Set([].concat(...tests.filter(isDefined).map(({
85
+ browser,
86
+ testName,
87
+ storyPath,
88
+ results
89
+ }) => {
90
+ var _results$slice$;
91
+
92
+ 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`);
93
+ })));
94
+ } // https://tuhrig.de/how-to-know-you-are-inside-a-docker-container/
95
+
96
+ export const isInsideDocker = existsSync('/proc/1/cgroup') && /docker/.test(readFileSync('/proc/1/cgroup', 'utf8'));
97
+ export const downloadBinary = (downloadUrl, destination) => new Promise((resolve, reject) => get(downloadUrl, response => {
98
+ if (response.statusCode == 302) {
99
+ const {
100
+ location
101
+ } = response.headers;
102
+ if (!location) return reject(new Error(`Couldn't download selenoid. Status code: ${response.statusCode ?? 'UNKNOWN'}`));
103
+ return resolve(downloadBinary(location, destination));
104
+ }
105
+
106
+ if (response.statusCode != 200) return reject(new Error(`Couldn't download selenoid. Status code: ${response.statusCode ?? 'UNKNOWN'}`));
107
+ const fileStream = createWriteStream(destination);
108
+ response.pipe(fileStream);
109
+ fileStream.on('finish', () => {
110
+ fileStream.close();
111
+ resolve();
112
+ });
113
+ fileStream.on('error', error => {
114
+ unlink(destination, noop);
115
+ reject(error);
116
+ });
117
+ }));
118
+ export function removeProps(obj, propPath) {
119
+ const [prop, ...restPath] = propPath;
120
+
121
+ if (restPath.length > 0) {
122
+ if (typeof prop == 'string') obj[prop] && removeProps(obj[prop], restPath);
123
+ if (isFunction(prop)) Object.keys(obj).filter(prop).forEach(key => obj[key] && removeProps(obj[key], restPath));
124
+ } else {
125
+ if (typeof prop == 'string') delete obj[prop];
126
+ if (isFunction(prop)) Object.keys(obj).filter(prop).forEach(key => delete obj[key]);
127
+ }
128
+ }
129
+ export function readDirRecursive(dirPath) {
130
+ return [].concat(...readdirSync(dirPath, {
131
+ withFileTypes: true
132
+ }).map(dirent => dirent.isDirectory() ? readDirRecursive(`${dirPath}/${dirent.name}`) : [`${dirPath}/${dirent.name}`]));
133
+ }
@@ -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
+ }
@@ -0,0 +1 @@
1
+ export { default } from './worker';