vendredi 19 juin 2020

Testing a js function with Jest returns a bad value

It is my first use of Jest and I try to test a function with many many values like this :

const convertConfig = require('../tools/convertNumWord/config');
const numToWordConstructor = require('../tools/convertNumWord/num2words/numToWords');
const wordToNumConstructor = require('../tools/convertNumWord/words2num/wordsToNum');

describe('EN-CONFIG', () => {
  const config = convertConfig['en'];  // It returns an object with configuration values for the language
  const numToWord = numToWordConstructor(config); // It return a function builds with config object 
  const wordToNum = wordToNumConstructor(config);

  for (let i = 0; i <= 4; i++) {
    it(`Test for value ${i}`, () => {
      expect(wordToNum(numToWord(i))).toBe(Number(i));
    });
  }
});

Jest returns this error :

 ● ENGLISH CONFIG › Test for value 2

    expect(received).toBe(expected) // Object.is equality

    Expected: 2
    Received: 1

      69 |   for (let i = 0; i <= 4; i++) {
      70 |     it(`Test for value ${i}`, () => {
    > 71 |       expect(wordToNum(numToWord(i))).toBe(Number(i));
         |                                       ^
      72 |     });
      73 |   }
      74 | });

      at Object.<anonymous> (__tests__/loopConvertNum.test.js:71:39)

  ● ENGLISH CONFIG › Test for value 3

    expect(received).toBe(expected) // Object.is equality

    Expected: 3
    Received: 1

      69 |   for (let i = 0; i <= 4; i++) {
      70 |     it(`Test for value ${i}`, () => {
    > 71 |       expect(wordToNum(numToWord(i))).toBe(Number(i));
         |                                       ^
      72 |     });
      73 |   }
      74 | });

      at Object.<anonymous> (__tests__/loopConvertNum.test.js:71:39)

  ● ENGLISH CONFIG › Test for value 4

    expect(received).toBe(expected) // Object.is equality

    Expected: 4
    Received: 1

      69 |   for (let i = 0; i <= 4; i++) {
      70 |     it(`Test for value ${i}`, () => {
    > 71 |       expect(wordToNum(numToWord(i))).toBe(Number(i));
         |                                       ^
      72 |     });
      73 |   }
      74 | });

      at Object.<anonymous> (__tests__/loopConvertNum.test.js:71:39)

Test Suites: 1 failed, 2 passed, 3 total
Tests:       3 failed, 1 todo, 3 passed, 7 total
Snapshots:   0 total
Time:        1.773s
Ran all test suites.

My first test worked but for 2 days, Jest does not seem to execute my function for each round of the for loop, as if it kept a value in memory (often that of the second round of the loop) and the toBe test is ultimately false. In some cases, the result of my functions are completely inconsistent (null or some kind of increment from the previous result). When I launch my function with node with the same arguments it works well.

I tried with it.each and the problem persists. The each mode is not good for me because I want to test my fonction for many many many values 😅.

Thank you in advance for your valuable feedback.

Aucun commentaire:

Enregistrer un commentaire