The issue is caused by the rule: jest-cli
No, it isn't.
jest-resolve can't resolve files compiled by ts_library.
Maybe this is related to #1408 and #1385 but I'm not sure.
Clone: https://github.com/AnatoleLucet/issue-bazel-jest
And do a bazel run //src:test
Executing tests from //src:test
-----------------------------------------------------------------------------
FAIL src/myModule.test.js
โ Test suite failed to run
Cannot find module 'issue_bazel_jest/src/myModule' from 'myModule.test.js'
at Resolver.resolveModule (../npm/node_modules/jest-resolve/build/index.js:259:17)
at ../../myModule.test.ts:1:1
at src/myModule.test.js:3:13
at Object. (src/myModule.test.js:8:3)
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 0.815s
Ran all test suites within paths "src/myModule.js", "src/myModule.test.js".
## ๐ Your Environment
**Operating System:**
Distributor ID: LinuxMint
Release: 19.2
Output of bazel version:
Build label: 0.29.1
Build target: bazel-out/k8-opt/bin/src/main/java/com/google/devtools/build/lib/bazel/BazelServer_deploy.jar
Build time: Tue Sep 10 13:44:39 2019 (1568123079)
Build timestamp: 1568123079
Build timestamp as int: 1568123079
Rules version (SHA):
http_archive(
name = "build_bazel_rules_nodejs",
sha256 = "16fc00ab0d1e538e88f084272316c0693a2e9007d64f45529b82f6230aedb073",
urls = ["https://github.com/bazelbuild/rules_nodejs/releases/download/0.42.2/rules_nodejs-0.42.2.tar.gz"],
)
You need the latest release 0.42.3 to get the "linker" to run inside jest_test - this should create $pwd/node_modules/issue_bazel_jest directory for the jest-resolve plugin to find.
I did that update in your repro and run with --define=VERBOSE_LOGS=1 to get more info:
==================== Test output for //src:test:
[link_node_modules.js] module manifest: workspace issue_bazel_jest, bin bazel-out/darwin-fastbuild/bin, root npm/node_modules with first-party packages
{ issue_bazel_jest: [ 'bin', 'issue_bazel_jest' ] }
[link_node_modules.js] found legacy_external_runfiles, switching root to external/npm/node_modules
[link_node_modules.js] resolved root npm/node_modules to external/npm/node_modules
[link_node_modules.js] cwd /private/var/tmp/_bazel_alexeagle/05143d19e34b69a608c14aa921f8d324/execroot/issue_bazel_jest/bazel-out/darwin-fastbuild/bin/src/test.sh.runfiles/issue_bazel_jest
[link_node_modules.js] symlink( node_modules -> external/npm/node_modules )
[link_node_modules.js] grouped mappings [[{"name":"issue_bazel_jest","root":"bin","modulePath":"issue_bazel_jest"}]]
[link_node_modules.js] symlink( issue_bazel_jest -> /private/var/tmp/_bazel_alexeagle/05143d19e34b69a608c14aa921f8d324/execroot/issue_bazel_jest/bazel-out/darwin-fastbuild/bin/src/test.sh.runfiles/issue_bazel_jest/bazel-out/darwin-fastbuild/bin )
bazel node patches enabled. root: /private/var/tmp/_bazel_alexeagle/05143d19e34b69a608c14aa921f8d324/execroot/issue_bazel_jest/bazel-out/darwin-fastbuild/bin/src/test.sh.runfiles symlinks in this directory will not escape
FAIL src/myModule.test.js
โ Test suite failed to run
Cannot find module 'issue_bazel_jest/src/myModule' from 'myModule.test.js'
at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:259:17)
at ../../myModule.test.ts:1:1
at src/myModule.test.js:3:13
at Object.<anonymous> (src/myModule.test.js:8:3)
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 1.571s
Ran all test suites within paths "src/myModule.js", "src/myModule.test.js".
================================================================================
So that's disappointing; I expected that would fix it.
Strange, I've also tried it on my side but the rule doesn't seems to create a $pwd/node_modules/issue_bazel_jest folder. I've also checked in the cache but I can't find it.
Is it the same for you?
in this case $pwd is in the runfiles - see the path printed by the linker, in my case it was /private/var/tmp/_bazel_alexeagle/05143d19e34b69a608c14aa921f8d324/execroot/issue_bazel_jest/bazel-out/darwin-fastbuild/bin/src/test.sh.runfiles
What you need to do is put your workspace name in moduleNameMapper in your jest config, resolve it to the <rootDir>, and then it'll most likely work.
Example: https://github.com/marcus-sa/enterprise-fullstack-starter/blob/master/jest.config.js#L9
Related to https://github.com/bazelbuild/rules_nodejs/issues/1403
@marcus-sa I've tried that. It return me this exception:
Executing tests from //src:test
-----------------------------------------------------------------------------
FAIL src/myModule.test.js
โ Test suite failed to run
Configuration error:
Could not locate module issue_bazel_jest/src/myModule mapped as:
/home/anatole/.cache/bazel/_bazel_anatole/877c8a3378006f03794d52d3d67f7256/execroot/issue_bazel_jest/bazel-out/k8-fastbuild/bin/src/test.sh.runfiles/issue_bazel_jest.
Please check your configuration for these entries:
{
"moduleNameMapper": {
"/^issue_bazel_jest\/(.*)$/": "/home/anatole/.cache/bazel/_bazel_anatole/877c8a3378006f03794d52d3d67f7256/execroot/issue_bazel_jest/bazel-out/k8-fastbuild/bin/src/test.sh.runfiles/issue_bazel_jest"
},
"resolver": null
}
at createNoMappedModuleFoundError (node_modules/jest-resolve/build/index.js:501:17)
at ../../myModule.test.ts:1:1
at src/myModule.test.js:3:13
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 0.907s
Ran all test suites within paths "src/myModule.js", "src/myModule.test.js".
With this in my config file:
moduleNameMapper: {
'^issue_bazel_jest/(.*)$': '<rootDir>'
}
I assume jest-cli is already doing this.
@AnatoleLucet you need to provide it with <rootDir>/$1 and not <rootDir> my bad.
This has nothing to do with Jest, but the way the module resolution works with nodejs_binary as it's kinda broken because of https://github.com/bazelbuild/rules_nodejs/issues/1403
I've had to patch every package that's being used as an entry point for nodejs_binary to remove the duplicate workspace name.
That worked. Thanks you! So in the future there will be no more need to add the moduleNameMapper in the config?
@AnatoleLucet if you use module_name in your ts_library you'd still have to do the same, because the module mapped paths doesn't get resolved to their prior path at compile time.
Here's an example of what you'd have to do to make it work:
module.exports = {
testEnvironment: 'node',
testMatch: ['**/*.spec.js'],
collectCoverage: true,
moduleNameMapper: {
'@sa/prisma/(.*)': '<rootDir>/libs/prisma/$1.js',
'@sa/prisma': '<rootDir>/libs/prisma/index.js',
'sa_dashboard/(.*)': '<rootDir>/$1',
},
};