forked from promptfoo/promptfoo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesm.test.ts
79 lines (69 loc) · 2.16 KB
/
esm.test.ts
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
import path from 'path';
import { importModule } from '../src/esm';
import logger from '../src/logger';
jest.mock('../src/logger', () => ({
__esModule: true,
default: {
debug: jest.fn(),
error: jest.fn(),
warn: jest.fn(),
info: jest.fn(),
},
}));
describe('ESM utilities', () => {
beforeEach(() => {
jest.clearAllMocks();
});
describe('importModule', () => {
it('imports JavaScript modules', async () => {
const modulePath = path.resolve(__dirname, 'fixtures/testModule.js');
// Mock the file system module
jest.mock('fs', () => ({
existsSync: jest.fn().mockReturnValue(true),
readFileSync: jest
.fn()
.mockReturnValue('module.exports = { testFunction: () => "test result" }'),
}));
// Mock the module
jest.doMock(
modulePath,
() => ({
default: { testFunction: () => 'test result' },
}),
{ virtual: true },
);
const result = await importModule(modulePath);
expect(result).toEqual({ testFunction: expect.any(Function) });
expect(logger.debug).toHaveBeenCalledWith(
expect.stringContaining('Successfully required module'),
);
});
it('imports TypeScript modules', async () => {
const modulePath = path.resolve(__dirname, 'fixtures/testModule.ts');
jest.doMock(
modulePath,
() => ({
default: { testFunction: () => 'test result' },
}),
{ virtual: true },
);
const result = await importModule(modulePath);
expect(result).toEqual({ testFunction: expect.any(Function) });
expect(logger.debug).toHaveBeenCalledWith(
expect.stringContaining('TypeScript/ESM module detected'),
);
});
it('handles absolute paths', async () => {
const absolutePath = path.resolve('/absolute/path/module.js');
jest.doMock(
absolutePath,
() => ({
default: { testFunction: () => 'absolute path result' },
}),
{ virtual: true },
);
const result = await importModule(absolutePath);
expect(result).toEqual({ testFunction: expect.any(Function) });
});
});
});