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

jest resolver resolves to wrong file #15312

Closed
richard-ling opened this issue Feb 28, 2023 · 2 comments · Fixed by #15445
Closed

jest resolver resolves to wrong file #15312

richard-ling opened this issue Feb 28, 2023 · 2 comments · Fixed by #15445
Assignees
Labels
outdated scope: testing tools Issues related to Cypress / Jest / Playwright / Vitest support in Nx type: bug

Comments

@richard-ling
Copy link

richard-ling commented Feb 28, 2023

Current Behavior

When resolving path './foo.js', if './foo.js' does not exist but '../foo.js' exists, the resolver will incorrectly resolve to '../foo.js'.

Expected Behavior

resolver should be unable to resolve the file

GitHub Repo

https://github.com/nrwl/nx

Steps to Reproduce

  1. Usenpx create-nx-workspace@latest to create an angular standalone app called foo. Accept all defaults.
  2. In src/app/app.component.spec.ts, add a mock as follows
    jest.mock('./main', () => undefined);
  3. Try npx nx test foo

Expected result: test suite should fail to run, with an error Cannot find module './main' from 'src/app/app.component.spec.ts'

Actual result: The tests will pass. Tracing the code, the file found by '@nrwl/jest/plugins/resolver' is the existing file src/main.ts, i.e. ../main.ts.

Nx Report

>  NX   Report complete - copy this into the issue template

   Node : 16.14.0
   OS   : darwin x64
   npm  : 8.3.1
   
   nx                      : 15.7.2
   @nrwl/jest              : 15.7.2
   @nrwl/linter            : 15.7.2
   @nrwl/workspace         : 15.7.2
   @nrwl/angular           : 15.7.2
   @nrwl/cli               : 15.7.2
   @nrwl/cypress           : 15.7.2
   @nrwl/devkit            : 15.7.2
   @nrwl/eslint-plugin-nx  : 15.7.2
   @nrwl/js                : 15.7.2
   @nrwl/tao               : 15.7.2
   @nrwl/webpack           : 15.7.2
   @nrwl/nx-cloud          : 15.1.1
   typescript              : 4.8.4

Failure Logs

No response

Additional Information

This line in the jest resolver seems suspicious to me

    return ts.resolveModuleName(path, options.basedir, compilerOptions, host)
        .resolvedModule.resolvedFileName;

options.basedir has the value src/app which is a directory, but the receiving parameter in ts.resolveModuleName is named containingFile. If src/app is considered as a file, then src/main.ts would be a good resolution for ./main.ts.

Changing this line to

    return ts.resolveModuleName(path, options.basedir + '/some-file', compilerOptions, host)
        .resolvedModule.resolvedFileName;

gives the expected behaviour, `./main' cannot be resolved anymore.

@barbados-clemens barbados-clemens self-assigned this Feb 28, 2023
@barbados-clemens barbados-clemens added the scope: testing tools Issues related to Cypress / Jest / Playwright / Vitest support in Nx label Feb 28, 2023
@barbados-clemens
Copy link
Contributor

barbados-clemens commented Mar 4, 2023

hi @richard-ling thanks for the issue report,

I am seeing what you're saying the resolver is resolving to.

i.e.

{
  path: './main',
  baseDir: '/Users/caleb/Work/sandbox/jest-resolve-bug/src/app',
  resolved: '/Users/caleb/Work/sandbox/jest-resolve-bug/src/main.ts'
}

when I switch to options.rootDir I do get the error expected that the import doesn't exist.

error saying ./main doesn't exist

but I will say I am a bit confused as to why it was basedir instead of rootDir, as for nx the rootDir is always going to be the projectRoot is could be because I don't fully understand the finer details of jest + nx

since we do pass the rootDir for pulling the tsconfigs.

changing this value in the Nx repo doesn't seem to break anything, but there are a lot of various testing configurations out there that this could break for people so I'll need to test a lot more before agreeing to making this change.

edit: it seems like this is fine to do? still gotta test it all out.

https://github.com/microsoft/TypeScript/blob/43cc362cef72e5fa1372f59736a9c4b55d85def0/src/compiler/moduleNameResolver.ts#L1289

@github-actions
Copy link

github-actions bot commented May 7, 2023

This issue has been closed for more than 30 days. If this issue is still occuring, please open a new issue with more recent context.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 7, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated scope: testing tools Issues related to Cypress / Jest / Playwright / Vitest support in Nx type: bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants