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

esm.test.ts 2.2 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
  1. import path from 'path';
  2. import { importModule } from '../src/esm';
  3. import logger from '../src/logger';
  4. jest.mock('../src/logger', () => ({
  5. __esModule: true,
  6. default: {
  7. debug: jest.fn(),
  8. error: jest.fn(),
  9. warn: jest.fn(),
  10. info: jest.fn(),
  11. },
  12. }));
  13. describe('ESM utilities', () => {
  14. beforeEach(() => {
  15. jest.clearAllMocks();
  16. });
  17. describe('importModule', () => {
  18. it('imports JavaScript modules', async () => {
  19. const modulePath = path.resolve(__dirname, 'fixtures/testModule.js');
  20. // Mock the file system module
  21. jest.mock('fs', () => ({
  22. existsSync: jest.fn().mockReturnValue(true),
  23. readFileSync: jest
  24. .fn()
  25. .mockReturnValue('module.exports = { testFunction: () => "test result" }'),
  26. }));
  27. // Mock the module
  28. jest.doMock(
  29. modulePath,
  30. () => ({
  31. default: { testFunction: () => 'test result' },
  32. }),
  33. { virtual: true },
  34. );
  35. const result = await importModule(modulePath);
  36. expect(result).toEqual({ testFunction: expect.any(Function) });
  37. expect(logger.debug).toHaveBeenCalledWith(
  38. expect.stringContaining('Successfully required module'),
  39. );
  40. });
  41. it('imports TypeScript modules', async () => {
  42. const modulePath = path.resolve(__dirname, 'fixtures/testModule.ts');
  43. jest.doMock(
  44. modulePath,
  45. () => ({
  46. default: { testFunction: () => 'test result' },
  47. }),
  48. { virtual: true },
  49. );
  50. const result = await importModule(modulePath);
  51. expect(result).toEqual({ testFunction: expect.any(Function) });
  52. expect(logger.debug).toHaveBeenCalledWith(
  53. expect.stringContaining('TypeScript/ESM module detected'),
  54. );
  55. });
  56. it('handles absolute paths', async () => {
  57. const absolutePath = path.resolve('/absolute/path/module.js');
  58. jest.doMock(
  59. absolutePath,
  60. () => ({
  61. default: { testFunction: () => 'absolute path result' },
  62. }),
  63. { virtual: true },
  64. );
  65. const result = await importModule(absolutePath);
  66. expect(result).toEqual({ testFunction: expect.any(Function) });
  67. });
  68. });
  69. });
Tip!

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

Comments

Loading...