expectedStrings = [
'abc',
'def',
'ghi'
]
stringToCheck = 'zyxabc'
I want to check if stringToCheck contains one of the strings of expectedStrings with Jest. I have seen stringContaining and arrayContaining methods but I'm not sure how should I compose to make the test work.
I would like to use something close to this example :
describe('stringMatching in arrayContaining', () => {
const expected = [
expect.stringMatching(/^Alic/),
expect.stringMatching(/^[BR]ob/),
];
it('matches even if received contains additional elements', () => {
expect(['Alicia', 'Roberto', 'Evelina']).toEqual(
expect.arrayContaining(expected),
);
});
it('does not match if received does not contain expected elements', () => {
expect(['Roberto', 'Evelina']).not.toEqual(
expect.arrayContaining(expected),
);
});
});
I don't think combining arrayContaining and stringContaining is the best and most readable approach here (If even possible at all).
Instead simply try this method:
const numberOfMatches = expectedStrings.filter(x => stringToCheck.indexOf(x) > -1).length;
expect(numberOfMatches).toBeGreaterThan(0);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With