-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Nested this Not Reset for Every Test #3109
Comments
Immediately after posting this, I found some other issues around I suppose the workaround is using variables defined within the describe block. This is how I'm used to working with Mocha since I was using it long before |
Closing as a duplicate of #2014, but feel free to weigh in there. I summarized my understanding in #2014 (comment) I don't know about the rest of the team, but I actually would like to see this "fixed" -- give (Additionally, one factor I'd like to look at is whether one can have per-test data/actions/resources that are set using suite-level data, i.e. using data that can be overridden in nested suites. For example, it would be neat if it were possible for a |
Prerequisites
common mistake
labelDoesn't appear to be any other issues like this, except this one.
Not using any of that fancy stuff.
Tested completely separate from rest of system.
node node_modules/.bin/mocha --version
(Local) andmocha --version
(Global). We recommend avoiding the use of globally installed Mocha.Don't have a globally installed version of mocha.
Description
When setting a value in an outer describe block on
this
in abeforeEach
, the value should be reset for all tests defined within this describe block and all nested describe blocks. However, this is not the case. Nested describe block maintain their own state. This is noticeable when attempting to append a string to the end of the value defined in the outerdescribe
block.Use Case: Appending a string to the end of a variable defined on
this
is extremely useful for testing, especially routes. For instance, if this worked, I could create tests for my API like so:Steps to Reproduce
Copy/paste the following code and run. 1 test (the last one) will fail.
Expected behavior: [What you expect to happen]
All tests should pass. This would mean the inner
this.myVar
is reset to equal the string"myVar"
(the value it is being set to in the outerbeforeEach
) before every test.Actual behavior: [What actually happens]
The last test is failing because
this.myVar
refers to the value ofthis.myVar
from the last test. I.e. mocha maintains state across tests 😱Reproduces how often: [What percentage of the time does it reproduce?] Every time
Versions
Shouldn't be necessary, but
mocha --version
: The program 'mocha' is currently not installed. You can install it by typing:sudo apt install mocha
node node_modules/.bin/mocha --version
: 4.0.1node --version
: 8.9.1The rest seems unnecessary.
Possible Solutions
Set
this
to an empty object ({}
) for every test.The text was updated successfully, but these errors were encountered: