rules_jest icon indicating copy to clipboard operation
rules_jest copied to clipboard

[Bug]: ability to properly set the `snapshotResolver` under rules_jest

Open Aghassi opened this issue 1 year ago • 1 comments

What happened?

When passing a config that expects the ability to import relative to the root dir (root dir being where the package.json is) this modulePaths override is ignored due to rules_jest setting the rootDir relative to the target directory.

To fix this, I have added this workaround locally

jest.config

if (!process.env.ROOT_DIR) {
  process.env.ROOT_DIR = '<rootDir>'
}

//...

export default {
  modulePaths: [process.env.ROOT_DIR],

BUILD.bazel

        # Add the below to the jest_test rule
        env = {
            # rules_jest roots the resolver in the target folder as oppose
            # To the top level project folder. This is a hack to reposition
            # the root directory so the jest module resolver can find
            # other top level folders like "routes/"
            "ROOT_DIR": "%s/web/web-app/app" % path_to_workspace_root(),
        },

Version

Development (host) and target OS/architectures:

Output of bazel --version: bazel 6.0.0-pre.20221020.1

Version of the Aspect rules, or other relevant rules from your WORKSPACE or MODULE.bazel file: HEAD

Language(s) and/or frameworks involved:Typescript/SWC

How to reproduce

Have a jest.config with the following field


modulePaths: ['<rootDir'>]


and pass it into the `jest_test` as the `config`

In a test, have a file that `imports` something relative to the root directory like `import {thing} from 'path/to/thing'` where `path/to/thing` is accessible from the root (where the package.json and jest config reside)

Any other information?

No response

Fund our work

  • [ ] Sponsor our open source work by donating a bug bounty

Aghassi avatar Dec 01 '22 21:12 Aghassi