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 5.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
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
  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. const result = await importModule(modulePath);
  21. // importModule extracts the nested default from CommonJS modules
  22. expect(result).toEqual({
  23. testFunction: expect.any(Function),
  24. });
  25. expect(result.testFunction()).toBe('js default test result');
  26. expect(logger.debug).toHaveBeenCalledWith(
  27. expect.stringContaining('Successfully required module'),
  28. );
  29. });
  30. it('imports TypeScript modules', async () => {
  31. const modulePath = path.resolve(__dirname, '__fixtures__/testModule.ts');
  32. const result = await importModule(modulePath);
  33. expect(result).toEqual({
  34. testFunction: expect.any(Function),
  35. defaultProp: 'ts default property',
  36. });
  37. expect(result.testFunction()).toBe('ts default test result');
  38. expect(logger.debug).toHaveBeenCalledWith(
  39. expect.stringContaining('TypeScript/ESM module detected'),
  40. );
  41. });
  42. it('imports CommonJS modules', async () => {
  43. const modulePath = path.resolve(__dirname, '__fixtures__/testModule.cjs');
  44. const result = await importModule(modulePath);
  45. // importModule extracts the nested default from CommonJS modules
  46. expect(result).toEqual({
  47. testFunction: expect.any(Function),
  48. });
  49. expect(result.testFunction()).toBe('cjs default test result');
  50. });
  51. it('imports ESM modules', async () => {
  52. const modulePath = path.resolve(__dirname, '__fixtures__/testModule.mjs');
  53. const result = await importModule(modulePath);
  54. expect(result).toEqual({
  55. testFunction: expect.any(Function),
  56. defaultProp: 'esm default property',
  57. });
  58. expect(result.testFunction()).toBe('esm default test result');
  59. });
  60. it('imports simple modules without nested defaults', async () => {
  61. const modulePath = path.resolve(__dirname, '__fixtures__/testModuleSimple.js');
  62. const result = await importModule(modulePath);
  63. expect(result).toEqual(expect.any(Function));
  64. expect(result()).toBe('simple function result');
  65. });
  66. it('returns named function when functionName is specified', async () => {
  67. const modulePath = path.resolve(__dirname, '__fixtures__/testModule.js');
  68. const result = await importModule(modulePath, 'testFunction');
  69. expect(result).toEqual(expect.any(Function));
  70. expect(result()).toBe('js default test result');
  71. expect(logger.debug).toHaveBeenCalledWith('Returning named export: testFunction');
  72. });
  73. it('returns named function from TypeScript module', async () => {
  74. const modulePath = path.resolve(__dirname, '__fixtures__/testModule.ts');
  75. const result = await importModule(modulePath, 'testFunction');
  76. expect(result).toEqual(expect.any(Function));
  77. expect(result()).toBe('ts default test result');
  78. });
  79. it('handles absolute paths', async () => {
  80. const absolutePath = path.resolve(__dirname, '__fixtures__/testModule.js');
  81. const result = await importModule(absolutePath);
  82. expect(result).toEqual({
  83. testFunction: expect.any(Function),
  84. });
  85. expect(result.testFunction()).toBe('js default test result');
  86. });
  87. it('throws error for non-existent module', async () => {
  88. const nonExistentPath = path.resolve(__dirname, '__fixtures__/nonExistent.js');
  89. await expect(importModule(nonExistentPath)).rejects.toThrow();
  90. expect(logger.debug).toHaveBeenCalledWith(expect.stringContaining('ESM import failed'));
  91. expect(logger.debug).toHaveBeenCalledWith(
  92. expect.stringContaining('CommonJS require also failed'),
  93. );
  94. });
  95. it('logs debug information during import process', async () => {
  96. const modulePath = path.resolve(__dirname, '__fixtures__/testModule.js');
  97. await importModule(modulePath);
  98. expect(logger.debug).toHaveBeenCalledWith(
  99. expect.stringContaining('Attempting to import module'),
  100. );
  101. expect(logger.debug).toHaveBeenCalledWith(
  102. expect.stringContaining('Attempting ESM import from'),
  103. );
  104. });
  105. it('falls back to CommonJS when ESM import fails', async () => {
  106. const modulePath = path.resolve(__dirname, '__fixtures__/testModule.cjs');
  107. const result = await importModule(modulePath);
  108. expect(result).toEqual({
  109. testFunction: expect.any(Function),
  110. });
  111. // Should try ESM first, then fall back to CommonJS
  112. expect(logger.debug).toHaveBeenCalledWith(expect.stringContaining('ESM import failed'));
  113. expect(logger.debug).toHaveBeenCalledWith(
  114. expect.stringContaining('Successfully required module'),
  115. );
  116. });
  117. it('extracts named export from ESM module', async () => {
  118. const modulePath = path.resolve(__dirname, '__fixtures__/testModule.mjs');
  119. const result = await importModule(modulePath, 'testFunction');
  120. expect(result).toEqual(expect.any(Function));
  121. expect(result()).toBe('esm default test result');
  122. });
  123. });
  124. });
Tip!

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

Comments

Loading...