Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

invariant.test.ts 1.3 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
  1. import invariant from '../../src/util/invariant';
  2. describe('invariant', () => {
  3. it('should not throw when condition is true', () => {
  4. expect(() => invariant(true)).not.toThrow();
  5. expect(() => invariant(1)).not.toThrow();
  6. expect(() => invariant({})).not.toThrow();
  7. });
  8. it('should throw when condition is false', () => {
  9. expect(() => invariant(false)).toThrow('Invariant failed');
  10. expect(() => invariant(0)).toThrow('Invariant failed');
  11. expect(() => invariant(null)).toThrow('Invariant failed');
  12. expect(() => invariant(undefined)).toThrow('Invariant failed');
  13. });
  14. it('should include the provided message in the error', () => {
  15. const message = 'Custom error message';
  16. expect(() => invariant(false, message)).toThrow('Invariant failed: Custom error message');
  17. });
  18. it('should support message callback functions', () => {
  19. const getMessage = () => 'Dynamic message';
  20. expect(() => invariant(false, getMessage)).toThrow('Invariant failed: Dynamic message');
  21. });
  22. it('should work for type narrowing', () => {
  23. const value: string | null = 'test';
  24. // This should compile without type errors
  25. invariant(value !== null, 'Value should not be null');
  26. // After the invariant, TypeScript should know that value is a string
  27. const length: number = value.length;
  28. expect(length).toBe(4);
  29. });
  30. });
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...