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-conditional-expect.md 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # Prevent calling `expect` conditionally (`no-conditional-expect`)
  2. This rule prevents the use of `expect` in conditional blocks, such as `if`s &
  3. `catch`s.
  4. This includes using `expect` in callbacks to functions named `catch`, which are
  5. assumed to be promises.
  6. ## Rule Details
  7. Jest considered a test to have failed if it throws an error, rather than on if
  8. any particular function is called, meaning conditional calls to `expect` could
  9. result in tests silently being skipped.
  10. Additionally, conditionals tend to make tests more brittle and complex, as they
  11. increase the amount of mental thinking needed to understand what is actually
  12. being tested.
  13. While `expect.assertions` & `expect.hasAssertions` can help prevent tests from
  14. silently being skipped, when combined with conditionals they typically result in
  15. even more complexity being introduced.
  16. The following patterns are warnings:
  17. ```js
  18. it('foo', () => {
  19. doTest && expect(1).toBe(2);
  20. });
  21. it('bar', () => {
  22. if (!skipTest) {
  23. expect(1).toEqual(2);
  24. }
  25. });
  26. it('baz', async () => {
  27. try {
  28. await foo();
  29. } catch (err) {
  30. expect(err).toMatchObject({ code: 'MODULE_NOT_FOUND' });
  31. }
  32. });
  33. it('throws an error', async () => {
  34. await foo().catch(error => expect(error).toBeInstanceOf(error));
  35. });
  36. ```
  37. The following patterns are not warnings:
  38. ```js
  39. it('foo', () => {
  40. expect(!value).toBe(false);
  41. });
  42. function getValue() {
  43. if (process.env.FAIL) {
  44. return 1;
  45. }
  46. return 2;
  47. }
  48. it('foo', () => {
  49. expect(getValue()).toBe(2);
  50. });
  51. it('validates the request', () => {
  52. try {
  53. processRequest(request);
  54. } catch {
  55. // ignore errors
  56. } finally {
  57. expect(validRequest).toHaveBeenCalledWith(request);
  58. }
  59. });
  60. it('throws an error', async () => {
  61. await expect(foo).rejects.toThrow(Error);
  62. });
  63. ```