web 3d图形渲染器
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.

70 lines
1.4 KiB

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