@dr.pogodin/react-utils 1.47.0-alpha.1 → 1.47.0-alpha.11

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 (306) hide show
  1. package/babel.config.js +1 -1
  2. package/babel.module.config.js +11 -0
  3. package/bin/build.js +8 -4
  4. package/bin/setup.js +2 -3
  5. package/build/development/client/getInj.js +57 -44
  6. package/build/development/client/getInj.js.map +1 -1
  7. package/build/development/client/index.js +15 -21
  8. package/build/development/client/index.js.map +1 -1
  9. package/build/development/client/init.js +2 -6
  10. package/build/development/client/init.js.map +1 -1
  11. package/build/development/index.js +3 -183
  12. package/build/development/index.js.map +1 -1
  13. package/build/development/server/Cache.js +1 -8
  14. package/build/development/server/Cache.js.map +1 -1
  15. package/build/development/server/index.js +22 -50
  16. package/build/development/server/index.js.map +1 -1
  17. package/build/development/server/renderer.js +83 -92
  18. package/build/development/server/renderer.js.map +1 -1
  19. package/build/development/server/server.js +58 -62
  20. package/build/development/server/server.js.map +1 -1
  21. package/build/development/server/utils/errors.js +8 -31
  22. package/build/development/server/utils/errors.js.map +1 -1
  23. package/build/development/server/utils/index.js +2 -9
  24. package/build/development/server/utils/index.js.map +1 -1
  25. package/build/development/shared/components/Button/index.js +9 -16
  26. package/build/development/shared/components/Button/index.js.map +1 -1
  27. package/build/development/shared/components/Checkbox/index.js +6 -13
  28. package/build/development/shared/components/Checkbox/index.js.map +1 -1
  29. package/build/development/shared/components/GenericLink/index.js +4 -10
  30. package/build/development/shared/components/GenericLink/index.js.map +1 -1
  31. package/build/development/shared/components/Input/index.js +11 -18
  32. package/build/development/shared/components/Input/index.js.map +1 -1
  33. package/build/development/shared/components/Link.js +6 -13
  34. package/build/development/shared/components/Link.js.map +1 -1
  35. package/build/development/shared/components/Modal/index.js +16 -22
  36. package/build/development/shared/components/Modal/index.js.map +1 -1
  37. package/build/development/shared/components/NavLink.js +6 -13
  38. package/build/development/shared/components/NavLink.js.map +1 -1
  39. package/build/development/shared/components/PageLayout/index.js +7 -14
  40. package/build/development/shared/components/PageLayout/index.js.map +1 -1
  41. package/build/development/shared/components/TextArea/index.js +15 -22
  42. package/build/development/shared/components/TextArea/index.js.map +1 -1
  43. package/build/development/shared/components/Throbber/index.js +7 -14
  44. package/build/development/shared/components/Throbber/index.js.map +1 -1
  45. package/build/development/shared/components/WithTooltip/Tooltip.js +12 -18
  46. package/build/development/shared/components/WithTooltip/Tooltip.js.map +1 -1
  47. package/build/development/shared/components/WithTooltip/index.js +16 -23
  48. package/build/development/shared/components/WithTooltip/index.js.map +1 -1
  49. package/build/development/shared/components/YouTubeVideo/index.js +10 -17
  50. package/build/development/shared/components/YouTubeVideo/index.js.map +1 -1
  51. package/build/development/shared/components/index.js +16 -130
  52. package/build/development/shared/components/index.js.map +1 -1
  53. package/build/development/shared/components/selectors/CustomDropdown/Options/index.js +18 -19
  54. package/build/development/shared/components/selectors/CustomDropdown/Options/index.js.map +1 -1
  55. package/build/development/shared/components/selectors/CustomDropdown/index.js +24 -28
  56. package/build/development/shared/components/selectors/CustomDropdown/index.js.map +1 -1
  57. package/build/development/shared/components/selectors/NativeDropdown/index.js +19 -19
  58. package/build/development/shared/components/selectors/NativeDropdown/index.js.map +1 -1
  59. package/build/development/shared/components/selectors/Switch/index.js +12 -17
  60. package/build/development/shared/components/selectors/Switch/index.js.map +1 -1
  61. package/build/development/shared/components/selectors/common.js +1 -7
  62. package/build/development/shared/components/selectors/common.js.map +1 -1
  63. package/build/development/shared/components/selectors/index.js +3 -27
  64. package/build/development/shared/components/selectors/index.js.map +1 -1
  65. package/build/development/shared/utils/config.js +40 -21
  66. package/build/development/shared/utils/config.js.map +1 -1
  67. package/build/development/shared/utils/globalState.js +3 -8
  68. package/build/development/shared/utils/globalState.js.map +1 -1
  69. package/build/development/shared/utils/index.js +12 -89
  70. package/build/development/shared/utils/index.js.map +1 -1
  71. package/build/development/shared/utils/isomorphy/buildInfo.js +2 -9
  72. package/build/development/shared/utils/isomorphy/buildInfo.js.map +1 -1
  73. package/build/development/shared/utils/isomorphy/environment-check.js +2 -8
  74. package/build/development/shared/utils/isomorphy/environment-check.js.map +1 -1
  75. package/build/development/shared/utils/isomorphy/index.js +7 -32
  76. package/build/development/shared/utils/isomorphy/index.js.map +1 -1
  77. package/build/development/shared/utils/jest/E2eSsrEnv.js +24 -31
  78. package/build/development/shared/utils/jest/E2eSsrEnv.js.map +1 -1
  79. package/build/development/shared/utils/jest/global.js +1 -7
  80. package/build/development/shared/utils/jest/global.js.map +1 -1
  81. package/build/development/shared/utils/jest/index.js +20 -44
  82. package/build/development/shared/utils/jest/index.js.map +1 -1
  83. package/build/development/shared/utils/splitComponent.js +54 -46
  84. package/build/development/shared/utils/splitComponent.js.map +1 -1
  85. package/build/development/shared/utils/time.js +22 -31
  86. package/build/development/shared/utils/time.js.map +1 -1
  87. package/build/development/shared/utils/webpack.js +17 -16
  88. package/build/development/shared/utils/webpack.js.map +1 -1
  89. package/build/production/client/getInj.js +13 -11
  90. package/build/production/client/getInj.js.map +1 -1
  91. package/build/production/client/index.js +3 -3
  92. package/build/production/client/index.js.map +1 -1
  93. package/build/production/client/init.js +2 -2
  94. package/build/production/client/init.js.map +1 -1
  95. package/build/production/index.js +2 -6
  96. package/build/production/index.js.map +1 -1
  97. package/build/production/server/Cache.js +3 -3
  98. package/build/production/server/Cache.js.map +1 -1
  99. package/build/production/server/index.js +8 -8
  100. package/build/production/server/index.js.map +1 -1
  101. package/build/production/server/renderer.js +24 -25
  102. package/build/production/server/renderer.js.map +1 -1
  103. package/build/production/server/server.js +9 -11
  104. package/build/production/server/server.js.map +1 -1
  105. package/build/production/server/utils/errors.js +9 -9
  106. package/build/production/server/utils/errors.js.map +1 -1
  107. package/build/production/server/utils/index.js +2 -1
  108. package/build/production/server/utils/index.js.map +1 -1
  109. package/build/production/shared/components/Button/index.js +4 -4
  110. package/build/production/shared/components/Button/index.js.map +1 -1
  111. package/build/production/shared/components/Checkbox/index.js +1 -1
  112. package/build/production/shared/components/Checkbox/index.js.map +1 -1
  113. package/build/production/shared/components/GenericLink/index.js +4 -4
  114. package/build/production/shared/components/GenericLink/index.js.map +1 -1
  115. package/build/production/shared/components/Input/index.js +5 -5
  116. package/build/production/shared/components/Input/index.js.map +1 -1
  117. package/build/production/shared/components/Link.js +3 -3
  118. package/build/production/shared/components/Link.js.map +1 -1
  119. package/build/production/shared/components/Modal/index.js +7 -7
  120. package/build/production/shared/components/Modal/index.js.map +1 -1
  121. package/build/production/shared/components/NavLink.js +2 -2
  122. package/build/production/shared/components/NavLink.js.map +1 -1
  123. package/build/production/shared/components/PageLayout/index.js +2 -2
  124. package/build/production/shared/components/PageLayout/index.js.map +1 -1
  125. package/build/production/shared/components/TextArea/index.js +5 -5
  126. package/build/production/shared/components/TextArea/index.js.map +1 -1
  127. package/build/production/shared/components/Throbber/index.js +2 -2
  128. package/build/production/shared/components/Throbber/index.js.map +1 -1
  129. package/build/production/shared/components/WithTooltip/Tooltip.js +4 -4
  130. package/build/production/shared/components/WithTooltip/Tooltip.js.map +1 -1
  131. package/build/production/shared/components/WithTooltip/index.js +4 -4
  132. package/build/production/shared/components/WithTooltip/index.js.map +1 -1
  133. package/build/production/shared/components/YouTubeVideo/index.js +4 -4
  134. package/build/production/shared/components/YouTubeVideo/index.js.map +1 -1
  135. package/build/production/shared/components/index.js +3 -1
  136. package/build/production/shared/components/index.js.map +1 -1
  137. package/build/production/shared/components/selectors/CustomDropdown/Options/index.js +2 -2
  138. package/build/production/shared/components/selectors/CustomDropdown/Options/index.js.map +1 -1
  139. package/build/production/shared/components/selectors/CustomDropdown/index.js +2 -2
  140. package/build/production/shared/components/selectors/CustomDropdown/index.js.map +1 -1
  141. package/build/production/shared/components/selectors/NativeDropdown/index.js +4 -4
  142. package/build/production/shared/components/selectors/NativeDropdown/index.js.map +1 -1
  143. package/build/production/shared/components/selectors/Switch/index.js +1 -1
  144. package/build/production/shared/components/selectors/Switch/index.js.map +1 -1
  145. package/build/production/shared/components/selectors/common.js +2 -2
  146. package/build/production/shared/components/selectors/common.js.map +1 -1
  147. package/build/production/shared/components/selectors/index.js +1 -1
  148. package/build/production/shared/components/selectors/index.js.map +1 -1
  149. package/build/production/shared/utils/config.js +8 -6
  150. package/build/production/shared/utils/config.js.map +1 -1
  151. package/build/production/shared/utils/globalState.js +2 -2
  152. package/build/production/shared/utils/globalState.js.map +1 -1
  153. package/build/production/shared/utils/index.js +1 -1
  154. package/build/production/shared/utils/index.js.map +1 -1
  155. package/build/production/shared/utils/isomorphy/buildInfo.js +3 -3
  156. package/build/production/shared/utils/isomorphy/buildInfo.js.map +1 -1
  157. package/build/production/shared/utils/isomorphy/environment-check.js +3 -3
  158. package/build/production/shared/utils/isomorphy/environment-check.js.map +1 -1
  159. package/build/production/shared/utils/isomorphy/index.js +4 -4
  160. package/build/production/shared/utils/isomorphy/index.js.map +1 -1
  161. package/build/production/shared/utils/jest/E2eSsrEnv.js +14 -14
  162. package/build/production/shared/utils/jest/E2eSsrEnv.js.map +1 -1
  163. package/build/production/shared/utils/jest/global.js +1 -1
  164. package/build/production/shared/utils/jest/global.js.map +1 -1
  165. package/build/production/shared/utils/jest/index.js +11 -11
  166. package/build/production/shared/utils/jest/index.js.map +1 -1
  167. package/build/production/shared/utils/splitComponent.js +11 -22
  168. package/build/production/shared/utils/splitComponent.js.map +1 -1
  169. package/build/production/shared/utils/time.js +3 -3
  170. package/build/production/shared/utils/time.js.map +1 -1
  171. package/build/production/shared/utils/webpack.js +6 -6
  172. package/build/production/shared/utils/webpack.js.map +1 -1
  173. package/build/types-code/client/getInj.d.ts +1 -2
  174. package/build/types-code/client/index.d.ts +1 -1
  175. package/build/types-code/index.d.ts +1 -7
  176. package/build/types-code/server/index.d.ts +6 -11
  177. package/build/types-code/server/utils/index.d.ts +1 -1
  178. package/build/types-code/shared/utils/config.d.ts +4 -2
  179. package/build/types-code/shared/utils/index.d.ts +2 -2
  180. package/build/types-code/shared/utils/splitComponent.d.ts +6 -4
  181. package/build/web/client/getInj.js +55 -44
  182. package/build/web/client/getInj.js.map +1 -1
  183. package/build/web/client/index.js +15 -21
  184. package/build/web/client/index.js.map +1 -1
  185. package/build/web/client/init.js +2 -4
  186. package/build/web/client/init.js.map +1 -1
  187. package/build/web/index.js +1 -15
  188. package/build/web/index.js.map +1 -1
  189. package/build/web/server/index.js +12 -21
  190. package/build/web/server/index.js.map +1 -1
  191. package/build/web/server/renderer.js +34 -40
  192. package/build/web/server/renderer.js.map +1 -1
  193. package/build/web/server/server.js +14 -11
  194. package/build/web/server/server.js.map +1 -1
  195. package/build/web/server/utils/index.js +1 -1
  196. package/build/web/server/utils/index.js.map +1 -1
  197. package/build/web/shared/components/Button/index.js +1 -1
  198. package/build/web/shared/components/Button/index.js.map +1 -1
  199. package/build/web/shared/components/Checkbox/index.js +1 -1
  200. package/build/web/shared/components/Checkbox/index.js.map +1 -1
  201. package/build/web/shared/components/Input/index.js +1 -1
  202. package/build/web/shared/components/Input/index.js.map +1 -1
  203. package/build/web/shared/components/Modal/index.js +3 -3
  204. package/build/web/shared/components/Modal/index.js.map +1 -1
  205. package/build/web/shared/components/PageLayout/index.js +1 -1
  206. package/build/web/shared/components/PageLayout/index.js.map +1 -1
  207. package/build/web/shared/components/TextArea/index.js +1 -1
  208. package/build/web/shared/components/TextArea/index.js.map +1 -1
  209. package/build/web/shared/components/Throbber/index.js +1 -1
  210. package/build/web/shared/components/Throbber/index.js.map +1 -1
  211. package/build/web/shared/components/WithTooltip/index.js +1 -1
  212. package/build/web/shared/components/WithTooltip/index.js.map +1 -1
  213. package/build/web/shared/components/YouTubeVideo/index.js +1 -1
  214. package/build/web/shared/components/YouTubeVideo/index.js.map +1 -1
  215. package/build/web/shared/components/selectors/CustomDropdown/Options/index.js +3 -0
  216. package/build/web/shared/components/selectors/CustomDropdown/Options/index.js.map +1 -1
  217. package/build/web/shared/components/selectors/CustomDropdown/index.js +3 -1
  218. package/build/web/shared/components/selectors/CustomDropdown/index.js.map +1 -1
  219. package/build/web/shared/components/selectors/NativeDropdown/index.js +4 -1
  220. package/build/web/shared/components/selectors/NativeDropdown/index.js.map +1 -1
  221. package/build/web/shared/components/selectors/Switch/index.js +2 -1
  222. package/build/web/shared/components/selectors/Switch/index.js.map +1 -1
  223. package/build/web/shared/utils/config.js +38 -14
  224. package/build/web/shared/utils/config.js.map +1 -1
  225. package/build/web/shared/utils/index.js +2 -2
  226. package/build/web/shared/utils/index.js.map +1 -1
  227. package/build/web/shared/utils/jest/E2eSsrEnv.js +3 -2
  228. package/build/web/shared/utils/jest/E2eSsrEnv.js.map +1 -1
  229. package/build/web/shared/utils/splitComponent.js +27 -20
  230. package/build/web/shared/utils/splitComponent.js.map +1 -1
  231. package/build/web/shared/utils/webpack.js +13 -7
  232. package/build/web/shared/utils/webpack.js.map +1 -1
  233. package/config/babel/node-ssr.js +12 -6
  234. package/config/babel/webpack.js +6 -1
  235. package/config/jest/default.js +8 -2
  236. package/config/jest/setup.js +8 -1
  237. package/config/webpack/app-base.js +23 -15
  238. package/config/webpack/app-development.js +2 -3
  239. package/config/webpack/lib-base.js +1 -2
  240. package/package.json +35 -25
  241. package/types.d.ts +1 -6
  242. package/node-entry.js +0 -15
  243. package/src/client/getInj.ts +0 -51
  244. package/src/client/index.tsx +0 -43
  245. package/src/client/init.ts +0 -52
  246. package/src/index.ts +0 -79
  247. package/src/server/Cache.ts +0 -61
  248. package/src/server/index.ts +0 -249
  249. package/src/server/renderer.tsx +0 -653
  250. package/src/server/server.ts +0 -332
  251. package/src/server/utils/errors.ts +0 -122
  252. package/src/server/utils/index.ts +0 -3
  253. package/src/shared/components/Button/index.tsx +0 -130
  254. package/src/shared/components/Button/style.scss +0 -54
  255. package/src/shared/components/Checkbox/index.tsx +0 -53
  256. package/src/shared/components/Checkbox/theme.scss +0 -63
  257. package/src/shared/components/GenericLink/index.tsx +0 -141
  258. package/src/shared/components/GenericLink/style.scss +0 -3
  259. package/src/shared/components/Input/index.tsx +0 -96
  260. package/src/shared/components/Input/theme.scss +0 -52
  261. package/src/shared/components/Link.tsx +0 -25
  262. package/src/shared/components/Modal/base-theme.scss +0 -38
  263. package/src/shared/components/Modal/index.tsx +0 -177
  264. package/src/shared/components/Modal/styles.scss +0 -3
  265. package/src/shared/components/NavLink.tsx +0 -18
  266. package/src/shared/components/PageLayout/base-theme.scss +0 -30
  267. package/src/shared/components/PageLayout/index.tsx +0 -51
  268. package/src/shared/components/TextArea/index.tsx +0 -139
  269. package/src/shared/components/TextArea/style.scss +0 -68
  270. package/src/shared/components/Throbber/index.tsx +0 -28
  271. package/src/shared/components/Throbber/theme.scss +0 -26
  272. package/src/shared/components/WithTooltip/Tooltip.tsx +0 -274
  273. package/src/shared/components/WithTooltip/default-theme.scss +0 -36
  274. package/src/shared/components/WithTooltip/index.tsx +0 -186
  275. package/src/shared/components/YouTubeVideo/base.scss +0 -15
  276. package/src/shared/components/YouTubeVideo/index.tsx +0 -73
  277. package/src/shared/components/YouTubeVideo/throbber.scss +0 -11
  278. package/src/shared/components/index.ts +0 -20
  279. package/src/shared/components/selectors/CustomDropdown/Options/index.tsx +0 -124
  280. package/src/shared/components/selectors/CustomDropdown/Options/style.scss +0 -6
  281. package/src/shared/components/selectors/CustomDropdown/index.tsx +0 -146
  282. package/src/shared/components/selectors/CustomDropdown/theme.scss +0 -118
  283. package/src/shared/components/selectors/NativeDropdown/index.tsx +0 -90
  284. package/src/shared/components/selectors/NativeDropdown/theme.scss +0 -81
  285. package/src/shared/components/selectors/Switch/index.tsx +0 -74
  286. package/src/shared/components/selectors/Switch/theme.scss +0 -39
  287. package/src/shared/components/selectors/common.ts +0 -57
  288. package/src/shared/components/selectors/index.ts +0 -8
  289. package/src/shared/utils/config.ts +0 -28
  290. package/src/shared/utils/globalState.ts +0 -44
  291. package/src/shared/utils/index.ts +0 -45
  292. package/src/shared/utils/isomorphy/buildInfo.ts +0 -50
  293. package/src/shared/utils/isomorphy/environment-check.ts +0 -18
  294. package/src/shared/utils/isomorphy/index.ts +0 -36
  295. package/src/shared/utils/jest/E2eSsrEnv.ts +0 -305
  296. package/src/shared/utils/jest/global.ts +0 -17
  297. package/src/shared/utils/jest/index.ts +0 -215
  298. package/src/shared/utils/splitComponent.tsx +0 -275
  299. package/src/shared/utils/time.ts +0 -101
  300. package/src/shared/utils/webpack.ts +0 -74
  301. package/src/styles/_global/reset.css +0 -52
  302. package/src/styles/_mixins/fonts.scss +0 -40
  303. package/src/styles/_mixins/media.scss +0 -150
  304. package/src/styles/_mixins/typography.scss +0 -83
  305. package/src/styles/global.scss +0 -11
  306. package/src/styles/mixins.scss +0 -6
@@ -1,305 +0,0 @@
1
- /**
2
- * Jest environment for end-to-end SSR and client-side testing. It relies on
3
- * the standard react-utils mechanics to execute SSR of given scene, and also
4
- * Webpack build of the code for client-side execution, it further exposes
5
- * Jsdom environment for the client-side testing of the outcomes.
6
- */
7
-
8
- // BEWARE: The module is not imported into the JU module / the main assembly of
9
- // the library, because doing so easily breaks stuff:
10
- // 1) This module depends on Node-specific modules, which would make JU
11
- // incompatible with JsDom if included into JU.
12
- // 2) If this module is weakly imported from somewhere else in the lib,
13
- // it seems to randomly break tests using it for a different reason,
14
- // probably some sort of a require-loop, or some issues with weak
15
- // require in that scenario.
16
-
17
- // TODO: We need to add correct typing for environment options.
18
-
19
- import path from 'node:path';
20
-
21
- import type { Request, Response } from 'express';
22
-
23
- import defaults from 'lodash/defaults';
24
- import set from 'lodash/set';
25
-
26
- import type { ReactNode } from 'react';
27
-
28
- // As this environment is a part of the Jest testing utils,
29
- // we assume development dependencies are available when it is used.
30
- /* eslint-disable import/no-extraneous-dependencies */
31
- import register from '@babel/register/experimental-worker';
32
-
33
- import JsdomEnv from 'jest-environment-jsdom';
34
- import { Volume, createFsFromVolume } from 'memfs';
35
- import webpack, { type Configuration } from 'webpack';
36
- /* eslint-enable import/no-extraneous-dependencies */
37
-
38
- import type {
39
- EnvironmentContext,
40
- JestEnvironmentConfig,
41
- } from '@jest/environment';
42
-
43
- import ssrFactory from 'server/renderer';
44
-
45
- import { setBuildInfo } from '../isomorphy/buildInfo';
46
-
47
- function noop() {
48
- // NOOP
49
- }
50
-
51
- export default class E2eSsrEnv extends JsdomEnv {
52
- pragmas: Record<string, string | string[]>;
53
-
54
- ssrRequest: object;
55
-
56
- rootDir: string;
57
-
58
- testFolder: string;
59
-
60
- withSsr: boolean;
61
-
62
- webpackStats?: webpack.StatsCompilation;
63
-
64
- /**
65
- * Loads Webpack config, and exposes it to the environment via global
66
- * webpackConfig object.
67
- */
68
- private loadWebpackConfig() {
69
- const optionsString = this.pragmas['webpack-config-options'] as string;
70
-
71
- const options = (optionsString ? JSON.parse(optionsString) : {}) as
72
- webpack.Configuration;
73
-
74
- defaults(options, {
75
- context: this.testFolder,
76
- fs: this.global.webpackOutputFs,
77
- });
78
-
79
- const factoryPath = this.pragmas['webpack-config-factory'] as string;
80
- // eslint-disable-next-line import/no-dynamic-require, @typescript-eslint/no-require-imports
81
- let factory = require(path.resolve(this.rootDir, factoryPath)) as
82
- (((ops: Configuration) => Configuration) | {
83
- default: (ops: Configuration) => Configuration;
84
- });
85
- factory = 'default' in factory ? factory.default : factory;
86
-
87
- this.global.webpackConfig = factory(options);
88
-
89
- const fs = this.global.webpackOutputFs;
90
- let buildInfo = `${options.context}/.build-info`;
91
- if (fs.existsSync(buildInfo)) {
92
- buildInfo = fs.readFileSync(buildInfo, 'utf8') as string;
93
- this.global.buildInfo = JSON.parse(buildInfo);
94
- }
95
- }
96
-
97
- /**
98
- * Executes Webpack build.
99
- * @return {Promise}
100
- */
101
- async runWebpack(): Promise<void> {
102
- this.loadWebpackConfig();
103
-
104
- if (!this.global.webpackConfig) throw Error('Failed to load Webpack config');
105
- const compiler = webpack(this.global.webpackConfig);
106
- if (!compiler) throw Error('Failed to construct Webpack compiler');
107
-
108
- // TODO: The "as typeof compiler.outputFileSystem" piece below is
109
- // a workaround for the Webpack regression:
110
- // https://github.com/webpack/webpack/issues/18242
111
- compiler.outputFileSystem = this.global.webpackOutputFs as
112
- typeof compiler.outputFileSystem;
113
-
114
- return new Promise<void>((done, fail) => {
115
- compiler.run((err, stats) => {
116
- if (err) fail(err);
117
- if (stats?.hasErrors()) {
118
- // eslint-disable-next-line no-console
119
- console.error(stats.toJson().errors);
120
- fail(Error('Webpack compilation failed'));
121
- }
122
-
123
- this.global.webpackStats = stats?.toJson();
124
-
125
- // Keeps reference to the raw Webpack stats object, which should be
126
- // explicitly passed to the server-side renderer alongside the request,
127
- // so that it can to pick up asset paths for different named chunks.
128
- this.webpackStats = stats;
129
-
130
- done();
131
- });
132
- });
133
- }
134
-
135
- async runSsr(): Promise<void> {
136
- const optionsString = this.pragmas['ssr-options'] as string;
137
- const options = optionsString
138
- ? JSON.parse(optionsString) as Record<string, unknown>
139
- : {};
140
-
141
- // TODO: This is temporary to shortcut the logging added to SSR.
142
- options.logger ??= {
143
- debug: noop,
144
- info: noop,
145
- log: noop,
146
- warn: noop,
147
- };
148
-
149
- options.buildInfo ??= this.global.buildInfo;
150
-
151
- let cleanup: (() => void) | undefined;
152
-
153
- if (options.entry) {
154
- const p = path.resolve(this.testFolder, options.entry as string);
155
- // TODO: This sure can be replaced by a dynamic import().
156
- // eslint-disable-next-line import/no-dynamic-require, @typescript-eslint/no-require-imports
157
- const module = require(p) as NodeJS.Module;
158
- if ('cleanup' in module) cleanup = module.cleanup as () => void;
159
-
160
- const exportName = (options.entryExportName as string) || 'default';
161
- if (exportName in module) {
162
- options.Application = (
163
- module as unknown as Record<string, unknown>
164
- )[exportName] as ReactNode;
165
- }
166
- }
167
-
168
- const renderer = ssrFactory(this.global.webpackConfig!, options);
169
- let status = 200; // OK
170
- const markup = await new Promise<string>((done, fail) => {
171
- void renderer(
172
- this.ssrRequest as Request,
173
-
174
- // TODO: This will do for now, with the current implementation of
175
- // the renderer, but it will require a rework once the renderer is
176
- // updated to do streaming.
177
- ({
178
- cookie: noop,
179
- send: done,
180
- set: noop,
181
- status: (value: number) => {
182
- status = value;
183
- },
184
-
185
- // This is how up-to-date Webpack stats are passed to the server in
186
- // development mode, and we use this here always, instead of having
187
- // to pass some information via filesystem.
188
- locals: {
189
- webpack: {
190
- devMiddleware: {
191
- stats: this.webpackStats,
192
- },
193
- },
194
- },
195
- } as unknown) as Response,
196
-
197
- (error) => {
198
- // TODO: Strictly speaking, that error as Error casting is not all
199
- // correct, but it works, so no need to spend time on it right now.
200
- if (error) fail(error as Error);
201
- else done('');
202
- },
203
- );
204
- });
205
-
206
- this.global.ssrMarkup = markup;
207
- this.global.ssrOptions = options;
208
- this.global.ssrStatus = status;
209
-
210
- if (cleanup) cleanup();
211
- }
212
-
213
- constructor(
214
- config: JestEnvironmentConfig,
215
- context: EnvironmentContext,
216
- ) {
217
- const pragmas = context.docblockPragmas;
218
-
219
- const requestString = pragmas['ssr-request'] as string;
220
- const request = requestString
221
- ? JSON.parse(requestString) as Record<string, unknown>
222
- : {};
223
-
224
- request.url ??= '/';
225
- request.csrfToken = noop;
226
-
227
- // This ensures the initial JsDom URL matches the value we use for SSR.
228
- set(
229
- config.projectConfig,
230
- 'testEnvironmentOptions.url',
231
- `http://localhost${request.url as string}`,
232
- );
233
-
234
- super(config, context);
235
-
236
- this.global.dom = this.dom;
237
- this.global.webpackOutputFs = createFsFromVolume(new Volume());
238
-
239
- // Extracts necessary settings from config and context.
240
- const { projectConfig } = config;
241
- this.rootDir = projectConfig.rootDir;
242
- this.testFolder = path.dirname(context.testPath);
243
- this.withSsr = !pragmas['no-ssr'];
244
- this.ssrRequest = request;
245
- this.pragmas = pragmas;
246
-
247
- // The usual "babel-jest" transformation setup does not apply to
248
- // the environment code and imports from it, this workaround enables it.
249
- const optionsString = this.pragmas['ssr-options'] as string;
250
- const options = optionsString
251
- ? JSON.parse(optionsString) as Record<string, unknown>
252
- : {};
253
- let root;
254
- switch (options.root) {
255
- case 'TEST':
256
- root = this.testFolder;
257
- break;
258
- default: root = process.cwd();
259
- }
260
- register({
261
- envName: options.babelEnv as string,
262
- extensions: ['.js', '.jsx', '.ts', '.tsx', '.svg'],
263
- root,
264
- });
265
- }
266
-
267
- async setup(): Promise<void> {
268
- await super.setup();
269
- await this.runWebpack();
270
-
271
- // NOTE: It is possible that the Webpack run above, and the SSR run below
272
- // load different versions of the same module (CommonJS, and ES), and it may
273
- // cause very confusing problems (e.g. see:
274
- // https://github.com/birdofpreyru/react-utils/issues/413).
275
- // It seems we can't reset the cache of ES modules, and Jest's module reset
276
- // does not reset modules loaded in this enviroment module, and also only
277
- // replacing entire cache object by and empty {} seems to help (in contrast
278
- // to deleting all entries by their keys, as it is done within .teardown()
279
- // method below). Thus, for now we do this as a hotfix, and we also reset
280
- // build info to undefined, because ES module version not beeing reset
281
- // triggers an error on the subsequent test using the environment.
282
- // TODO: Look for a cleaner solution.
283
- require.cache = {};
284
- setBuildInfo(undefined, true);
285
-
286
- if (this.withSsr) await this.runSsr();
287
- this.global.REACT_UTILS_FORCE_CLIENT_SIDE = true;
288
- }
289
-
290
- async teardown(): Promise<void> {
291
- delete this.global.REACT_UTILS_FORCE_CLIENT_SIDE;
292
-
293
- // Resets module cache and @babel/register. Effectively this ensures that
294
- // the next time an instance of this environment is set up, all modules are
295
- // transformed by Babel from scratch, thus taking into account the latest
296
- // Babel config (which may change between different environment instances,
297
- // which does not seem to be taken into account by Babel / Node caches
298
- // automatically).
299
- Object.keys(require.cache).forEach((key) => {
300
- delete require.cache[key];
301
- });
302
- register.revert();
303
- await super.teardown();
304
- }
305
- }
@@ -1,17 +0,0 @@
1
- import type { IFs } from 'memfs';
2
- import type { Configuration, StatsCompilation } from 'webpack';
3
-
4
- declare global {
5
- // eslint-disable-next-line @typescript-eslint/consistent-type-definitions
6
- interface Window {
7
- ssrMarkup: string | undefined;
8
- ssrStatus: number | undefined;
9
- webpackConfig: Configuration | undefined;
10
- webpackOutputFs: IFs;
11
- webpackStats?: StatsCompilation;
12
- }
13
- }
14
-
15
- export default function getGlobal(): Window {
16
- return global as unknown as Window;
17
- }
@@ -1,215 +0,0 @@
1
- /* global jest, document */
2
- /* eslint-disable import/no-extraneous-dependencies */
3
-
4
- import type {
5
- AxiosRequestConfig,
6
- AxiosResponse,
7
- AxiosStatic,
8
- InternalAxiosRequestConfig,
9
- } from 'axios';
10
-
11
- import mockdate from 'mockdate';
12
- import { type ReactElement, type ReactNode, act } from 'react';
13
- import { type Root, createRoot } from 'react-dom/client';
14
-
15
- import { type RenderResult, render } from '@testing-library/react';
16
-
17
- /**
18
- * An alias for [act(..)](https://reactjs.org/docs/test-utils.html#act)
19
- * from `react`.
20
- */
21
- export { act };
22
-
23
- export { default as getGlobal } from './global';
24
-
25
- global.IS_REACT_ACT_ENVIRONMENT = true;
26
-
27
- const originalProcessVersions = process.versions;
28
-
29
- /**
30
- * Tricks **react-utils** into thinking the test is running within client-side
31
- * (browser) environment.
32
- */
33
- export function mockClientSide(): void {
34
- Object.defineProperty(process, 'versions', { value: undefined });
35
- }
36
-
37
- /**
38
- * Reverts the effect of {@link module:JU.mockClientSide mockClientSide(..)}.
39
- */
40
- export function unmockClientSide(): void {
41
- Object.defineProperty(process, 'versions', {
42
- value: originalProcessVersions,
43
- writable: false,
44
- });
45
- }
46
-
47
- /**
48
- * Generates a mock UUID, or better said it determenistically transforms given
49
- * `seed` number into a UUID-formatted string.
50
- * @param {number} seed
51
- * @return {string}
52
- */
53
- export function getMockUuid(seed = 0): string {
54
- const x = seed.toString(16).padStart(32, '0');
55
- return `${x.slice(0, 8)}-${x.slice(8, 12)}-${x.slice(12, 16)}-${x.slice(16, 20)}-${x.slice(20)}`;
56
- }
57
-
58
- export type AxiosRequestHandlerT = (
59
- config: AxiosRequestConfig,
60
- ) => Partial<AxiosResponse> | null | undefined;
61
-
62
- export function mockAxios(handlers: AxiosRequestHandlerT[]): AxiosStatic {
63
- const axios: AxiosStatic = jest.requireActual('axios');
64
-
65
- axios.defaults.adapter = async (
66
- config: AxiosRequestConfig,
67
- ): Promise<AxiosResponse> => {
68
- for (const handler of handlers) {
69
- const res = handler(config);
70
- if (res) {
71
- return {
72
- config: config as InternalAxiosRequestConfig,
73
- data: null,
74
- headers: {},
75
- status: 200,
76
- statusText: 'OK',
77
- ...res,
78
- };
79
- }
80
- }
81
-
82
- // Fallback to the regular network request.
83
- let res: AxiosResponse;
84
- try {
85
- res = await axios({ ...config, adapter: ['xhr', 'http', 'fetch'] });
86
- // eslint-disable-next-line no-console
87
- console.warn(
88
- 'Network request has not been mocked for a test.\n\nConfig:\n',
89
- config,
90
- '\n\nResult:\n',
91
- JSON.stringify(res, null, 2),
92
- );
93
- } catch (e) {
94
- // eslint-disable-next-line no-console
95
- console.warn(
96
- 'Network request has not been mocked for a test, and failed.\n\nConfig:\n',
97
- config,
98
- '\n\nError\n',
99
- JSON.stringify(e, null, 2),
100
- );
101
- throw e;
102
- }
103
-
104
- return res;
105
- };
106
-
107
- return axios;
108
- }
109
-
110
- /**
111
- * Advances mock timers, and mock date by the specified time.
112
- * @param {number} time Time step [ms].
113
- * @returns {Promise} Wait for this to "jump after" any async code which should
114
- * be executed because of the mock time movement.
115
- */
116
- export async function mockTimer(time: number): Promise<void> {
117
- mockdate.set(time + Date.now());
118
- await jest.advanceTimersByTimeAsync(time);
119
- }
120
-
121
- export type MountedSceneT = HTMLElement & {
122
- destroy: () => void;
123
- snapshot: () => void;
124
- };
125
-
126
- /**
127
- * Mounts `scene` to the DOM, and returns the root scene element.
128
- * @param scene
129
- * @return Created container DOM element with destroy() function
130
- * attached.
131
- */
132
- export function mount(scene: ReactNode): MountedSceneT {
133
- let root: Root;
134
- const element = document.createElement('div');
135
- document.body.appendChild(element);
136
-
137
- const res: MountedSceneT = (element as unknown) as MountedSceneT;
138
- res.destroy = () => {
139
- // NOTE: As it seems @testing-library may reset this flag to false
140
- // when it is simulating user events.
141
- global.IS_REACT_ACT_ENVIRONMENT = true;
142
-
143
- act(() => {
144
- root.unmount();
145
- });
146
- res.remove();
147
- };
148
-
149
- res.snapshot = () => {
150
- expect(res).toMatchSnapshot();
151
- };
152
-
153
- // NOTE: As it seems @testing-library may reset this flag to false
154
- // when it is simulating user events.
155
- global.IS_REACT_ACT_ENVIRONMENT = true;
156
-
157
- act(() => {
158
- root = createRoot(res);
159
- root.render(scene);
160
- });
161
- return res;
162
- }
163
-
164
- // NOTE: If in future we have additional options here, they should be distributed
165
- // across two objects, depending whether they are applicable to the sync, or async
166
- // versions of snapshot(), or both.
167
- type AsyncSnapshotOptionsT = {
168
- await?: Promise<void>;
169
- };
170
-
171
- export function snapshot(element: ReactElement): RenderResult;
172
-
173
- export async function snapshot(
174
- element: ReactElement,
175
- options: AsyncSnapshotOptionsT,
176
- ): Promise<RenderResult>;
177
-
178
- export function snapshot(
179
- element: React.ReactElement,
180
- options?: AsyncSnapshotOptionsT,
181
- ): Promise<RenderResult> | RenderResult {
182
- let res: RenderResult | undefined;
183
-
184
- // TODO: Just adding async to the actor function breaks stuff, as it makes
185
- // act() asynchronous no matter the `options.await` value, thus breaking all
186
- // calls that do not await for snapshot() result... thus... perhaps we need
187
- // to have a more complex typing to ensure it all works as intended in all
188
- // cases, and while being correctly enforced by TypeScript.
189
- // eslint-disable-next-line @typescript-eslint/promise-function-async
190
- const promise = act(() => {
191
- res = render(element);
192
- return options?.await;
193
- });
194
-
195
- if (res === undefined) throw Error('Render failed');
196
- if (options?.await) {
197
- // BEWARE: Although `promise` is thenable (i.e. it has .then() method),
198
- // it is not an instance of proper Promise class, and returning it directly
199
- // breaks some async logic in Jest test or React test functions... thus, we
200
- // wrap it into Promise instance here.
201
- return new Promise((resolve) => {
202
- void promise.then(() => {
203
- // TODO: These lines are the same as the lines below for sync variant of
204
- // the function. We should split and reuse them in both places.
205
- const nodes = res!.asFragment().childNodes;
206
- expect(nodes.length > 1 ? [...nodes] : nodes[0]).toMatchSnapshot();
207
- resolve(res!);
208
- });
209
- });
210
- }
211
-
212
- const nodes = res.asFragment().childNodes;
213
- expect(nodes.length > 1 ? [...nodes] : nodes[0]).toMatchSnapshot();
214
- return res;
215
- }