Running a spec file before and after each test

36 views
Skip to first unread message

Ian Oroceo

unread,
Jan 17, 2018, 4:38:01 PM1/17/18
to Mocha
Hello, another noob question. I decided to ask this while I am searching in Google LOL

I have the following spec files
login.spec.js
feature1.spec.js
feature2.spec.js
logout.spec.js

currently feature1.spec.js and feature2.spec.js flow is, users login first then do something then logout.
instead of writing the same login and logout steps in the other spec file, how can I run the login spec first before each test and logout spec after each test? is that possible?

Thanks in advance! and apologies for a stupid question

Vlad GURDIGA

unread,
Jan 18, 2018, 4:17:01 AM1/18/18
to Mocha
Hey Ian, 👋

I would try to use the before/after hooks. Something like this:

describe('The app', function() {
  beforeEach(function() {
      login();
  });
  it('does the feature1 right', function() {
    // assertions
  });
  afterEach(function() {
      logout();
  });
});


Give it a try and come back with more questions if something doesn’t go as expected. Good luck! 🤓

Ian Christopher Oroceo

unread,
Jan 18, 2018, 12:25:50 PM1/18/18
to moc...@googlegroups.com
As always Vlad! you are awesome!
When I tried it yesterday, it was not working, I'm getting login is undefined or login is not a function. And I realize, I am defining login wrong LOL! 

Thank you Vlad!

--
You received this message because you are subscribed to a topic in the Google Groups "Mocha" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mochajs/p0B8h4nfooc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mochajs+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Vlad GURDIGA

unread,
Jan 18, 2018, 12:51:15 PM1/18/18
to Mocha
Don’t worry, it happens even to the best of us! 😊

Ian Oroceo

unread,
Jan 18, 2018, 1:01:54 PM1/18/18
to Mocha
I have another question Vlad.
Since I am reusing the login spec for all other test, the report is now showing 2 tests being executed
that is the expected behavior right?

So now I just have to figure out how to exclude that login spec file from the report

[chrome #0-0] Session ID:
[chrome #0-0] Spec: C:\Users\iano\Repos\Web\specs\Portal\smokeTests\login.spec.js
[chrome #0-0] Running: chrome
[chrome #0-0]
[chrome #0-0] QA_Login
[chrome #0-0]   √ Screeners Login
[chrome #0-0]
[chrome #0-0] QA_Smoke Tests for Login
[chrome #0-0]   √ C663811 Login with Valid Credentials/Multi-Portal User Selects Active Portal
[chrome #0-0]
[chrome #0-0]
[chrome #0-0] 2 passing (38s)
[chrome #0-0]

Wrote json report to [./Reports].


2 passing (40.90s)

Vlad GURDIGA

unread,
Jan 18, 2018, 1:07:15 PM1/18/18
to Mocha
If you want it to be executed only once, before all the tests, you just replace the beforeEach with before. 🤓

But if you really want it to be executed before every test, then yeah, this it is expected to see it more than once in the test report. 🤔

Ian Christopher Oroceo

unread,
Jan 18, 2018, 1:22:58 PM1/18/18
to moc...@googlegroups.com
hmmmm 🤔 ok, I have to look into further. so now it will show 3 results for each test cases 1 for login, 1 for the feature that is being tests and 1 for logout :|

now sometimes I have a spec.js that has multiple test
describe('The app', function() {
beforeEach(function(login) {  
});
 
    afterEach(function(logout) {
});
it('does the feature1 right', function() {
// assertions
});
  
    it('does the feature2 right', function() {
// assertions
});
  
    it('does the feature3 right', function() {
// assertions
});
});

this will now show 9 test passed on the report :|

I'm going to post this on the mocha gitter and see the opinions of other people :) 


On Thu, Jan 18, 2018 at 10:07 AM, Vlad GURDIGA <gur...@gmail.com> wrote:
If you want it to be executed only once, before all the tests, you just replace the beforeEach with before. 🤓

But if you really want it to be executed before every test, then yeah, this it is expected to see it more than once in the test report. 🤔

--

Ian Oroceo

unread,
Jan 18, 2018, 9:39:56 PM1/18/18
to Mocha
Ok, i'm doing this wrong LOL!

so instead of calling the spec for login and logout, I just added it on the pageobjects and added a method for login and logout that is being called by the other spec files.

I feel dumb hahahaa! this is what I get from shifting from dev to business and management and to qa engineer LOL!

Thanks, Vlad again for all of the help!
Reply all
Reply to author
Forward
0 new messages