Я пытаюсь преобразовать свои модульные тесты с Jasmine в Jest. Некоторые тесты начали давать сбой после преобразования их в Jest. Может кто-нибудь объяснить, почему они терпят неудачу с Jest.
Мне удалось выделить проблему в тестовом примере ниже.
С Жасмин проходит успешно:
import { JasmineMarble } from './jasmine-marble';
import { cold } from 'jasmine-marbles';
import { switchMap } from 'rxjs/operators';
import { EMPTY, Observable } from 'rxjs';
class Service {
foo(): Observable<any> {
return EMPTY;
}
bar(a): Observable<any> {
return EMPTY;
}
}
describe('JasmineMarble', () => {
it('should create an instance', () => {
const service = new Service();
spyOn(service, 'foo').and.returnValue(cold('a|', { a: 'A' }));
spyOn(service, 'bar').and.returnValue(cold('a-b-c|', { a: 'A', b: 'B', c: 'C'}));
const result$ = service.foo().pipe(switchMap(a => service.bar(a)));
expect(result$).toBeObservable(cold('a-b-c|', { a: 'A', b: 'B', c: 'C'}));
expect(service.bar).toHaveBeenCalledWith('A');
});
});
С Jest это не удается с этой трассировкой ошибки:
expect(jest.fn()).toHaveBeenCalledWith(expected)
Expected mock function to have been called with:
["A"]
But it was not called.
Код Jest:
import { JestMarble } from './jest-marble';
import { cold } from 'jest-marbles';
import { switchMap } from 'rxjs/operators';
import { EMPTY, Observable } from 'rxjs';
class Service {
foo(): Observable<any> {
return EMPTY;
}
bar(a): Observable<any> {
return EMPTY;
}
}
describe('JestMarble', () => {
it('should create an instance', () => {
const service = new Service();
jest.spyOn(service, 'foo').mockReturnValue(cold('a|', { a: 'A' }));
jest.spyOn(service, 'bar').mockReturnValue(cold('a-b-c|', { a: 'A', b: 'B', c: 'C'}));
const result$ = service.foo().pipe(switchMap(a => service.bar(a)));
expect(result$).toBeObservable(cold('a-b-c|', { a: 'A', b: 'B', c: 'C'}));
expect(service.bar).toHaveBeenCalledWith('A');
});
});
Может кто-нибудь объяснить такое поведение?
Здесь вы можете найти пример репозитория: https://github.com/stijnvn/marbles Пример Jasmine можно запустить с ng test jasmine-marbles
. Шутка с ng test jest-marbles
.
"jasmine-marbles": "=0.3.1"
на"jest-marbles": "=2.3.1"
все тесты со шпионами начали терпеть неудачу :( - person Felix   schedule 17.05.2019