2021-08-04
Mise en place de Jest sans Rewire dans Movie Scrapping
-
Intégration dans le projet pour UT de Jest
-
Suppression de Rewire car incompatibilité forte avec la fonction coverage de Jest
A la place, je procède de la manière suivante suivante:
- dans
<filename>.js, je rend mes fonctions agnostique du contexte global - création d'un
<filename>.util.js avec les fonctions à tester unitairement que je ne veux pas exporter de<filename>.js - import de ce
<filename>.util.js dans<filename>.js - test de
<filename>.util.js avec Jest - test de
<filename>.js avec Jest - le coverage de
<filename>.util.js et<filename>.js est correcte
Fix de l'utilisation du module de mémoization mem
Objectif: prendre en compte plusieurs paramètres (pas seulement le premier paramètre comme par defaut)
cf documentation
From mem
Go to text →
Caching strategy
By default, only the first argument is compared via exact equality (===) to determine whether a call is identical.
const power = mem((a, b) => Math.power(a, b));
power(2, 2); // => 4, stored in cache with the key 2 (number)
power(2, 3); // => 4, retrieved from cache at key 2 (number), it's wrong
You will have to use the cache and cacheKey options appropriate to your function. In this specific case, the following could work:
const power = mem((a, b) => Math.power(a, b), {
cacheKey: arguments_ => arguments_.join(',')
});
power(2, 2); // => 4, stored in cache with the key '2,2' (both arguments as one string)
power(2, 3); // => 8, stored in cache with the key '2,3'
More advanced examples follow.