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

Use globally defined setImmediate on server side for test suites like jest that have mock timers #337

Merged
merged 3 commits into from
Jan 17, 2018

Conversation

robframpton
Copy link

@robframpton robframpton commented Jan 16, 2018

Jest leverages Node's setImmediate function with it's mock timers. After updating metal-clay-components to metal 2.16.0, there were a few test failures due to this.

My proposal is that we use the globally defined setImmediate only when it's defined on the server.

The reason for not using Metal's isServerSide method here, is that it also checks if the NODE_ENV variable is equal to "test", which is the case in metal-clay-components and other projects which use jest.

@robframpton
Copy link
Author

Tests are not running due to TravisCI outage https://www.traviscistatus.com/incidents/c1s4dlyxd4lf

@jbalsas
Copy link
Contributor

jbalsas commented Jan 17, 2018

Re-ran the tests and all seems good...

Why not updating isServerSide to either remove the NODE_ENV check or provide some variant? That way we don't need to repeat this logic here and remember what it means...

@robframpton
Copy link
Author

robframpton commented Jan 17, 2018

Hey @jbalsas, makes sense.

Before I bother writing tests and pushing up, how does this look to you?

export function isServerSide(checkEnv = true) {
	let serverSide = typeof process !== 'undefined' && !process.browser;
	if (serverSide && checkEnv) {
		serverSide =
			typeof process.env !== 'undefined' &&
			process.env.NODE_ENV !== 'test';
	}
	return serverSide;
}

Then in the case of nextTick, we would call isServerSide(false).

@jbalsas
Copy link
Contributor

jbalsas commented Jan 17, 2018

Hey @Robert-Frampton, yeah that should be fine, I'm just not a huge fan of Boolean Traps... it makes it hard to read the user code when you see isServerSide(true) you're pretty much always forced to look at the implementation or at least the method signature...

I know it's more verbose, but maybe better to have something like:

export function isServerSide(options = {checkEnv: true}) {
    console.log(options.checkEnv);
}

That could be called like:

isServerSide({checkEnv: false});

const checkEnv = false;
isServerSide({checkEnv});

@robframpton
Copy link
Author

Sure let's do it that way.

@robframpton robframpton merged commit 98137d3 into metal:develop Jan 17, 2018
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.

2 participants