-1
const {sum, subtract} = require('./math')



let result, expected
result = sum(3, 7)
expected = 10
expect(result).toBe(expected)


function expect(actual) {
  return {
    toBe(expected) {
      if (actual !== expected) {
        throw new Error(`${actual} is not equal to ${expected}`)
      }
    },
  }
}

is toBe{...} and object or a function?

It is used as a function when you call expect(result).toBe(expected).However, in the expect function definition it looks like an object because it doesn't have a function keyword. However objects do not accept parameters. Thank you in advance.

1 Answers1

0

I think you are new to ES6 syntax, Search and read some article online.

Function expect is returning an object (return {}); That Object has a function called toBe(Its ES6 way of writing function inside object that you can omit keyword function).

Similarly inside ES6 classes there is no need to use the keyword function

Sumer
  • 2,687
  • 24
  • 24