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

Prefer capability sniffing to environment check? #161

Open
glebec opened this issue Mar 3, 2019 · 3 comments
Open

Prefer capability sniffing to environment check? #161

glebec opened this issue Mar 3, 2019 · 3 comments

Comments

@glebec
Copy link
Member

glebec commented Mar 3, 2019

As per #160, it is technically possible to run code with the environment set to test even if the code isn't actually being run by Mocha. This causes a reference error for code which attempts to use after.

While that isn't the way the codebase is intended to be used, one possible way of making the code more robust is to use capability sniffing:

-if (process.env.NODE_ENV === 'test') {
+if (typeof global.after === 'function') {
   after(...)
}

I'm not 100% sure I prefer this – it is safer, but perhaps less clear.

@collin
Copy link
Contributor

collin commented Apr 9, 2019

Maybe name it as a utility?

function processIsRunningInMochaTestRunner () {
  // Some nice prose description of feature detection
  return typeof global.after === 'function'
}
if (processIsRunningInMochaTestRunner()) {
  after(/* ... */)
}

@glebec
Copy link
Member Author

glebec commented Apr 9, 2019

Maybe. If we switched to e.g. Jest we would just change the name of the function, which isn't so onerous because for example Jest uses afterAll – so we'd have to change some code anyway. And I like the centralization this might yield, if we share the function in the multiple places we register after hooks.

@glebec
Copy link
Member Author

glebec commented Apr 9, 2019

Or perhaps we should be creating a resource cleanup service?

const resourceManager = require('../utils/resourceManager')

...

resourceManager.onComplete(() => db.close())

Then inside the manager, we would use the check:

if (typeof global.after === 'function') {
    after(() => manager.runCleanupHooks())
}

…something like that. This might be an unnecessary level of abstraction though… thinking about it.

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

No branches or pull requests

2 participants