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!
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. 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?
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 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}`
},
}
@supasate you can make use of transformIgnorePatterns. And another example from RN tutorial: http://facebook.github.io/jest/docs/tutorial-react-native.html#transformignorepatterns-customization
It works! My bad that I overlooked that option.
Thank you @thymikee!
@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. :+1:
Most helpful comment
I'm going to have to resurrect this and ask that you'd at least consider a pull request to make this happen.
express-enroutenuses this as doesbabel-register.In my particular case (with
express-enrouten), all I need is forrequire.extensionsto 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?