Open hswolff opened 6 years ago
This is really interesting, when does purge
gets called in Mocha?
It's called here, when you're in 'watch' mode and you trigger a re-run of the mocha test suite.
I think this makes sense for me, but I would love to see what @ljharb thinks
I'm a bit confused; why would the setup files be run more than once per suite?
If you have logic you need run before all tests, your setup file would use a beforeAll
callback, no?
I believe beforeAll
will run before each describe
block. I can create a test case to verify this.
The behavior i want is to have my test harness code ran once before all tests and once after all tests. With beforeEach
I was seeing the code ran more than desired.
beforeAll
should run once, before the entire suite - not just before each describe
. beforeEach
of course runs before each it
.
However, with jest-runner-mocha, if there's N files, there's N suites - so beforeAll
would get run before each one (which might happen to have one describe per file). Since each file runs in its own process, this would be necessary for setting up polyfills/shims/mocks/etc.
If what you want is something that runs before/after the entire jest-run suite, I think that'd need to be a separate option unique to this runner.
Well explained!
Indeed, what I want is something ran before/after either every suite ran within an individual process, or even before/after an individual suite run.
I did some more debugging and it seems like the current state of code results in some unexpected behavior (at least to me).
With the addition of the cliOptions.file
option we can now include test harness code for mocha suites. i.e. I can have a testSafeguards.js
file passed into cliOptions.file
which contains:
before(function() {
// do stuff before all tests
console.log('before');
});
let count = 1;
beforeEach(function() {
// before each test
console.log('beforeEach', count++);
});
after(function() {
// after all tests
console.log('after');
});
When I run use this file and run it before two individual test files (each being a test suite with one top level describe block) I get:
When I include this proposed change to delete the nodejs cache this is the output I get from jest.
It appears that due to the test harness file testSafeguards.js
having been already required()
for the first test file, when the second one is require'd the testSafeguards.js
test hooks are not re-created as the file is already cached.
This change will resolve that issue and fix this issue.
Would love to get this included upstream!
Hello! It's me again :)
After #8 lands we'll have support for including test harness files for each individual process used to run tests. For my use cases these test harness files have some generic
before
andafter
handlers that I expect to run for each individual test file that I'm running.However I encountered an issue when attempting to integrate this runner with our tests at Mongo. When this runner spins up its test runner processes it recycles them for each individual test. Meaning that test A and test C are ran within process 1 and test B and test D are ran in process 2.
However the test harness files are only ran once, when the process is first created. This means that for test A and test B the
before
andafter
handlers are ran, however because test C and D are ran within the existing processes, and the test harness files are already cached, they do not get the test harness handlers.I have a solution that I'm using currently that I'd love to get upstreamed but it's a little more controversial IMO and wanted to discuss it first before opening a PR.
The solution is available for viewing here. The
clearModuleCache
is copy and pasted from mocha directly so it keeps parity there as well.Questions:
resetFile
? I'm not sure about this one. Naming is hard :(.