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

setupFilesAfterEnv and clearMocks support #25

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

jehy
Copy link

@jehy jehy commented Jan 30, 2020

This is a quick and a bit dirty solution for #24

It does not break anything but lets you use setupFilesAfterEnv and use clearMocks 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:

const sinon = require('sinon');
module.exports = {
	clearMocks: () => {
		sinon.sandbox.restore();
	}
};

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.

@@ -14,6 +15,11 @@ const runMocha = ({ config, testPath, globalConfig }, workerCallback) => {
const failures = [];
const passes = [];

runner.on('suite', () => {
if (clearMocks) {
Copy link
Collaborator

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?

Copy link
Author

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;
Copy link
Collaborator

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?

Copy link
Author

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.

Copy link
Collaborator

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.

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

Successfully merging this pull request may close these issues.

None yet

2 participants