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

envars.test.ts 2.9 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
  1. import { getEnvString, getEnvBool, getEnvInt, getEnvFloat } from '../src/envars';
  2. describe('envars', () => {
  3. const originalEnv = process.env;
  4. beforeEach(() => {
  5. jest.resetModules();
  6. process.env = { ...originalEnv };
  7. });
  8. afterAll(() => {
  9. process.env = originalEnv;
  10. });
  11. describe('getEnvar', () => {
  12. it('should return the value of an existing environment variable', () => {
  13. process.env.PROMPTFOO_AUTHOR = 'test value';
  14. expect(getEnvString('PROMPTFOO_AUTHOR')).toBe('test value');
  15. });
  16. it('should return undefined for a non-existing environment variable', () => {
  17. expect(getEnvString('PROMPTFOO_AUTHOR')).toBeUndefined();
  18. });
  19. it('should return the default value for a non-existing environment variable', () => {
  20. expect(getEnvString('PROMPTFOO_AUTHOR', 'default')).toBe('default');
  21. });
  22. });
  23. describe('getEnvBool', () => {
  24. it('should return true for truthy string values', () => {
  25. ['1', 'true', 'yes', 'yup', 'yeppers'].forEach((value) => {
  26. process.env.PROMPTFOO_CACHE_ENABLED = value;
  27. expect(getEnvBool('PROMPTFOO_CACHE_ENABLED')).toBe(true);
  28. });
  29. });
  30. it('should return false for falsy string values', () => {
  31. ['0', 'false', 'no', 'nope'].forEach((value) => {
  32. process.env.PROMPTFOO_CACHE_ENABLED = value;
  33. expect(getEnvBool('PROMPTFOO_CACHE_ENABLED')).toBe(false);
  34. });
  35. });
  36. it('should return the default value for a non-existing environment variable', () => {
  37. expect(getEnvBool('PROMPTFOO_CACHE_ENABLED', true)).toBe(true);
  38. expect(getEnvBool('PROMPTFOO_CACHE_ENABLED', false)).toBe(false);
  39. });
  40. });
  41. describe('getEnvInt', () => {
  42. it('should return the integer value of an existing environment variable', () => {
  43. process.env.PROMPTFOO_CACHE_MAX_FILE_COUNT = '42';
  44. expect(getEnvInt('PROMPTFOO_CACHE_MAX_FILE_COUNT')).toBe(42);
  45. });
  46. it('should return undefined for a non-numeric environment variable', () => {
  47. process.env.PROMPTFOO_CACHE_MAX_FILE_COUNT = 'not a number';
  48. expect(getEnvInt('PROMPTFOO_CACHE_MAX_FILE_COUNT')).toBeUndefined();
  49. });
  50. it('should return the default value for a non-existing environment variable', () => {
  51. expect(getEnvInt('PROMPTFOO_CACHE_MAX_FILE_COUNT', 100)).toBe(100);
  52. });
  53. });
  54. describe('getEnvFloat', () => {
  55. it('should return the float value of an existing environment variable', () => {
  56. process.env.OPENAI_TEMPERATURE = '3.14';
  57. expect(getEnvFloat('OPENAI_TEMPERATURE')).toBe(3.14);
  58. });
  59. it('should return undefined for a non-numeric environment variable', () => {
  60. process.env.OPENAI_TEMPERATURE = 'not a number';
  61. expect(getEnvFloat('OPENAI_TEMPERATURE')).toBeUndefined();
  62. });
  63. it('should return the default value for a non-existing environment variable', () => {
  64. expect(getEnvFloat('OPENAI_TEMPERATURE', 2.718)).toBe(2.718);
  65. });
  66. });
  67. });
Tip!

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

Comments

Loading...