I have code inside validate method if condition true it returns true else false. I want to test that code. Is there any way to do it.
Asked
Active
Viewed 452 times
-1
-
`validate` is not really meant for Jest tests, `validate` is for runtime validations. What did you tried so far? – kissu Dec 09 '21 at 15:19
-
@kissu I would really appreciate if you understand the **need** of the question rather than saying what is **meant**. Anyway I am happy you took interest in my question. – Vishal Sagar Dec 10 '21 at 05:59
1 Answers
0
So usually asyncData method and validate method are part of nuxt not vuejs and can't be tested usually but we do want to test it if it was possible.
Why test validate : As we are working on a long project we want to test it if every condition works as expected, someone's change didn't hinder the working of this method and thus i came around with solution to call that method manually and pass parameters to it accordingly.
// example.vue
validate({ params, query }) {
if (params.includes("abc" && query["something"])) {
return true;
} else {
return false;
}
}
// example.test.js
import index from "example.vue"
describe("test", () => {
it("Check validate method", async () => {
// Check when query present
const params = {
abc: "abc"
};
const query = {
something: "abc"
};
const validate = Index.validate({ params, $constants, query });
expect(validate).toBe(true); // or false
});
});

Vishal Sagar
- 331
- 1
- 3
- 13