I have a controller which used rxjs' lastValueFrom method, and I want to mock the method so that the actual method won't be called. My code for the controller is:
async doSomething(request) {
...
const data = await lastValueFrom(
...
);
...
}
For the test, I've tried multiple ways, which is:
import rxjs from 'rxjs';
jest.spyOn(rxjs, 'lastValueFrom').mockReturnValueOnce(
new Promise((resolve, reject) => {
resolve(true);
}),
);
// This gives me error: Cannot spyOn on a primitive value; undefined given
import { lastValueFrom } from 'rxjs';
lastValueFrom = jest.fn().mockReturnValueOnce(
new Promise((resolve, reject) => {
resolve(true),
);
// This gives me error: Cannot assign to 'lastValueFrom' because it is an import.
import rxjs from 'rxjs';
rxjs.lastValueFrom = jest.fn().mockReturnValue(
new Promise((resolve, reject) => {
resolve(true);
}),
);
// This gives me error: Cannot set property 'lastValueFrom' of undefined