loopback icon indicating copy to clipboard operation
loopback copied to clipboard

Boot scripts are not loaded when testing with Jest

Open supasate opened this issue 9 years ago • 8 comments

Bug

When testing with Jest, boot scripts in the server/boot folder are not loaded.

Expected result

Boot scripts in the server/boot folder are loaded correctly when testing with Jest

Additional information

darwin x64 6.7.0 ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected]

supasate avatar Feb 14 '17 05:02 supasate

After looking into the code, it seems like the root cause is that the loopback-boot checks that file extensions must be specified in require.extensions(this line and this line). However, Jest doesn't set require.extensions. So, all files in the boot directory are ignored.

require.extensions is deprecated and Jest doesn't plan to support it. Is there any alternative method to work around this issue?

supasate avatar Feb 14 '17 06:02 supasate

[Update] I found a workaround by using transform and transformIgnorePatterns options as follows:

In my package.json,

  "jest": {
    "testEnvironment": "node",
    "transform": {
      "^.+\\.js$": "<rootDir>/preprocessor.js"
    },
    "transformIgnorePatterns": [
      "node_modules/(?!loopback-boot)"
    ]
  },

My preprocessor.js

module.exports = {
  process(src) {
    return `
// a jest transform inserted this code:
require.extensions = {
  '.js': function () {},
  '.json': function () {},
  '.node': function () {},
  '.ejs': function () {},
}
// end jest transform
${src}`
  },
}

However, I wonder should we rely on deprecated require.extensions?

supasate avatar Feb 14 '17 08:02 supasate

Hi @supasate

Thanks for the information. That's a nice catch. We can replace require.extensions with something that can go with node 4/6 for loopback 3. Would you like to contribute for the change?

siddhipai avatar Feb 23 '17 18:02 siddhipai

Hi @siddhipai

I would like to contribute, however, I'm not quite sure what's is the right way to deal with it yet.

Based on this statement from nodejs.org about require.extensions,

Deprecated In the past, this list has been used to load non-JavaScript modules into Node.js by compiling them on-demand. However, in practice, there are much better ways to do this, such as loading modules via some other Node.js program, or compiling them to JavaScript ahead of time.

without require.extensions, how do we know which extensions are supported in runtime by other programs (custom loaders)? Do you have any idea on this issue?

supasate avatar Feb 24 '17 17:02 supasate

  • [ ] Find out from @bajtos can we remove the dependency on require.extensions
  • [ ] Implement the fix/workaround
  • [ ] Add test cases
  • [ ] Backport

superkhau avatar Mar 06 '17 23:03 superkhau

how do we know which extensions are supported in runtime by other programs (custom loaders)?

This comment has nailed it down. loopback-boot uses require.extensions to figure out which file extensions to try when loading script files (server/boot/*, but also common/models/{model-name}).

can we remove the dependency on require.extensions

We cannot remove it, but we can replace it with a different alternative.

I don't know about any generic solution that would work as seamless as the current one.

The only option that comes to my mind is to add a new loopback-boot option that will specify list of extensions supported by the runtime/custom loaders, defaulting to Object.keys(require.extensions). That way:

  • current apps continue to work as before
  • apps using jest can explicitly provide the list of supported extensions and thus avoid dependency on require.extensions
// sample usage
boot(app, {
  appRootDir: __dirname,
  scriptExtensions: ['.js', '.json', '.node', '.ejs'],
}, cb);

bajtos avatar Mar 07 '17 14:03 bajtos

That sounds good to me. I'll explore the code and try with this method.

supasate avatar Mar 08 '17 00:03 supasate

@bajtos @superkhau I've created a PR at strongloop/loopback-boot#240 based on the comment.

supasate avatar Mar 08 '17 17:03 supasate