BlogEx
BACK TO FEED
2020.08.03 ● PUBLIC

Test runs ok from webstorm, but fails as nx test lib in terminal

reactjestnxrxjs

SYMPTOMS

The test in question was related to the rxjs code below:

this.allStartTimes$ = this.store.select(selectAllCheckedCountersValues).pipe(
map(countersValues => {
const allValues = countersValues
.map(counterValues => counterValues.values)
.flat()
.map(value => value.startTime);
return sortedUniq(allValues.sort());
})
);

The tests were failing with generic TypeError, after adding catchError in related observables, the following error was shown:

TypeError: countersValues.map(...).flat is not a function

Which is weird because it is a legit array function and this case runs from webstorm Run button no prob.

FIX

This is a known issue in ts-jest, adding core-js to test setup fixes the issue, as described here.

Weird how it works in webstorm though. In this case, I couldn't add core-js to test setup, so the workaround was to use flatten from lodash instead of array.flat().

this.allStartTimes$ = this.store.select(selectAllCheckedCountersValues).pipe(
map(countersValues => {
const allValues: Value[][] = countersValues.map(
counterValues => counterValues.values
);
const allTimestamps: string[] = flatten(allValues).map(
(value: Value) => value.startTime
);
return sortedUniq(allTimestamps.sort());
})
);