-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
Comments
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. |
I'm going to have to resurrect this and ask that you'd at least consider a pull request to make this happen. In my particular case (with |
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. |
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. |
I have the same issue with Is there any method to transform the code in The followings are my config and preprocessor file: "jest": {
"verbose": true,
"testEnvironment": "node",
"transform": {
"^.+\\.js$": "<rootDir>/preprocessor.js"
}
} My module.exports = {
process(src, path) {
return `
require.extensions = [{ '.js': function () {} }]
${src}`
},
} |
@supasate you can make use of |
It works! My bad that I overlooked that option. |
@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. |
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. 👍 |
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. |
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 modulenode-jsx
stills uses it internally https://github.com/petehunt/node-jsx/blob/master/index.js#L16Any suggestion?
Thanks!
The text was updated successfully, but these errors were encountered: