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 pose 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),
);
});
});
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 pose 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),
);
});
});
Share
Improve this question
edited Mar 11, 2019 at 16:46
Quentin Del
asked Mar 11, 2019 at 16:31
Quentin DelQuentin Del
1,6953 gold badges21 silver badges34 bronze badges
1 Answer
Reset to default 5I don't think bining 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);
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744288398a4566912.html
评论列表(0条)