I am using Jest to test for a function, which generate a english word.
I tried below but not working. Just want to check if the word is combined by english characters
it("generate a english word", () => {
const word = "APPLE";
expect(word).toMatch('/^[A-Za-z]*$/');
});
What's wrong with it?
CodePudding user response:
This test
expect(word).toMatch('/^[A-Za-z]*$/');
tries to match the string '/^[A-Za-z]*$/'
.
You want to pass it as a regular expression instead
expect(word).toMatch(/^[A-Za-z]*$/);
The lack of quotation marks in the second case makes all the difference.
Here is the link to the documentation https://jestjs.io/docs/expect#tomatchregexp--string