Я взял часто используемый тест в общем модуле globals.js
:
const user = {
username: 'Skat',
password: '123',
role: 'user',
};
function relogin(user, authService) {
describe('--> relogin', () => {
it('--> ' + user.role + ', ' + user.username, async () => {
authService.logout();
expect(authService.isLogged()).toBe(false);
authService.login(user.username, user.password).subscribe();
await delay(800);
expect(authService.isLogged()).toBe(true);
});
});
}
module.exports.user = user;
module.exports.relogin = relogin;
Вызов общего теста из service.spec.ts
:
const globals = require('../globals');
describe('AdvertService', () => {
let authService: AuthService;
beforeEach(() => {
jasmine.DEFAULT_TIMEOUT_INTERVAL = 20000;
TestBed.configureTestingModule({
imports: [HttpClientModule, RouterTestingModule],
providers: [
AuthService
],
});
});
it('Injected services should be created', inject([AuthService], async (authService1: AuthService) => {
expect(authService).toBeFalsy();
authService = authService1;
expect(authService).toBeTruthy();
}
));
globals.relogin(globals.user, authService);
});
В результате тестаиз общего модуля не удается из-за:
TypeError: Невозможно прочитать свойство 'logout' из неопределенного
сбой теста printscreen
Но сервисный объект был определенно создан, просто по какой-то причине undefined
пришел к общему модулю.
Скажите, пожалуйста, как мне пройти сервисный объект в тесте, который я перемещаю в общем модуле?