Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

no-large-snapshots.md 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. # disallow large snapshots (`no-large-snapshots`)
  2. When using Jest's snapshot capability one should be mindful of the size of
  3. created snapshots. As a general best practice snapshots should be limited in
  4. size in order to be more manageable and reviewable. A stored snapshot is only as
  5. good as its review and as such keeping it short, sweet, and readable is
  6. important to allow for thorough reviews.
  7. ## Usage
  8. Because Jest snapshots are written with back-ticks (\` \`) which are only valid
  9. with
  10. [ES2015 onwards](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals)
  11. you should set `parserOptions` in your config to at least allow ES2015 in order
  12. to use this rule:
  13. ```js
  14. module.exports = {
  15. parserOptions: {
  16. ecmaVersion: 2015,
  17. },
  18. };
  19. ```
  20. ## Rule Details
  21. This rule looks at all Jest inline and external snapshots (files with `.snap`
  22. extension) and validates that each stored snapshot within those files does not
  23. exceed 50 lines (by default, this is configurable as explained in `Options`
  24. section below).
  25. Example of **incorrect** code for this rule:
  26. ```js
  27. exports[`a large snapshot 1`] = `
  28. line 1
  29. line 2
  30. line 3
  31. line 4
  32. line 5
  33. line 6
  34. line 7
  35. line 8
  36. line 9
  37. line 10
  38. line 11
  39. line 12
  40. line 13
  41. line 14
  42. line 15
  43. line 16
  44. line 17
  45. line 18
  46. line 19
  47. line 20
  48. line 21
  49. line 22
  50. line 23
  51. line 24
  52. line 25
  53. line 26
  54. line 27
  55. line 28
  56. line 29
  57. line 30
  58. line 31
  59. line 32
  60. line 33
  61. line 34
  62. line 35
  63. line 36
  64. line 37
  65. line 38
  66. line 39
  67. line 40
  68. line 41
  69. line 42
  70. line 43
  71. line 44
  72. line 45
  73. line 46
  74. line 47
  75. line 48
  76. line 49
  77. line 50
  78. line 51
  79. `;
  80. ```
  81. Example of **correct** code for this rule:
  82. ```js
  83. exports[`a more manageable and readable snapshot 1`] = `
  84. line 1
  85. line 2
  86. line 3
  87. line 4
  88. `;
  89. ```
  90. ## Options
  91. This rule has options for modifying the max number of lines allowed for a
  92. snapshot:
  93. In an `eslintrc` file:
  94. ```json
  95. {
  96. "rules": {
  97. "jest/no-large-snapshots": ["warn", { "maxSize": 12, "inlineMaxSize": 6 }]
  98. }
  99. }
  100. ```
  101. Max number of lines allowed could be defined by snapshot type (Inline and
  102. External). Use `inlineMaxSize` for
  103. [Inline Snapshots](https://jestjs.io/docs/en/snapshot-testing#inline-snapshots)
  104. size and `maxSize` for
  105. [External Snapshots](https://jestjs.io/docs/en/snapshot-testing#snapshot-testing-with-jest).
  106. If only `maxSize` is provided on options, the value of `maxSize` will be used to
  107. both snapshot types (Inline and External).
  108. Since `eslint-disable` comments are not preserved by Jest when updating
  109. snapshots, you can use the `allowedSnapshots` option to have specific snapshots
  110. allowed regardless of their size.
  111. This option takes a map, with the key being the absolute filepath to a snapshot
  112. file, and the value an array of values made up of strings and regular
  113. expressions to compare to the names of the snapshots in the `.snap` file when
  114. checking if the snapshots size should be allowed.
  115. Note that regular expressions can only be passed in via `.eslintrc.js` as
  116. instances of `RegExp`.
  117. In an `.eslintrc.js` file:
  118. ```javascript
  119. module.exports = {
  120. rules: {
  121. 'jest/no-large-snapshots': [
  122. 'error',
  123. {
  124. allowedSnapshots: {
  125. '/path/to/file.js.snap': ['snapshot name 1', /a big snapshot \d+/],
  126. },
  127. },
  128. ],
  129. },
  130. };
  131. ```
  132. Since absolute paths are typically not very portable, you can use the builtin
  133. `path.resolve` function to expand relative paths into absolutes like so:
  134. ```javascript
  135. const path = require('path');
  136. module.exports = {
  137. rules: {
  138. 'jest/no-large-snapshots': [
  139. 'error',
  140. {
  141. allowedSnapshots: {
  142. [path.resolve('test/__snapshots__/get.js.snap')]: ['full request'],
  143. [path.resolve('test/__snapshots__/put.js.snap')]: ['full request'],
  144. },
  145. },
  146. ],
  147. },
  148. };
  149. ```