-
Notifications
You must be signed in to change notification settings - Fork 192
Expand file tree
/
Copy pathtestingMetadata.ts
More file actions
52 lines (45 loc) · 1.39 KB
/
testingMetadata.ts
File metadata and controls
52 lines (45 loc) · 1.39 KB
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
import {
clearMetadataCache,
getMetadata,
} from '@aws-lambda-powertools/commons/utils/metadata';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
describe('function: getMetadata', async () => {
let fetchMock: ReturnType<typeof vi.fn>;
beforeEach(() => {
vi.clearAllMocks();
clearMetadataCache();
fetchMock = vi.fn();
vi.stubGlobal('fetch', fetchMock);
});
afterEach(() => {
vi.unstubAllEnvs();
vi.unstubAllGlobals();
});
it('fetches metadata and caches the response', async () => {
// Prepare
vi.stubEnv('AWS_LAMBDA_INITIALIZATION_TYPE', 'on-demand');
vi.stubEnv('AWS_LAMBDA_METADATA_API', '127.0.0.1:1234');
vi.stubEnv('AWS_LAMBDA_METADATA_TOKEN', 'test-token');
const payload = { runtime: 'nodejs22.x' };
fetchMock.mockResolvedValue({
ok: true,
json: vi.fn().mockResolvedValue(payload),
});
// Act
const resultA = await getMetadata();
const resultB = await getMetadata();
// Assess
expect(resultA).toEqual(payload);
expect(resultB).toBe(resultA);
expect(fetchMock).toHaveBeenCalledTimes(1);
expect(fetchMock).toHaveBeenCalledWith(
'http://127.0.0.1:1234/2026-01-15/metadata/execution-environment',
expect.objectContaining({
headers: {
Authorization: 'Bearer test-token',
},
signal: expect.any(AbortSignal),
})
);
});
});