Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

node_jest_test: Couldn't find preset "es2015" relative to directory #4

Closed
gfl-chris opened this issue Mar 21, 2019 · 6 comments
Closed

Comments

@gfl-chris
Copy link

I'm trying to use node_jest_test (from the jest_rules branch) to run our test suite. Here's the rule in our BUILD file (some names changed to protect the innocent):

nodejs_jest_test(
    name="testsuite",
    srcs=[
      "//my_project/frontend:tests",
    ],
    data=[
        "//my_project/frontend:frontend",
        ":.babelrc",
        ":jest-test-env.js",
        ":package.json",
        ":webpack.babel.js",
    ],
    config=":jest.json",
    deps=[
        "@npm_my_project//node_modules/babel-jest:babel-jest__pkg",
        "@npm_my_project//node_modules/babel-preset-es2015:babel-preset-es2015__pkg",
        "@npm_my_project//node_modules/fs-extra:fs-extra__pkg",
        "@npm_my_project//node_modules/jest-cli:jest-cli__pkg",
    ],
)

where //my_project/frontend:tests is a glob for all *.test.js files and //my_project/frontend:frontend is a glob for all files. Here's my jest.json:

{
  "bail": false,
  "globals": {
    "window": true,
    "App": true
  },
  "setupFiles": [
    "<rootDir>/jest-test-env.js"
  ],
  "moduleFileExtensions": [
    "js",
    "json"
  ],
  "moduleDirectories": [
    "<rootDir>/../../external/npm_my_project/node_modules",
    "<rootDir>/external/npm_my_project/node_modules",
    "<rootDir>/my_project/frontend/modules",
    "my_project/frontend/modules",
    "<rootDir>/frontend/modules",
    "frontend/modules",
    "<rootDir>"
  ],
  "moduleNameMapper": {
    "\\.(css|scss)$": "<rootDir>/__mocks__/styleMock.js",
    "\\.(gif|ttf|eot|svg|png)$": "<rootDir>/__mocks__/fileMock.js"
  }
}

It tries to run the tests, but every test fails with the following trace:

FAIL frontend/modules/ncex_redux/reducers/tests/theme-reducer.test.js
  â Test suite failed to run

    Couldn't find preset "es2015" relative to directory ".../my_project"

      at ../../../../../../../../../../../../../../external/npm_my_project/node_modules/babel-core/lib/transformation/file/options/option-manager.js:293:19

I've tried a bunch of different version of jest and babel-jest (20.0.3 and 23.2.0), but no luck. Any suggestions? :|

@Globegitter
Copy link
Collaborator

@gfl-chris I ended up simplifying the jest_test rules for now and you can get them directly from master: https://github.com/ecosia/bazel_rules_nodejs_contrib/blob/master/internal/jest_node_test/defs.bzl

I might change them later on, but for now they have been working well for us. the second problem we have started running into is that the --runTestsByPath flag provided to jest itself is happy with symlinks but the rest of jest still is not. So to properly fix everyhing this PR jestjs/jest#7364 would ideally be pushed forward and merged.

So what we have done to workaround this issue is using this paclage: https://github.com/ds300/patch-package we are patching our jest via yarn. You can find the patches for jest 24.5.0 here: https://gist.github.com/Globegitter/ef72bba83e12a21480f33be22877a641

Also interestingly, I have not noticed the option moduleDirectories before and we are setting the modulePaths. But that alone still caused some issues so we are also setting the NODE_PATH env. But I will give moduleDirectories a try myself :)

The last thing with babel-jest/babel, to have babel work correctly with bazel you will need to configure it via the babel.config.js file and then require the plugins/presets in there, so e.g:

const presets = [require('@nuxtjs/babel-preset-app')];
const plugins = [require('@babel/plugin-proposal-object-rest-spread')];
module.exports = { presets, plugins };

That is because per default babel also uses its own require method so will otherwise not find the plugins/presets.

Hope that helps. Let me know if you still run into any issues.

@gfl-chris
Copy link
Author

  1. I switched my repository to master. 👍
  2. I added patch-package to my package.json. 👍
  3. I set my postinstall script to patch-package. 👍
  4. I put the patches in your gist in a patches directory. 👍

Unfortunately rules_nodejs's npm_install doesn't seem to run the postinstall script. 👎 How do you invoke patch-package?

@Globegitter
Copy link
Collaborator

Hmm, it should invoke them. We are using yarn_install and it is working fine. You can set quiet = False to the npm_install rule to see the full npm output. At least with yarn I can see:

...
$ patch-package --patch-dir=tools/patches
patch-package 6.0.5
Applying patches...
@jest/[email protected][email protected][email protected] ✔
Done in 23.85s.

Also you have tell bazel to provide the patch files to yarn/npm via

npm_install(
...
    data = [
      "//:tools/patches/jest-haste-map+24.5.0.patch",
      "//:tools/patches/jest-resolve+24.5.0.patch",
      "//:tools/patches/@jest+transform+24.5.0.patch",
    ],
)

in our postinstall we then have patch-package --patch-dir=tools/patches

@Globegitter
Copy link
Collaborator

@gfl-chris Any success with that?

@Globegitter
Copy link
Collaborator

We talked on Slack that the issue has been resolved now.

@Globegitter
Copy link
Collaborator

Here is an updated set of jest patches for 24.7.1 plus further patches needed for coverage support: https://gist.github.com/Globegitter/690dd1b97828a3927b436bc11211f139

purkhusid added a commit to purkhusid/rules_nodejs that referenced this issue Oct 15, 2019
This PR adds an example of how you might create a Jest rule

Unfortunately Jest does not handle symlinks very well so we have to add some patches so that it behaves correctly(See: jasongwartz/bazel_rules_nodejs_contrib#4 (comment))
alexeagle pushed a commit to purkhusid/rules_nodejs that referenced this issue Oct 22, 2019
This PR adds an example of how you might create a Jest rule

Unfortunately Jest does not handle symlinks very well so we have to add some patches so that it behaves correctly(See: jasongwartz/bazel_rules_nodejs_contrib#4 (comment))
alexeagle pushed a commit to bazel-contrib/rules_nodejs that referenced this issue Oct 23, 2019
* feat(examples): add Jest example

This PR adds an example of how you might create a Jest rule

Unfortunately Jest does not handle symlinks very well so we have to add some patches so that it behaves correctly(See: jasongwartz/bazel_rules_nodejs_contrib#4 (comment))

Ultimately we should be able to move these patches into bazel_require_script which globally patches symlink operations for all node programs run by Bazel.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants