Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to get test data in beforeEach() and afterEach() #797

Closed
jlipps opened this issue Apr 5, 2013 · 9 comments
Closed

how to get test data in beforeEach() and afterEach() #797

jlipps opened this issue Apr 5, 2013 · 9 comments

Comments

@jlipps
Copy link

jlipps commented Apr 5, 2013

I need to get the name of each test in beforeEach and the pass/fail status of each finished test in afterEach. I looked at using this.test, but from there the best thing I found was this.test.parent, which is the suite of tests. That's all well and good, but how do I know which of those tests is the one whose before/after block I'm in?

@jlipps
Copy link
Author

jlipps commented Apr 5, 2013

I saw #795, and that might help with beforeEach, but doesn't seem to address my need to know test status in afterEach.

@avaly
Copy link
Contributor

avaly commented Apr 7, 2013

You can achieve this using my proposed code change from #795. It works for afterEach also. You can write something like this:

beforeEach(function(){
    console.log('beforeEach', this.currentTest.title);
});
afterEach(function(){
    console.log('afterEach', this.currentTest.title, this.currentTest.state);
});

@jlipps
Copy link
Author

jlipps commented Apr 8, 2013

awesome, let's get it merged then!

@tj tj closed this as completed Jul 1, 2013
tj added a commit that referenced this issue Jul 1, 2013
Expose current test information to beforeEach/afterEach, fixes #794, #797
@adamduffy
Copy link

i would love to use this functionality but i am not sure what 'this' is in this context. in my tests, 'this' is undefined. is there a more thorough example?

edit: must have been in an update because it works now! thanks :)

edit: it works in the beforeEach and afterEach but not inside the test. it would be nice to set currentTest.myValue inside the test for the afterEach to observe.

@bebraw
Copy link

bebraw commented Apr 24, 2015

@adamduffy You have probably solved this by now but you need to be very careful about scope when dealing with this. If you have inner functions there (no fat arrow, no explicit bind), they will have their own scope. You can either bind the scope to parent or set something like var that = this at root scope.

@ohadschn
Copy link

ohadschn commented Jul 11, 2016

this.currentTest.state is great, but it's unclear exactly what its possible values are. A quick search through the code suggests it's either "failed" or "passed". Apparently it can also be undefined if a failure took place in one of the before or beforeEach hooks.

derwok added a commit to 4minitz/4minitz that referenced this issue Jan 28, 2018
@Visakeswaran
Copy link

How to get the state of previous test, in beforeEach hook?

@ORESoftware
Copy link

@Visakeswaran why would you want to do that? just curious? beforeEach only pertains to one test (the current test), not the previous test.

@lotosotol
Copy link

@Visakeswaran why would you want to do that? just curious? beforeEach only pertains to one test (the current test), not the previous test.

I need to retrieve the status of the whole suite before closing the browser either before the .quit() method and pass those to BrowserStack (which accepts passed or failed only). My understanding is this would be something we cannot achieve in Mocha yet?! I've tried using the this.*** but couldn't really find a proper solution. Any thoughts?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants