Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

163
Views
Test primitive value on if statement with Jest

I would like to create a test with Jest to make sure there is an if statement in my code. Let's say we have a function that receives a string and I need to verify if this string includes something in it, if there is execute another function otherwise just ignore it.

function create (str) {
  if (str.includes('something')) {
    otherFunction()
  }

  ...
}

So I want not just to make sure there is an if statement, I also want that the string "calling" includes is str. I know I can use String.prototype but it does not make sure the string "calling" includes is str, any other string calling the same function passes the test

What I have till now:

test('Should call otherFunction if str includes something', () => {
  const { sut, otherObj } = makeSut()
  const otherFunctionSpy = jest.spyOn(otherObj, 'otherFunction')
  const includesSpy = jest.spyOn(String.prototype, 'includes')

  sut.create('has_something')

  expect(includesSpy).toHaveBeenCalledWith('something')
  expect(otherFunctionSpy).toHaveBeenCalled()
})
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!