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

Fails when testing modules that overrides require.extensions #281

Closed
facundocabrera opened this issue Feb 27, 2015 · 10 comments
Closed

Fails when testing modules that overrides require.extensions #281

facundocabrera opened this issue Feb 27, 2015 · 10 comments

Comments

@facundocabrera
Copy link

Hey all,

I'm trying to use Jest in replacement of plain jasmine for a small project and seems it's not playing well with code that uses require.extensions. I know it's deprecated, but the module node-jsx stills uses it internally https://github.com/petehunt/node-jsx/blob/master/index.js#L16

Any suggestion?

Thanks!

@cpojer
Copy link
Member

cpojer commented Oct 23, 2015

Unfortunately we won't be able to support this natively anytime soon. I recommend moving away from require.extensions and using a preprocessor to do any kind of extra processing.

@kentcdodds
Copy link
Contributor

I'm going to have to resurrect this and ask that you'd at least consider a pull request to make this happen. express-enrouten uses this as does babel-register.

In my particular case (with express-enrouten), all I need is for require.extensions to be: { js: function() { } }. It doesn't even need to actually be anything special. So even if I could stub this somehow myself, that'd be enough for me. Any ideas?

@cpojer
Copy link
Member

cpojer commented Nov 4, 2016

The next version of Jest supports a "transforms" configuration option that you can use to run custom preprocessors on your files and wrap around babel-jest. That should also solve your problem.

@kentcdodds
Copy link
Contributor

kentcdodds commented Nov 4, 2016

Awesome! So would this be correct?

const path = require('path')

module.exports = {
  process(src, filename, config, options) {
    return `
// a jest transform inserted this code:
require.extensions = [{js: function() {}}]
// end jest transform
${src}
    `
  },
  getCacheKey(fileData, filename, configString, options) {
    return filename
  },
}

And then configure it like so:

{
  "transform": {
    "regex-that-matches-filename": "<rootDir>/path-to-transformer.js"
  }
}

Is that correct?

Second question: How can I help get this released? This and #2048 are the only things left for my mocha to jest migration of my project's server tests. It's so close I can taste it.

@supasate
Copy link

I have the same issue with require.extensions. I tried with the same approach with @kentcdodds. My test file is transformed, however, the module that my test file depending on (in node_modules) is not transformed and that module uses require.extensions.

Is there any method to transform the code in node_modules? or did I do something wrong?

The followings are my config and preprocessor file:
My package.json

  "jest": {
    "verbose": true,
    "testEnvironment": "node",
    "transform": {
      "^.+\\.js$": "<rootDir>/preprocessor.js"
    }
  }

My preprocessor.js

module.exports = {
  process(src, path) {
    return `
require.extensions = [{ '.js': function () {} }]
${src}`
  },
}

@thymikee
Copy link
Collaborator

@supasate
Copy link

It works! My bad that I overlooked that option.
Thank you @thymikee!

@theoutlander
Copy link

@kentcdodds did you ever solve this issue? I just added server tests and noticed that enrouten is having issues around require.extensions. I tried modifying it before the test runs, but I think jest is overriding it or using mocks.

@kentcdodds
Copy link
Contributor

Yes. My problem was also express-enrouten. The workaround I tried above worked. Here's what it looks like currently:

// this is here because enrouten uses require.extensions (something that was deprecated in Node v0.10.0) and Jest
// doesn't support that API. Basically this just sticks a mock to the top of the relevant file.
module.exports = {
  process(src, filename, config, options) {
    return `
// a jest transform inserted this code:
require.extensions = {'.js': function() {}};
// end jest transform
${src}
    `
  },
}

Then in my jest config:

"transform": {
  // other stuff
  "express-enrouten\\/lib\\/directory\\.js$":  "<rootDir>/tests/lib/enrouten-require-extensions-transform.js"
},

Good luck. 👍

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 13, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants