|
1 | 1 | import * as jsYaml from "js-yaml"; |
2 | | -import { FirebaseError } from "../error"; |
| 2 | +import { getErrMsg, FirebaseError } from "../error"; |
3 | 3 | import { TestCase } from "./types"; |
4 | 4 |
|
5 | 5 | declare interface YamlStep { |
@@ -63,9 +63,7 @@ function checkAllowedKeys(allowedKeys: Set<string>, o: object) { |
63 | 63 | } |
64 | 64 |
|
65 | 65 | function fromYamlTestCases(appName: string, yamlTestCases: YamlTestCase[]): TestCase[] { |
66 | | - if (!Array.isArray(yamlTestCases)) { |
67 | | - throw new FirebaseError("YAML file must contain a list of test cases."); |
68 | | - } |
| 66 | + |
69 | 67 | return yamlTestCases.map((yamlTestCase) => { |
70 | 68 | checkAllowedKeys(ALLOWED_YAML_TEST_CASE_KEYS, yamlTestCase); |
71 | 69 | return { |
@@ -93,5 +91,14 @@ function fromYamlTestCases(appName: string, yamlTestCases: YamlTestCase[]): Test |
93 | 91 | } |
94 | 92 |
|
95 | 93 | export function fromYaml(appName: string, yaml: string): TestCase[] { |
96 | | - return fromYamlTestCases(appName, jsYaml.safeLoad(yaml) as YamlTestCase[]); |
| 94 | + let parsedYaml: unknown |
| 95 | + try { |
| 96 | + parsedYaml = jsYaml.safeLoad(yaml) |
| 97 | + } catch (err: unknown) { |
| 98 | + throw new FirebaseError(`Failed to parse YAML: ${getErrMsg(err)}`) |
| 99 | + } |
| 100 | + if (!Array.isArray(parsedYaml)) { |
| 101 | + throw new FirebaseError("YAML file must contain a list of test cases."); |
| 102 | + } |
| 103 | + return fromYamlTestCases(appName, parsedYaml as YamlTestCase[]); |
97 | 104 | } |
0 commit comments