toEmitValuesWith.test.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import { interval, Observable, of, throwError } from 'rxjs';
  2. import { map, mergeMap, take } from 'rxjs/operators';
  3. import { OBSERVABLE_TEST_TIMEOUT_IN_MS } from './types';
  4. describe('toEmitValuesWith matcher', () => {
  5. describe('failing tests', () => {
  6. describe('passing null in expect', () => {
  7. it('should fail with correct message', async () => {
  8. const observable = null as unknown as Observable<number>;
  9. const rejects = expect(() =>
  10. expect(observable).toEmitValuesWith((received) => {
  11. expect(received).toEqual([1, 2, 3]);
  12. })
  13. ).rejects;
  14. await rejects.toThrow();
  15. });
  16. });
  17. describe('passing undefined in expect', () => {
  18. it('should fail with correct message', async () => {
  19. const observable = undefined as unknown as Observable<number>;
  20. const rejects = expect(() =>
  21. expect(observable).toEmitValuesWith((received) => {
  22. expect(received).toEqual([1, 2, 3]);
  23. })
  24. ).rejects;
  25. await rejects.toThrow();
  26. });
  27. });
  28. describe('passing number instead of Observable in expect', () => {
  29. it('should fail with correct message', async () => {
  30. const observable = 1 as unknown as Observable<number>;
  31. const rejects = expect(() =>
  32. expect(observable).toEmitValuesWith((received) => {
  33. expect(received).toEqual([1, 2, 3]);
  34. })
  35. ).rejects;
  36. await rejects.toThrow();
  37. });
  38. });
  39. describe('wrong number of emitted values', () => {
  40. it('should fail with correct message', async () => {
  41. const observable = interval(10).pipe(take(3));
  42. const rejects = expect(() =>
  43. expect(observable).toEmitValuesWith((received) => {
  44. expect(received).toEqual([0, 1]);
  45. })
  46. ).rejects;
  47. await rejects.toThrow();
  48. });
  49. });
  50. describe('wrong emitted values', () => {
  51. it('should fail with correct message', async () => {
  52. const observable = interval(10).pipe(take(3));
  53. const rejects = expect(() =>
  54. expect(observable).toEmitValuesWith((received) => {
  55. expect(received).toEqual([1, 2, 3]);
  56. })
  57. ).rejects;
  58. await rejects.toThrow();
  59. });
  60. });
  61. describe('wrong emitted value types', () => {
  62. it('should fail with correct message', async () => {
  63. const observable = interval(10).pipe(take(3)) as unknown as Observable<string>;
  64. const rejects = expect(() =>
  65. expect(observable).toEmitValuesWith((received) => {
  66. expect(received).toEqual(['0', '1', '2']);
  67. })
  68. ).rejects;
  69. await rejects.toThrow();
  70. });
  71. });
  72. describe(`observable that does not complete within ${OBSERVABLE_TEST_TIMEOUT_IN_MS}ms`, () => {
  73. it('should fail with correct message', async () => {
  74. const observable = interval(600);
  75. const rejects = expect(() =>
  76. expect(observable).toEmitValuesWith((received) => {
  77. expect(received).toEqual([0]);
  78. })
  79. ).rejects;
  80. await rejects.toThrow();
  81. });
  82. });
  83. });
  84. describe('passing tests', () => {
  85. describe('correct emitted values', () => {
  86. it('should pass with correct message', async () => {
  87. const observable = interval(10).pipe(take(3));
  88. await expect(observable).toEmitValuesWith((received) => {
  89. expect(received).toEqual([0, 1, 2]);
  90. });
  91. });
  92. });
  93. describe('correct emitted values with throw', () => {
  94. it('should pass with correct message', async () => {
  95. const observable = interval(10).pipe(
  96. map((interval) => {
  97. if (interval > 1) {
  98. throw 'an error';
  99. }
  100. return interval;
  101. })
  102. );
  103. await expect(observable).toEmitValuesWith((received) => {
  104. expect(received).toEqual([0, 1, 'an error']);
  105. });
  106. });
  107. });
  108. describe('correct emitted values with throwError', () => {
  109. it('should pass with correct message', async () => {
  110. const observable = interval(10).pipe(
  111. mergeMap((interval) => {
  112. if (interval === 1) {
  113. return throwError('an error');
  114. }
  115. return of(interval);
  116. })
  117. );
  118. await expect(observable).toEmitValuesWith((received) => {
  119. expect(received).toEqual([0, 'an error']);
  120. });
  121. });
  122. });
  123. });
  124. });