Angular + Jasmine, move test to the common module. Service -> undefined

114 views
Skip to first unread message

gonzo...@gmail.com

unread,
Nov 26, 2018, 11:47:44 AM11/26/18
to Jasmine
I took the frequently used test in the common module 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;


Calling a common test from 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], (authService1: AuthService) => {
      expect
(authService).toBeFalsy();
      authService
= authService1;
      expect
(authService).toBeTruthy();
   
}
 
));

 
globals.relogin(globals.user, authService); // <--------

});


As a result, the test from the common module fails due to: TypeError: Cannot read property 'logout' of undefined

common_test_fail.png

But the service object was definitely created, just for some reason undefined came to the common module.

Please tell me how do I pass the service object in the test, which I move in the common module?

Gregg Van Hove

unread,
Dec 4, 2018, 8:51:42 PM12/4/18
to jasmi...@googlegroups.com
It looks like there might be a couple of things going on here. First, you're only assigning to your `authService` inside of one of the tests, because Jasmine runs your tests in random order by default, your common modules tests might run before the tests that assigns the variable. If you want to use the value in multiple tests, you should probably do the assignment in a beforeEach.

Once the creation/assignment is happening in a beforeEach, you might still have an issue because your `authService` won't actually be created and assigned until the beforeEach is invoked. You'll either need to pass in a closure (a function that returns the local variable should work here), or store the reference somewhere that the tests in the common module can access it. If you use the `function` keyword instead of the lambda syntax (`() => {}`), your calls will have access to the same `this` that Jasmine maintains for each test.

Hope this helps. Thanks for using Jasmine!

-Gregg


--
You received this message because you are subscribed to the Google Groups "Jasmine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jasmine-js+...@googlegroups.com.
To post to this group, send email to jasmi...@googlegroups.com.
Visit this group at https://groups.google.com/group/jasmine-js.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages