You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have issues with mocking an async function. I assume that I can return a promise when the mocked method is called, so that the then function can be called on that promise. However when running the test, I get the following error:
1) LoginSpec
login should return a valid app token:
TypeError: Cannot read property 'then' of null
at Promise (src/login.js:1:11787)
at new Promise (<anonymous>)
at LoginService.getToken (src/login.js:1:11319)
at LoginService.<anonymous> (src/login.js:1:9410)
...
Test class:
import {expect} from 'chai';
import {suite, test} from "mocha-typescript";
import {anything, instance, mock, when} from "ts-mockito";
import {LoginService} from '../src/login';
import {RestAccess} from "../src/rest";
@suite
class LoginSpec {
private restMock = mock(RestAccess);
@test.only
public async 'login should return a valid app token'() {
// given
const ssoToken = {
access_token: 'AccessToken',
refresh_token: 'RefreshToken',
};
const promise = new Promise((resolve, reject) => {
resolve(ssoToken);
});
when(this.restMock.fetchJson('url', anything))
.thenReturn(promise);
const sut = new LoginService(instance(this.restMock));
// when
const event = {
queryStringParameters: {
code: 'ssoCode',
},
};
const appToken = await sut.login(event);
// then
const decodedToken = this.auth.decodeToken(appToken);
...
}
}
I have issues with mocking an async function. I assume that I can return a promise when the mocked method is called, so that the then function can be called on that promise. However when running the test, I get the following error:
Test class:
Call to mock:
Mocked method:
package.json
The text was updated successfully, but these errors were encountered: