Calling shared tests does not honor before/after hooks
See original GitHub issuePrerequisites
- Checked that your issue hasn’t already been filed by cross-referencing issues with the
faq
label - Checked next-gen ES issues and syntax problems by using the same environment and/or transpiler configuration without Mocha to ensure it isn’t just a feature that actually isn’t supported in the environment in question or a bug in your code.
- ‘Smoke tested’ the code to be tested by running it outside the real test suite to get a better sense of whether the problem is in the code under test, your usage of Mocha, or Mocha itself
- Ensured that there is no discrepancy between the locally and globally installed versions of Mocha. You can find them with:
node node_modules/.bin/mocha --version
(Local) andmocha --version
(Global). We recommend that you not install Mocha globally.
Description
Steps to Reproduce
/* eslint-env mocha */
const assert = require('assert');
const genVar = () => new Promise((resolve) => {
setTimeout(resolve(1), 500);
});
const sharedTest = (i) => {
it('should be 1', () => {
assert.equal(i, 1);
});
};
describe('shared', () => {
let i = 0;
before(async () => {
i = await genVar();
});
it('should be 1', () => {
assert.equal(i, 1);
});
sharedTest(i); // failed because i == 0
after(() => {
i = 2;
});
});
Expected behavior: both tests passed
Actual behavior: [What actually happens]
sharedTest(i) would failed.
Reproduces how often: [What percentage of the time does it reproduce?] 100%
Versions
- The output of
mocha --version
andnode node_modules/.bin/mocha --version
: 6.2.0 - The output of
node --version
: v10.16.2 - Your operating system
- name and version: manjaro
- architecture (32 or 64-bit): 64
- Your shell (e.g., bash, zsh, PowerShell, cmd): zsh
- Your browser and version (if running browser tests):
- Any third-party Mocha-related modules (and their versions):
- Any code transpiler (e.g., TypeScript, CoffeeScript, Babel) being used (and its version):
Additional Information
I was tring to implement some Shared Behaviours
in https://github.com/mochajs/mocha/wiki/Shared-Behaviours to reuse some test cases. But I got some async call in my before hook, calling the shared test would not wait the promise in before hook to resolve.
Wrapping sharedTest(i);
in another it
would cause other problems.
If calling paramized test cases would always fail, how to do it correctly?
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (1 by maintainers)
Top Results From Across the Web
before and after global hooks? Concurrently testing a shared ...
I'm doing some database testing & I have multiple test files. When all the tests complete, I'd like to drop the table. The...
Read more >Before and After Suite execution hook in jUnit 4.x
I've tried using maven's pre- & post-integration-test phases, but, if a test fails, maven stops and doesn't run post-integration-test , which is no...
Read more >Avoid Nesting when you're Testing - Kent C. Dodds
Why using hooks like beforeEach as a mechanism for code reuse leads to unmaintainable tests and how to avoid it.
Read more >Spock Framework Reference Documentation
Spock is a testing and specification framework for Java and Groovy applications. What makes it stand out from the crowd is its beautiful...
Read more >A Complete Guide to Testing React Hooks - Toptal
This is because the state update in useStaleRefresh hook happens outside act(). To make sure DOM updates are processed timely, React recommends you...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@DrizzlingCattus Thanks for your comment. The
this
context matters. So I have to write the tests in this way:Instead of passing test target as an argument to shared test, we should use
this
. Well, it works, all tests pass, but the code just looks from the es5 dynasty.here is a test case for passing arguments to shared tests.
this.x
works as expected. But thelocal x
worksinteresting
.