rushstack icon indicating copy to clipboard operation
rushstack copied to clipboard

[heft] jest-build-transform does not support sources outside of `/src`

Open MichaelSitter opened this issue 3 years ago • 6 comments

Summary

jest-build-transform does not support sources outside of /src, looks like its enforced here:

https://github.com/microsoft/rushstack/blob/master/heft-plugins/heft-jest-plugin/src/transformers/BuildTransformer.ts#L353

I'm unclear why this limitation is enforced by the plugin, so I don't know if this is a feature or a bug. Is there a reason why src is checked this way?

Repro steps

My rush package is structured with the sources all living under the /modules directory instead of /src. Note that if the /src directory doesn't exist the runner will fail before it even detects the project specs. This is b/c the roots config from @rushstack/heft-jest-plugin/includes/jest-shared.config.json gets merged, instead of overridden.

{
  "extends": "@rushstack/heft-jest-plugin/includes/jest-shared.config.json",
  "clearMocks": true,
  "testMatch": ["<rootDir>/modules/**/*.test.[jt]s?(x)"],
  "roots": ["<rootDir>/modules"],
  "setupFiles": ["./testEnv.js"],
  "testEnvironment": "jest-environment-node"
}

Expected result:

Should run jest tests for any specs which match the jest config.

Actual result:

 FAIL  modules/auth/__tests__/middleware.test.ts
  ● Test suite failed to run

    jest-build-transform: The input path is not under the "src" folder:
    /Users/michaelsitter/xxxxxxx/modules/auth/__tests__/middleware.test.ts

      at BuildTransformer.process (../../common/temp/node_modules/.pnpm/@[email protected]_@[email protected]/node_modules/@rushstack/heft-jest-plugin/src/transformers/BuildTransformer.ts:388:13)
      at ScriptTransformer.transformSource (../../common/temp/node_modules/.pnpm/@[email protected]/node_modules/@jest/transform/build/ScriptTransformer.js:620:31)
      at ScriptTransformer._transformAndBuildScript (../../common/temp/node_modules/.pnpm/@[email protected]/node_modules/@jest/transform/build/ScriptTransformer.js:766:40)
      at ScriptTransformer.transform (../../common/temp/node_modules/.pnpm/@[email protected]/node_modules/@jest/transform/build/ScriptTransformer.js:823:19)

Standard questions

Please answer these questions to help us investigate your issue more quickly:

Question Answer
@rushstack/heft version? 5.56.0
Operating system? Mac
Would you consider contributing a PR? Yes
Node.js version (node -v)? v14.17.5

MichaelSitter avatar Jan 05 '22 00:01 MichaelSitter

Just wondering if I can get some direction as to wether this is a bug or a feature enhancement. I'm happy to carve out some time to make a contribution, it's just not clear from the plugin documentation if this is intended to work like this.

MichaelSitter avatar Jan 13 '22 18:01 MichaelSitter

My company has worked with a modified version of the plugin for six months with this change. We wanted to have a separate "deploy" folder for all our cdk code. It requires changes in 3 places in BuildTransform.ts and then you can use jestOptions.roots.

ErikMikkelson avatar Jun 03 '22 07:06 ErikMikkelson

@MichaelSitter we are also getting the following error when we try to use typegoose, mongoose or mongodb packages in any of our rushjs packages when building with heft-jest:

jest-build-transform: The input path is not under the "src" folder:
    /<path-to-repo>/common/temp/node_modules/.pnpm/[email protected]/node_modules/mongodb/src/bson.ts

FullStackSteve avatar Jun 28 '22 12:06 FullStackSteve

My company has worked with a modified version of the plugin for six months with this change. We wanted to have a separate "deploy" folder for all our cdk code. It requires changes in 3 places in BuildTransform.ts and then you can use jestOptions.roots.

@ErikMikkelson would it be possible to share the steps required to create a modified plugin?

FullStackSteve avatar Jun 28 '22 12:06 FullStackSteve

@FullStackSteve afaik I linked to the line you'd need to modify to support other paths in the issue report. I was hoping someone from the maintainer team would comment if this was an intentional choice or just a limitation of the current implementation.

MichaelSitter avatar Jul 21 '22 21:07 MichaelSitter

@FullStackSteve https://gist.github.com/ErikMikkelson/1ba9d39e46d4479364a3b52d7aad9627

ErikMikkelson avatar Aug 30 '22 15:08 ErikMikkelson

FWIW, I think the current implementation is just a safety precaution. Commonly, for web-based outputs, you'll configure typescript to compile /src to /lib and /lib-commonjs (/lib will contain your ESM output, /lib-commonjs will contain a second copy emitted as CommonJS).

If you have multiple source root folders set up, you would want to make sure that they get mapped to identical subpaths within both of those output folders, and that they wouldn't conflict with each other (e.g. src/index.ts and modules/index.ts, if both are inputs).

It looks like your solution is to treat the <rootDir> as the build root, so you'd have /lib/src, /lib/modules, /lib-commonjs/src, /lib-commonjs/modules, etc. -- this still seems pretty safe to me, although you might need to also adjust your webpack etc. config to compensate.

elliot-nelson avatar Nov 27 '22 20:11 elliot-nelson

This issue seems to be superseded by the changes made to @rushstack/heft-jest-plugin by @dmichon-msft in our move to the updated version of Heft, so I'm closing this out. Please re-open if this is still an issue.

D4N14L avatar Sep 07 '23 18:09 D4N14L