Skip to content

Commit cc04f6c

Browse files
Lukas Holzeralexeagle
authored andcommitted
fix(examples): fix jest example on windows
Fixes #1454
1 parent 8e76536 commit cc04f6c

File tree

6 files changed

+30
-10
lines changed

6 files changed

+30
-10
lines changed

examples/jest/.bazelrc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
11
import %workspace%/../../common.bazelrc
2+
3+
# jest relies on the runfiles generation to find the correct files.
4+
# To work on windows we have to specify this flags.
5+
build --enable_runfiles
6+
run --enable_runfiles
7+
test --enable_runfiles

examples/jest/BUILD.bazel

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,6 @@ jest_test(
1919
] + glob(["__snapshots__/*.snap"]),
2020
jest_config = ":jest.config.js",
2121
tags = [
22-
# Need to set the pwd to avoid jest needing a runfiles helper
23-
# Windows users with permissions can use --enable_runfiles
24-
# to make this test work
25-
"no-bazelci-windows",
2622
# TODO: why does this fail almost all the time, but pass on local Mac?
2723
"no-bazelci-mac",
2824
],

examples/jest/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@ This example shows how you might use the Jest testing framework
44

55
It has a utility macro in `jest.bzl` which makes a more ergonomic API for calling the `jest_test` rule in `@npm//jest-cli:index.bzl`. We suggest copying that to your repo.
66

7+
## Running on Windows
8+
9+
To make the tests running on windows as well you have to add the `--enable_runfiles` flag to your `.bazelrc`.
10+
This requires running under elevated privileges (Admin rights), Windows 10 Creators Update (1703) or later system version, and enabling developer mode.
11+
12+
```
13+
build --enable_runfiles
14+
run --enable_runfiles
15+
test --enable_runfiles
16+
```
17+
718
# Jest typescript example
819

920
Under `ts/` there's an example of using jest with typescript directly with generated rule from `@npm//jest-cli:index.bzl`

internal/common/windows_utils.bzl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ if "%RUNFILES_MANIFEST_ONLY%" neq "1" (
3535
set %~2=%~1
3636
exit /b 0
3737
)
38+
if exist "%RUNFILES_DIR%" (
39+
set RUNFILES_MANIFEST_FILE=%RUNFILES_DIR%_manifest
40+
)
3841
if "%RUNFILES_MANIFEST_FILE%" equ "" (
3942
set RUNFILES_MANIFEST_FILE=%~f0.runfiles\MANIFEST
4043
)

internal/node/launcher.sh

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,14 +149,18 @@ fi
149149

150150
# Export the location of the runfiles helpers script
151151
export BAZEL_NODE_RUNFILES_HELPER=$(rlocation "TEMPLATED_runfiles_helper_script")
152-
if [[ "${BAZEL_NODE_RUNFILES_HELPER}" != /* ]] && [[ ! "${BAZEL_NODE_RUNFILES_HELPER}" =~ ^[A-Z]:[\\/] ]]; then
152+
# Paths can be with lower and upper case on windows because of the msys64 package in the powershell
153+
# https://regex101.com/r/c0Gjn8/1/
154+
if [[ "${BAZEL_NODE_RUNFILES_HELPER}" != /* ]] && [[ ! "${BAZEL_NODE_RUNFILES_HELPER}" =~ ^[A-Za-z]:[\/\\] ]]; then
153155
export BAZEL_NODE_RUNFILES_HELPER=$(pwd)/${BAZEL_NODE_RUNFILES_HELPER}
154156
fi
155157

156158
# Export the location of the require patch script as it can be used to bootstrap
157159
# node require patch if needed
158160
export BAZEL_NODE_PATCH_REQUIRE=$(rlocation "TEMPLATED_require_patch_script")
159-
if [[ "${BAZEL_NODE_PATCH_REQUIRE}" != /* ]] && [[ ! "${BAZEL_NODE_PATCH_REQUIRE}" =~ ^[A-Z]:[\\/] ]]; then
161+
# Paths can be with lower and upper case on windows because of the msys64 package in the powershell
162+
# https://regex101.com/r/c0Gjn8/1/
163+
if [[ "${BAZEL_NODE_PATCH_REQUIRE}" != /* ]] && [[ ! "${BAZEL_NODE_PATCH_REQUIRE}" =~ ^[A-Za-z]:[\/\\] ]]; then
160164
export BAZEL_NODE_PATCH_REQUIRE=$(pwd)/${BAZEL_NODE_PATCH_REQUIRE}
161165
fi
162166

internal/node/test/env.spec.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,16 @@ function expectPathsToMatch(a, b) {
3030

3131
describe('launcher.sh environment', function() {
3232
it('should setup correct bazel environment variables when in runfiles', function() {
33-
const runfilesRoot = normPath(process.env['RUNFILES']);
33+
const runfilesRoot = normPath(process.env['RUNFILES_DIR']);
3434
const match = runfilesRoot.match(/\/bazel-out\//);
3535
expect(!!match).toBe(true);
3636
const execroot = runfilesRoot.slice(0, match.index);
3737
expectPathsToMatch(path.basename(runfilesRoot), `env_test.${runfilesExt}.runfiles`);
3838
expectPathsToMatch(process.env['BAZEL_WORKSPACE'], 'build_bazel_rules_nodejs');
3939
expectPathsToMatch(process.env['BAZEL_TARGET'], '//internal/node/test:env_test');
40-
expectPathsToMatch(process.cwd(), `${process.env['RUNFILES']}/build_bazel_rules_nodejs`);
41-
expectPathsToMatch(process.env['PWD'], `${process.env['RUNFILES']}/build_bazel_rules_nodejs`);
42-
expectPathsToMatch(process.env['BAZEL_PATCH_ROOT'], process.env['RUNFILES']);
40+
expectPathsToMatch(process.cwd(), `${process.env['RUNFILES_DIR']}/build_bazel_rules_nodejs`);
41+
expectPathsToMatch(process.env['PWD'], `${process.env['RUNFILES_DIR']}/build_bazel_rules_nodejs`);
42+
expectPathsToMatch(process.env['BAZEL_PATCH_ROOT'], process.env['RUNFILES_DIR']);
4343
expectPathsToMatch(process.env['BAZEL_NODE_MODULES_ROOT'], 'npm/node_modules');
4444
const expectedGuards = [
4545
`${execroot}/node_modules`,

0 commit comments

Comments
 (0)