When to use expect.stringContaining instead of toContain for substring matching in Jest

Viewed 1224

What's the use case to use stringContaining instead of just using toContain for substring matching with Jest?

it("tests with stringContaining", () => {
  expect("testerama").toEqual(expect.stringContaining("test"))
})
it("tests with toContain", () => {
  expect("testerama").toContain("test")
})

As far as I can see the behaviour is similar apart from that if the string is null or undefined the stringContaining error message is a bit clearer.

Expected: StringContaining "test"
Received: null

as opposed to

Matcher error: received value must not be null nor undefined
Received has value: null

It hardly seems worth it for the less terse code. Am I missing something?

1 Answers

The only real difference is that the expect.stringContaining method is maintained by the community, whereas expect.toContain is part of the official Jest API.

Related