Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

Update get-port to v5.1.0 #152

Closed
b1zzu opened this issue Jan 7, 2020 · 5 comments
Closed

Update get-port to v5.1.0 #152

b1zzu opened this issue Jan 7, 2020 · 5 comments
Labels
enhancement New feature or request

Comments

@b1zzu
Copy link
Contributor

b1zzu commented Jan 7, 2020

The new version of the get-port module has some braking changes that break the tests, we need to investigate these changes and fix them in order to update to the latest version.

#139

@b1zzu b1zzu added the enhancement New feature or request label Jan 7, 2020
@darahayes
Copy link
Contributor

Looks like the latest version of get-port is now using a setInterval call to create a timer. sindresorhus/get-port@v5.0.0...v5.1.0#diff-168726dbe96b3ce427e7fedce31bb0bc

At some point they call timer.unref.

This is a Node.js specific thing that is not typically available in browser environments. When running the code in Jest, some globals such as setInterval are replaced by the JSDOM implementation. That implementation does not include unref. See this issue here: jestjs/jest#1909 (comment)

@b1zzu
Copy link
Contributor Author

b1zzu commented Jan 8, 2020

I see, but I'm not sure that setting the testEnvironment to node is a good choice. Is there maybe another solution for this?

@darahayes
Copy link
Contributor

darahayes commented Jan 8, 2020

Yes the test that's failing is offline.test.ts which is testing some behaviours of Offix so presumably we cannot set testEnvironment to node. We can either:

If we look at the following documentation: https://jestjs.io/docs/en/configuration.html#testenvironment-string

They have some example code of how to create/extend an environment. We would follow that example but we would extend the https://www.npmjs.com/package/jest-environment-jsdom environment and then reassign all of the global timer functions like setInterval, setImmediate, setTimeout etc. It might be possible to reassign those using lolex which is already used internally by jest to mock timers for Node.

I think the overall solution would look something like this:

const JSDomEnvironment = require('jest-environment-jsdom');
const lolex = require('lolex');

class JSDomEnvironmentWithNodeTimers extends JSDomEnvironment {
  constructor(config, context) {
    super(config, context);
    this.testPath = context.testPath;
    this.docblockPragmas = context.docblockPragmas;
  }

  async setup() {
    await super.setup();
    this.clock = lolex.install(); // mocks all the global timers
  }

  async teardown() {
    this.clock.uninstall() // tear down the global timers
    await super.teardown();
  }

  runScript(script) {
    return super.runScript(script);
  }
}

module.exports = JSDomEnvironmentWithNodeTimers;

I'm not 100% sure if it will work but it might be worth trying?

@b1zzu
Copy link
Contributor Author

b1zzu commented Jan 8, 2020

  • Adding a new JSDomEnvironment is definitely overkilling for just an example, we need to keep it simple
  • I would like to avoid doing the same thing we are doing in karma because all the point of using jest was to not doing it like karma
  • Using something different from get-port could be a valid solution, also never updating it can be another solution 😅
  • Open the PR/Issue to get-port will be my preferred way, at least we should give it a try

wdyt?

@b1zzu
Copy link
Contributor Author

b1zzu commented Jan 8, 2020

@darahayes btw this is a low priority issue, the project is perfectly working with the old version

@b1zzu b1zzu closed this as completed Jan 15, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants