Skip to content

Commit

Permalink
fix: use default values when field not found in output methods
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed Apr 24, 2020
1 parent 3c17e85 commit 1d1871b
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 38 deletions.
1 change: 1 addition & 0 deletions config/babel/babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module.exports = (api) => {
const plugins = [
"babel-plugin-add-module-exports",
"@babel/plugin-transform-object-assign",
"@babel/plugin-proposal-optional-chaining",
];

return {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"license": "MIT",
"dependencies": {
"@ampproject/rollup-plugin-closure-compiler": "^0.25.2",
"@babel/plugin-proposal-optional-chaining": "^7.9.0",
"@babel/cli": "^7.8.4",
"@babel/core": "^7.9.0",
"@babel/plugin-transform-object-assign": "^7.8.3",
Expand Down
42 changes: 21 additions & 21 deletions packages/vest/dist/vest.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/vest/dist/vest.min.js

Large diffs are not rendered by default.

20 changes: 4 additions & 16 deletions packages/vest/src/core/suiteResult/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,7 @@ const suiteResult = (name) => {
return collectFailureMessages("errors");
}

if (output.tests[fieldName].errors) {
return output.tests[fieldName].errors;
}

return [];
return output.tests?.[fieldName]?.errors || [];
};

/**
Expand All @@ -221,11 +217,7 @@ const suiteResult = (name) => {
return collectFailureMessages("warnings");
}

if (output.tests[fieldName].warnings) {
return output.tests[fieldName].warnings;
}

return [];
return output.tests?.[fieldName]?.warnings || [];
};

/**
Expand All @@ -238,9 +230,7 @@ const suiteResult = (name) => {
return !!output.errorCount;
}

return Boolean(
output.tests[fieldName] && output.tests[fieldName].errorCount
);
return Boolean(output.tests?.[fieldName]?.errorCount);
};

/**
Expand All @@ -253,9 +243,7 @@ const suiteResult = (name) => {
return !!output.warnCount;
}

return Boolean(
output.tests[fieldName] && output.tests[fieldName].warnCount
);
return Boolean(output.tests?.[fieldName]?.warnCount);
};

const output = {
Expand Down
24 changes: 24 additions & 0 deletions packages/vest/src/core/suiteResult/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ describe("suiteResult module", () => {
expect(output.hasErrors(fieldName1)).toBe(true);
expect(output.hasErrors(fieldName2)).toBe(false);
});

describe("When field doesn't exist", () => {
it("Should return false", () => {
expect(output.hasErrors(fieldName2)).toBe(false);
});
});
});
});

Expand All @@ -122,6 +128,12 @@ describe("suiteResult module", () => {
expect(output.hasWarnings(fieldName1)).toBe(true);
expect(output.hasWarnings(fieldName2)).toBe(false);
});

describe("When field doesn't exist", () => {
it("Should return false", () => {
expect(output.hasWarnings("I Do Not Exist")).toBe(false);
});
});
});
});

Expand All @@ -147,6 +159,12 @@ describe("suiteResult module", () => {
res.markFailure({ fieldName: fieldName2, statement: statement2 });
expect(output.getErrors(fieldName2)).toEqual([statement, statement2]);
});

describe("When field doesn't exist", () => {
it("Should return an empty array", () => {
expect(output.getErrors("I Do Not Exist")).toEqual([]);
});
});
});
});

Expand Down Expand Up @@ -187,6 +205,12 @@ describe("suiteResult module", () => {
statement2,
]);
});

describe("When field doesn't exist", () => {
it("Should return an empty array", () => {
expect(output.getWarnings("I Do Not Exist")).toEqual([]);
});
});
});
});

Expand Down

0 comments on commit 1d1871b

Please sign in to comment.