-1

I am trying to make a pure function using a for loop pass a jest test/npm test in the terminal... I am getting an error that it cannot read the property of toBe...

My function:

const syntax = {
   for1: (a,b) => {
      for(let a=1; a<10; a++){
         for(let b=1; b<10; b++){
             return a+b;
         }
      }
   }
}

My Test.js file: I want it to test that 1+2 does not equal 0 making this test passing for the function

test('FORLOOP', () => {
    expect(syntax.for1(1,2).not.toBe(0));
});

TypeError in terminal:

TypeError: Cannot read property 'toBe' of undefined
      45 | test('FORLOOP', () => {
    > 46 |     expect(syntax.for1(1+3).not.toBe(0));
         |            ^
      47 | });

CHANGES:

TEST FILE: (fixed brackets)

test('FORLOOP', () => {
    expect(syntax.for1(1,2).not.toBe(0));
});
    TypeError: _syntax.default.for1 is not a function

      55 | 
      56 | test('FORLOOP', () => {
    > 57 |     expect(syntax.for1(1+3)).not.toBe(0);
         |                   ^
      58 | });
waterMelon
  • 15
  • 5

1 Answers1

0

The brackets are in the wrong place. It should be:

expect(syntax.for1(1+3)).not.toBe(0);

not

expect(syntax.for1(1+3).not.toBe(0));

.not needs to be called on the result of expect(...) not on the result of syntax.for1(1+3).

A Jar of Clay
  • 5,622
  • 6
  • 25
  • 39
  • Thanks! I ran the test now and it says for1 is not a function, why is that? TypeError: _syntax.default.for1 is not a function 55 | 56 | test('FORLOOP', () => { > 57 | expect(syntax.for1(1+3).not.toBe(0)); | ^ 58 | }); – waterMelon Feb 26 '20 at 16:20
  • @waterMelon that error message suggests that you have not corrected the brackets. Correct them, then try again. – A Jar of Clay Feb 26 '20 at 16:38
  • Please give a few more details if that doesn't work! – A Jar of Clay Feb 26 '20 at 17:51
  • @waterMelon how are you exporting/importing the syntax object? Also, `for1` takes two parameters but you are calling it with one in the test? – A Jar of Clay Feb 27 '20 at 11:19