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

fix(jest-utils,nx-plugin): Fix running jest config, test:cov nx configuration, and path resolution in tests #109

Merged
merged 8 commits into from
Aug 16, 2022

Conversation

luxaritas
Copy link
Member

Few things this addresses:

  • Jest requires ts-node to use ts configuration files. This is now added in the relevant generator
  • Jest currently forces ts configuration files to be compiled as CJS. This means we have to compile jest-utils as CJS, as otherwise we won't be able to import it (as you can't require ESM from CJS). See [Feature]: Support loading jest.config.ts as ESM jestjs/jest#13118
  • Only test was properly set up with nx (caching/dependencies/inputs/outputs), test:cov now is as well

@luxaritas
Copy link
Member Author

luxaritas commented Aug 12, 2022

As a note: I am up for discussion about whether we really want to require ts-node (ie, adding extra dependencies just for the config file). On a brief test, it does appear that we're getting proper autocomplete on JS files in VSC since it's able to pick up on the types from jest-utils, and we could just add // @ts-check to force the editor to report type errors

@luxaritas
Copy link
Member Author

After discussion, we've agreed that there's not really a benefit for adding ts-node - we'll stick with js configs and // @ts-check. I'll also probably work on #119 in this PR as well so that all the config changes are centralized

@luxaritas luxaritas changed the title fix(jest-utils,nx-plugin): Fix jest ts config and test:cov nx configuration fix(jest-utils,nx-plugin): Fix running jest config, test:cov nx configuration, and path resolution in tests Aug 12, 2022
@luxaritas
Copy link
Member Author

luxaritas commented Aug 12, 2022

So using a root config was a bust, but I realized something else - if we had something like

src/
  mod/
      mod.ts
  index.ts

and index.ts imported 'mod/mod', running a jest test importing index would throw a module resolution error. I had to explicitly set both the baseUrl in the spec tsconfig (so the TS compiler would see it) and the modulePaths property in the jest config (so that jest knows how to resolve it). The mechanism I'm using to resolve the tsconfig to look in for the baseUrl isn't perfectly robust vs how jest allows you to set configuration, but I think it's the best effort we can do.

@luxaritas luxaritas merged commit ba160c3 into main Aug 16, 2022
@luxaritas luxaritas deleted the fix/jest-ts-config branch August 16, 2022 15:51
@github-actions github-actions bot mentioned this pull request Aug 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants