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

prompts.processors.json.test.ts 1.4 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
  1. import * as fs from 'fs';
  2. import { processJsonFile } from '../src/prompts/processors/json';
  3. jest.mock('fs');
  4. describe('processJsonFile', () => {
  5. const mockReadFileSync = jest.mocked(fs.readFileSync);
  6. beforeEach(() => {
  7. jest.clearAllMocks();
  8. });
  9. it('should process a valid JSON file without a label', () => {
  10. const filePath = 'file.json';
  11. const fileContent = JSON.stringify({ key: 'value' });
  12. mockReadFileSync.mockReturnValue(fileContent);
  13. expect(processJsonFile(filePath, {})).toEqual([
  14. {
  15. raw: fileContent,
  16. label: `${filePath}: ${fileContent}`,
  17. },
  18. ]);
  19. expect(mockReadFileSync).toHaveBeenCalledWith(filePath, 'utf8');
  20. });
  21. it('should process a valid JSON file with a label', () => {
  22. const filePath = 'file.json';
  23. const fileContent = JSON.stringify({ key: 'value' });
  24. mockReadFileSync.mockReturnValue(fileContent);
  25. expect(processJsonFile(filePath, { label: 'Label' })).toEqual([
  26. {
  27. raw: fileContent,
  28. label: `Label`,
  29. },
  30. ]);
  31. expect(mockReadFileSync).toHaveBeenCalledWith(filePath, 'utf8');
  32. });
  33. it('should throw an error if the file cannot be read', () => {
  34. const filePath = 'nonexistent.json';
  35. mockReadFileSync.mockImplementation(() => {
  36. throw new Error('File not found');
  37. });
  38. expect(() => processJsonFile(filePath, {})).toThrow('File not found');
  39. expect(mockReadFileSync).toHaveBeenCalledWith(filePath, 'utf8');
  40. });
  41. });
Tip!

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

Comments

Loading...