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

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

Open
1 task
Aghassi opened this issue Dec 1, 2022 · 1 comment
Open
1 task
Labels
bug Something isn't working
Milestone

Comments

@Aghassi
Copy link

Aghassi commented Dec 1, 2022

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 Aghassi added the bug Something isn't working label Dec 1, 2022
@gregmagolan gregmagolan added this to the 1.0 milestone Jan 4, 2023
@Aghassi Aghassi changed the title [Bug]: ability to properly set the rootDir under rules_jest [Bug]: ability to properly set the snapshotResolver under rules_jest Apr 26, 2023
@Aghassi
Copy link
Author

Aghassi commented Apr 26, 2023

I spoke with @gregmagolan about this and I actually think the problem I'm running into is how the snapshotResolver specifically is resolved. The logic resides here

if (updateSnapshots) {
if (config.snapshotResolver) {
const snapshotResolverPath = path.isAbsolute(config.snapshotResolver)
? config.snapshotResolver
: path.resolve(
_resolveExecrootPath(userConfigPath),
"..",
config.snapshotResolver
);
if (!existsSync(snapshotResolverPath)) {
throw new Error(
`configured snapshotResolver '${config.snapshotResolver}' not found at ${snapshotResolverPath}`
);
}
process.env.JEST_TEST__USER_SNAPSHOT_RESOLVER = snapshotResolverPath;
}
config.snapshotResolver = bazelSnapshotResolverPath;
}

The assumption is that the config will be relative to the snapshot resolver, but between bazel test and bazel run the path from which the runner gets the resolver file is different. The only guaranteed fix I think locally is to do path.resolve(__dirname, 'snapshotResolver.cjs') when passing in the custom snapshot resolver. However, I still have issues with the bazel run command as it just hangs. I assume there is an error not bubbling up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Status: 📋 Backlog
Development

No branches or pull requests

2 participants