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

Preset #6538

Closed
artola opened this issue Jun 25, 2018 · 14 comments
Closed

Preset #6538

artola opened this issue Jun 25, 2018 · 14 comments

Comments

@artola
Copy link
Contributor

artola commented Jun 25, 2018

🐛 Bug Report

Documentations states:

A preset that is used as a base for Jest's configuration. A preset should point to an npm module that exports a jest-preset.json or jest-preset.js module at its top level.

See: https://facebook.github.io/jest/docs/en/configuration#preset-string

Then I would like to use an scope package as follow:

// jest.config.js

module.exports = {
  preset: '@xxx/rdk.jest-preset-proactive'
};

But I got this error:

● Validation Error:

  Preset @xxx/rdk.jest-preset-proactive not found.

  Configuration Documentation:
  https://facebook.github.io/jest/docs/configuration.html

As I understand the module resolution comes from: https://github.com/facebook/jest/blob/cb87c0a8536519a3ad2f89cfe357a0d2ae99d22c/packages/jest-config/src/normalize.js#L60

where it handles relative packages, but not node resolution.

Workaround:

module.exports = {
  preset: path.relative(__dirname, require.resolve('@xxx/rdk.jest-preset-proactive'))
};

Expected behavior

Try node resolution first, if it fails proceed with path normalisation.

@ranyitz
Copy link
Contributor

ranyitz commented Aug 15, 2018

@artola preset should support scoped packages.

The fact that require.resolve works for you suggest that you're pointing to an index.js file or the main field of your module while Jest is looking for jest-preset.js or jest-preset.json files on the root of the module.

Is this the case?

@artola
Copy link
Contributor Author

artola commented Aug 16, 2018

@ranyitz I do believe that the problem is the non documented constraint that presets need to be named following a convention @scope/jest-preset-foo then we could consume it as preset: "[scope]preset-foo". I would like to have more freedom, because as in our case we add some context to the name, e.g., @scope/xxx.jest-preset-foo. I think that makes sense to try first normal node resolution to import from any package making @jungle/monkey.banana a valid source. If this fails, proceed with the actual normalisation.

@ranyitz
Copy link
Contributor

ranyitz commented Aug 16, 2018

@artola you don't need to name your module specifically.

The following will work:

├── jest.config.js
├── node_modules
│   └── @jungle
│       └── monkey.banana
│           └── jest-preset.js
└── package.json

npm module that exports a jest-preset.json or jest-preset.js module at its top-level

Maybe we can change the description to:

"npm module that has jest-preset.json or jest-preset.js file at the root."

For example, foo-bar/jest-preset.js would be configured as follows:

{
  "preset": "foo-bar"
}

@SimenB
Copy link
Member

SimenB commented Aug 16, 2018

We should definitely improve both the docs and the error message in this case

@ranyitz
Copy link
Contributor

ranyitz commented Aug 16, 2018

@SimenB In case we've found a module without jest-preset.js/jest-preset.json file, we can say:

● Validation Error:

Preset "my-jest-preset" should have "jest-preset.js" or "jest-preset.json" file at the root.

@SimenB
Copy link
Member

SimenB commented Aug 16, 2018

Module "my-jest-preset" instead of Preset and I'm down with that 🙂

@artola
Copy link
Contributor Author

artola commented Aug 16, 2018

@SimenB @ranyitz Sorry, may be I did not explain it well. I would like to read our package publised in the scope @xxx and named rdk.jest-preset-proactive which main entry points to some js or json file containing the configuration. Just by doing the following:

// jest.config.js

module.exports = {
  preset: '@xxx/rdk.jest-preset-proactive'
};

Today it is not possible because of the rdk. prefix the package name.

Jest can manage something as jest-preset-proactive or for scoped @xxx/jest-preset-proactive.

In the meantime we have a workaround:

// jest.config.js

module.exports = require('@xxx/rdk.jest-preset-proactive');

This is the reason why I would prefer Node resolution before trying any kind of config lookup.

For me makes more sense to extend the use possibilities than just fix the documentation with a constraint.

@ranyitz
Copy link
Contributor

ranyitz commented Aug 16, 2018

@artola If you suggest to start and look for index.js first, then it will be a breaking change. Looking at jest-preset.js/jest-preset.json file is a feature and not just a constraint. It enables already working packages to expose jest-preset while reusing the same package.

For example, look at react-native's case - https://github.com/facebook/react-native/blob/master/jest-preset.json

@artola
Copy link
Contributor Author

artola commented Aug 16, 2018

@ranyitz @SimenB Just a comment, similar situation happens for Babel presets, with same reasoning shown above ... and now it is already in master for Babel 7, for details: babel/babel#8473

Very similar:

What this PR does is make it so that, if there is a scope and "babel-plugin" is anywhere in the package name (after the scope name anyway), we will skip inserting the prefix. See #8236 for an example usecase.

I find presets a great tool and we use them for everything (e.g. tslint, webpack, babel, neutrino and jest).
It would be great and very convenient to inline the tooling configurations (as much as doable).

@SimenB
Copy link
Member

SimenB commented Aug 18, 2018

We don't do anything with the package name, we just have expectations on the name of the file within that module

@artola
Copy link
Contributor Author

artola commented Aug 18, 2018

@SimenB @ranyitz Thanks a lot for your support. In short:

  1. my preset package works fine if I have jest-preset.js and consumed as preset: '@xxx/rdk.jest-preset-proactive'
  2. or it could have any name if consumed with a relative route preset: '../../node_modules/@xxx/rdk.jest-preset-proactive/index.js'.

For my actual use case, I will use option 1.

Closing this issue. Thanks again guys.

@artola artola closed this as completed Aug 18, 2018
@SimenB
Copy link
Member

SimenB commented Aug 18, 2018

@ranyitz I'd still love to see a PR for a better error and some clarifying docs :)

@kristianmandrup
Copy link

Please see this issue

@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 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants