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-hooks.md 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. # Disallow setup and teardown hooks (`no-hooks`)
  2. Jest provides global functions for setup and teardown tasks, which are called
  3. before/after each test case and each test suite. The use of these hooks promotes
  4. shared state between tests.
  5. ## Rule Details
  6. This rule reports for the following function calls:
  7. - `beforeAll`
  8. - `beforeEach`
  9. - `afterAll`
  10. - `afterEach`
  11. Examples of **incorrect** code for this rule:
  12. ```js
  13. /* eslint jest/no-hooks: "error" */
  14. function setupFoo(options) {
  15. /* ... */
  16. }
  17. function setupBar(options) {
  18. /* ... */
  19. }
  20. describe('foo', () => {
  21. let foo;
  22. beforeEach(() => {
  23. foo = setupFoo();
  24. });
  25. afterEach(() => {
  26. foo = null;
  27. });
  28. it('does something', () => {
  29. expect(foo.doesSomething()).toBe(true);
  30. });
  31. describe('with bar', () => {
  32. let bar;
  33. beforeEach(() => {
  34. bar = setupBar();
  35. });
  36. afterEach(() => {
  37. bar = null;
  38. });
  39. it('does something with bar', () => {
  40. expect(foo.doesSomething(bar)).toBe(true);
  41. });
  42. });
  43. });
  44. ```
  45. Examples of **correct** code for this rule:
  46. ```js
  47. /* eslint jest/no-hooks: "error" */
  48. function setupFoo(options) {
  49. /* ... */
  50. }
  51. function setupBar(options) {
  52. /* ... */
  53. }
  54. describe('foo', () => {
  55. it('does something', () => {
  56. const foo = setupFoo();
  57. expect(foo.doesSomething()).toBe(true);
  58. });
  59. it('does something with bar', () => {
  60. const foo = setupFoo();
  61. const bar = setupBar();
  62. expect(foo.doesSomething(bar)).toBe(true);
  63. });
  64. });
  65. ```
  66. ## Options
  67. ```json
  68. {
  69. "jest/no-hooks": [
  70. "error",
  71. {
  72. "allow": ["afterEach", "afterAll"]
  73. }
  74. ]
  75. }
  76. ```
  77. ### `allow`
  78. This array option controls which Jest hooks are checked by this rule. There are
  79. four possible values:
  80. - `"beforeAll"`
  81. - `"beforeEach"`
  82. - `"afterAll"`
  83. - `"afterEach"`
  84. By default, none of these options are enabled (the equivalent of
  85. `{ "allow": [] }`).
  86. Examples of **incorrect** code for the `{ "allow": ["afterEach"] }` option:
  87. ```js
  88. /* eslint jest/no-hooks: ["error", { "allow": ["afterEach"] }] */
  89. function setupFoo(options) {
  90. /* ... */
  91. }
  92. let foo;
  93. beforeEach(() => {
  94. foo = setupFoo();
  95. });
  96. afterEach(() => {
  97. jest.resetModules();
  98. });
  99. test('foo does this', () => {
  100. // ...
  101. });
  102. test('foo does that', () => {
  103. // ...
  104. });
  105. ```
  106. Examples of **correct** code for the `{ "allow": ["afterEach"] }` option:
  107. ```js
  108. /* eslint jest/no-hooks: ["error", { "allow": ["afterEach"] }] */
  109. function setupFoo(options) {
  110. /* ... */
  111. }
  112. afterEach(() => {
  113. jest.resetModules();
  114. });
  115. test('foo does this', () => {
  116. const foo = setupFoo();
  117. // ...
  118. });
  119. test('foo does that', () => {
  120. const foo = setupFoo();
  121. // ...
  122. });
  123. ```
  124. ## When Not To Use It
  125. If you prefer using the setup and teardown hooks provided by Jest, you can
  126. safely disable this rule.
  127. ## Further Reading
  128. - [Jest docs - Setup and Teardown](https://facebook.github.io/jest/docs/en/setup-teardown.html)