-
Notifications
You must be signed in to change notification settings - Fork 12
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
setupFilesAfterEnv and clearMocks support #25
base: master
Are you sure you want to change the base?
Conversation
@@ -14,6 +15,11 @@ const runMocha = ({ config, testPath, globalConfig }, workerCallback) => { | |||
const failures = []; | |||
const passes = []; | |||
|
|||
runner.on('suite', () => { | |||
if (clearMocks) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe only add the listener if clearMocks
is set?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When creating reporter, we don't know if clearMocks exists yet.
We can move creating a reporter to after requiring setupFilesAfterEnv - but that's not like it is implemented in other runners, and I'm not sure if it will be correct.
// eslint-disable-next-line global-require,import/no-dynamic-require | ||
const module = require(path); | ||
if (config.clearMocks && module.clearMocks) { | ||
clearMocks = module.clearMocks; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
doesn't this mean that the last setup file to set clearMocks will wipe out any set by the previous files?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Defining clearMocks export it several files hardly makes sense... But we can add a check and throw error if clearMocks is redefined.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alternatively, could we build up an array of functions, and call them all in order?
I think it makes sense that anything you can do in one file, you can refactor to do in multiple.
This is a quick and a bit dirty solution for #24
It does not break anything but lets you use
setupFilesAfterEnv
and useclearMocks
if it is enabled and exported from one of the setup files.For example, we use sinon but we can enjoy independent mocks with a simple clearMocks implementation:
I'm not sure if it will play as well with clearing jests's native mocks - but right now clearing native mocks does't work anyway.