-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathload-fixture.ts
More file actions
50 lines (46 loc) · 1.46 KB
/
load-fixture.ts
File metadata and controls
50 lines (46 loc) · 1.46 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
/**
* Load and parse a fixture file
*/
import fs from "fs";
/**
* Interface for the parsed fixture data structure
*/
export interface FixtureData {
export: string;
input: Record<string, any>;
expectedOutput: Record<string, any>;
target: string;
}
/**
* Load and parse a fixture file, extracting the payload data
* @param {string} filename - The path to the fixture JSON file
* @returns {Promise<Object>} The parsed fixture data with structure:
* - export: Object - The export data from payload.export
* - input: Object - The input data from payload.input
* - expectedOutput: Object - The output data from payload.output
* - target: string - The target string from payload.target
*/
export async function loadFixture(filename: string): Promise<FixtureData> {
try {
const fixtureContent = await fs.promises.readFile(filename, "utf-8");
const fixture = JSON.parse(fixtureContent);
return {
export: fixture.payload.export,
input: fixture.payload.input,
expectedOutput: fixture.payload.output,
target: fixture.payload.target,
};
} catch (error) {
if (error instanceof SyntaxError) {
throw new Error(
`Invalid JSON in fixture file ${filename}: ${error.message}`,
);
} else if (error instanceof Error) {
throw new Error(
`Failed to load fixture file ${filename}: ${error.message}`,
);
} else {
throw new Error(`Unknown error loading fixture file ${filename}`);
}
}
}