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-standalone-expect.md 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # Disallow using `expect` outside of `it` or `test` blocks (`no-standalone-expect`)
  2. Prevents `expect` statements outside of a `test` or `it` block. An `expect`
  3. within a helper function (but outside of a `test` or `it` block) will not
  4. trigger this rule.
  5. ## Rule Details
  6. This rule aims to eliminate `expect` statements that will not be executed. An
  7. `expect` inside of a `describe` block but outside of a `test` or `it` block or
  8. outside of a `describe` will not execute and therefore will trigger this rule.
  9. It is viable, however, to have an `expect` in a helper function that is called
  10. from within a `test` or `it` block so `expect` statements in a function will not
  11. trigger this rule.
  12. Statements like `expect.hasAssertions()` will NOT trigger this rule since these
  13. calls will execute if they are not in a test block.
  14. Examples of **incorrect** code for this rule:
  15. ```js
  16. // in describe
  17. describe('a test', () => {
  18. expect(1).toBe(1);
  19. });
  20. // below other tests
  21. describe('a test', () => {
  22. it('an it', () => {
  23. expect(1).toBe(1);
  24. });
  25. expect(1).toBe(1);
  26. });
  27. ```
  28. Examples of **correct** code for this rule:
  29. ```js
  30. // in it block
  31. describe('a test', () => {
  32. it('an it', () => {
  33. expect(1).toBe(1);
  34. });
  35. });
  36. // in helper function
  37. describe('a test', () => {
  38. const helper = () => {
  39. expect(1).toBe(1);
  40. };
  41. it('an it', () => {
  42. helper();
  43. });
  44. });
  45. describe('a test', () => {
  46. expect.hasAssertions(1);
  47. });
  48. ```
  49. \*Note that this rule will not trigger if the helper function is never used even
  50. thought the `expect` will not execute. Rely on a rule like no-unused-vars for
  51. this case.
  52. ### Options
  53. #### `additionalTestBlockFunctions`
  54. This array can be used to specify the names of functions that should also be
  55. treated as test blocks:
  56. ```json
  57. {
  58. "rules": {
  59. "jest/no-standalone-expect": [
  60. "error",
  61. { "additionalTestBlockFunctions": ["each.test"] }
  62. ]
  63. }
  64. }
  65. ```
  66. The following is _correct_ when using the above configuration:
  67. ```js
  68. each([
  69. [1, 1, 2],
  70. [1, 2, 3],
  71. [2, 1, 3],
  72. ]).test('returns the result of adding %d to %d', (a, b, expected) => {
  73. expect(a + b).toBe(expected);
  74. });
  75. ```
  76. ## When Not To Use It
  77. Don't use this rule on non-jest test files.