NestJS fornisce un utility di testing integrato (@nestjs/testing) integrato con Jest, e la sua architettura di dependency injection rende il testing particolarmente pulito — crei un modulo di testing e inietti mock per le dipendenze. I test si dividono in unit test (isolati) e test e2e (app completa).
Unit test di un service (con dipendenze mockate)
{ } ;
(, {
: ;
mockRepo = { : jest.(), : jest.() };
( () => {
= .({
: [
,
{ : (), : mockRepo },
],
}).();
service = .<>();
});
(, () => {
mockRepo..([{ : , : }]);
result = service.();
(result).();
(mockRepo.).();
});
});
