k-g-a / jest-node-exports-resolver

MIT License
25 stars 7 forks source link

Doesn't work for older versions of Jest #17

Open franklin-ross opened 2 years ago

franklin-ross commented 2 years ago

Ahoy, it looks like this change makes conditions a requirement, but that stops it working for older versions of Jest that don't provide it. Could we provide a default set of conditions where Jest doesn't pass any to at least try to do the right thing?

piranna commented 2 years ago

what changes do you suggest? There's no way to detect the actual platform to provide some safe default values...

franklin-ross commented 2 years ago

Yeah, fair enough. I guess I figured a best effort is better than nothing when conditions isn't provided? Otherwise, it straight up doesn't work for 100% of the cases prior to about 27.1, which is pretty recent. And considering this is about providing support for legacy versions of Jest (since it works out of the box for v28) that seems helpful?

Alternatively, is there a way to let users provide the conditions themselves in a config somewhere? I've done something like this as a custom wrapper, but I don't know how many people would dig into the code to work something like this out themselves.

const baseResolver = require('jest-node-exports-resolver')

const defaultConditions = ['require', 'node', 'default']

module.exports = (request, opts) => {
  let options = opts

  if (!options) {
    options = {
      conditions: defaultConditions
    }
  } else if (!options.conditions || options.conditions.length <= 0) {
    options.conditions = defaultConditions
  }

  return baseResolver(request, options)
}
franklin-ross commented 2 years ago

Or even just a handful of common pre-configured ones and a little documentation, like "for Node based tests, do 'resolver': 'jest-node-exports-resolver/node' or something. I dunno, just trying to think of ways to make it helpful and useful under more situations, even if it's not perfect or complete.