question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Multiple Jest projects bleed module file extension resolution between environments

See original GitHub issue

Do you want to request a feature or report a bug?

Bug

What is the current behavior?

I’ve created a test-case repo that illustrates this behavior: paularmstrong/jest-project-bleed.


In some cases, we want to have code that is defined differently for a node.js environment than a browser/DOM environment. We do this to avoid needing to polyfill node.js’s url module in the browser when we already have access to a powerful window.URL API in a browser (which is not available in node.js).

To do this, we set up our build environment to build server-side code with moduleFileExtensions to resolve *.node.js files before *.js files.

In Jest, we create two separate project configurations: config/jest/node.js and config/jest/web.js. These run the tests in the appropriate environment (node and jsdom, respectively).

If the current behavior is a bug, please provide the steps to reproduce and either a repl.it demo through https://repl.it/languages/jest or a minimal repository on GitHub that we can yarn install and yarn test.

I’ve created a test-case repo that illustrates this behavior: paularmstrong/jest-project-bleed.

What is the expected behavior?

I expect all tests to pass, given their environment and correct module file resolution.

Please provide your exact Jest configuration and mention your Jest, node, yarn/npm version and operating system.

Please see the repo test case: paularmstrong/jest-project-bleed.

  • yarn 0.27.5
  • node 8.9.0
  "devDependencies": {
    "babel-core": "^6.26.0",
    "babel-jest": "^22.2.2",
    "babel-preset-env": "^1.6.1",
    "jest": "^22.3.0"
  },

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:15 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
cbelsolecommented, Mar 22, 2018

I was able to repro this bug and I noticed when I printed out the project configs that they had the same md5 hash name which is what jest uses to pick out the right resolver. I found that in the normalizer if the name is not set it will set a name.

After fiddling around with the normalizer I came up with this code:

const normalizeMissingOptions = (options: InitialOptions): InitialOptions => {
  if (!options.name) {
    options.name = crypto
      .createHash('md5')
      .update(options.rootDir)
      .digest('hex');
  }

  options.projects = (options.projects || []).map(project => {
    if (!project.name) {
      project.name =
        crypto
          .createHash('md5')
          .update(project.rootDir)
          .digest('hex') || '';
    }
    return project;
  });

  if (!options.setupFiles) {
    options.setupFiles = [];
  }

  return options;
};

Now if the names on the projects were not set they would have their own unique name. This fixed the error since by having a unique name the right resolver was chosen.

1reaction
cpojercommented, Feb 17, 2018

Yeah that’s a bug.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Configuring Jest
A global setup module configured in a project (using multi-project runner) will be triggered only when you run at least one test from...
Read more >
Importing pure ESM module in TS project fails Jest test with ...
Trying to use the file-type module which is pure ESM in a TS project but my jest fails. I have set the ESM...
Read more >
Frontend testing standards and style guidelines - GitLab Docs
Manual mocks are used to mock modules across the entire Jest environment. This is a very powerful testing tool that helps simplify unit...
Read more >
Configuring Jest compiled - w3resource
In this tutorial we will focus on configuring Jest. Jest's configuration can be defined inside the package.json file of your project, ...
Read more >
ECMAScript Modules - webpack
The export keyword allows to expose things from an ESM to other modules: ... Node.js established a way of explicitly setting the module...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found