Skip to content

Commit

Permalink
feat(jasmine): add basic ESM support
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabian Wiles authored and alexeagle committed Mar 30, 2022
1 parent bc62f37 commit b4b2c74
Show file tree
Hide file tree
Showing 13 changed files with 34 additions and 28 deletions.
2 changes: 1 addition & 1 deletion internal/linker/link_node_modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import * as path from 'path';
// have typings we use a type-only import to the `@bazel/runfiles` package that is the source
// of truth for the checked-in file.
const {runfiles: _defaultRunfiles, _BAZEL_OUT_REGEX}:
typeof import('@bazel/runfiles') = require('../runfiles/index.js')
typeof import('@bazel/runfiles') = require('../runfiles/index.cjs')
import {Runfiles} from '@bazel/runfiles';

// Run Bazel with --define=VERBOSE_LOGS=1 to enable this logging
Expand Down
4 changes: 2 additions & 2 deletions internal/linker/test/integration/run_program.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# limitations under the License.

# This shell script is a minimal fixture for the launcher.sh script
# with a critical difference: instead of calling the loader.js script
# with a critical difference: instead of calling the loader.cjs script
# with the users program passed as an argument (allowing us to patch the node
# loader), this one just runs vanilla node with the users program as the argument
# which lets us assert that the linker is the reason the program works.
Expand All @@ -36,7 +36,7 @@ export VERBOSE_LOGS=1
# export NODE_DEBUG=module

# Export the location of the runfiles helpers script
export BAZEL_NODE_RUNFILES_HELPER=$(rlocation "build_bazel_rules_nodejs/internal/runfiles/runfile_helper_main.js")
export BAZEL_NODE_RUNFILES_HELPER=$(rlocation "build_bazel_rules_nodejs/internal/runfiles/runfile_helper_main.cjs")
if [[ "${BAZEL_NODE_RUNFILES_HELPER}" != /* ]] && [[ ! "${BAZEL_NODE_RUNFILES_HELPER}" =~ ^[A-Z]:[\\/] ]]; then
export BAZEL_NODE_RUNFILES_HELPER=$(pwd)/${BAZEL_NODE_RUNFILES_HELPER}
fi
Expand Down
2 changes: 1 addition & 1 deletion internal/linker/test/link_node_modules.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const linker: typeof import('../link_node_modules') = require('../index.js');
// As seen above, the linker file always loads the checked-in runfile helpers. We don't want
// to have a mix of checked-in files and sources, so we import runfile helpers from the
// checked-in file too, but use the types provided by the `@bazel/runfiles` package.
const {Runfiles}: typeof import('@bazel/runfiles') = require('../../runfiles/index.js');
const {Runfiles}: typeof import('@bazel/runfiles') = require('../../runfiles/index.cjs');

const BIN_DIR = `bazel-out/my-platform-fastbuild/bin`;

Expand Down
2 changes: 1 addition & 1 deletion internal/node/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ filegroup(
name = "package_contents",
srcs = glob([
"*.bzl",
"*.js",
"*.cjs",
"*.sh",
]) + [
"BUILD.bazel",
Expand Down
6 changes: 3 additions & 3 deletions internal/node/launcher.sh
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,11 @@ for ARG in ${ALL_ARGS[@]+"${ALL_ARGS[@]}"}; do
--bazel_capture_exit_code=*) EXIT_CODE_CAPTURE="${PWD}/${ARG#--bazel_capture_exit_code=}" ;;
# Outputs nothing on success
--bazel_silent_on_success=*) SILENT_ON_SUCCESS=true ;;
# Disable the node_loader.js monkey patches for require()
# Disable the node_loader.cjs monkey patches for require()
# Note that this means you need an explicit runfiles helper library
# This flag is now a no-op since the default is also false
--nobazel_patch_module_resolver) PATCH_REQUIRE=false ;;
# Enable the node_loader.js monkey patches for require()
# Enable the node_loader.cjs monkey patches for require()
--bazel_patch_module_resolver) PATCH_REQUIRE=true ;;
# Disable the --require node-patches (undocumented and unused; only here as an escape value)
--nobazel_node_patches) NODE_PATCHES=false ;;
Expand Down Expand Up @@ -331,7 +331,7 @@ if [ "$PATCH_REQUIRE" = true ]; then
* ) require_patch_script="${PWD}/${require_patch_script}" ;;
esac
LAUNCHER_NODE_OPTIONS+=( "--require" "$require_patch_script" )
# Change the entry point to be the loader.js script so we run code before node
# Change the entry point to be the loader.cjs script so we run code before node
MAIN=$(rlocation "TEMPLATED_loader_script")
else
# Entry point is the user-supplied script
Expand Down
4 changes: 2 additions & 2 deletions internal/node/node.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -626,8 +626,8 @@ Predefined genrule variables are not supported in this context.

_NODEJS_EXECUTABLE_OUTPUTS = {
"launcher_sh": "%{name}.sh",
"loader_script": "%{name}_loader.js",
"require_patch_script": "%{name}_require_patch.js",
"loader_script": "%{name}_loader.cjs",
"require_patch_script": "%{name}_require_patch.cjs",
}

nodejs_binary_kwargs = {
Expand Down
2 changes: 1 addition & 1 deletion internal/node/test/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ copy_file(
)

# This rule creates a file that alphabetically comes before any source file in this
# package. This genfile can be then set up as runfile to verify that the "loader.js"
# package. This genfile can be then set up as runfile to verify that the "loader.cjs"
# properly determines the local workspace path without incorrectly using the genfile as base
# for the local workspace path. See: https://github.com/bazelbuild/rules_nodejs/issues/352
write_file(
Expand Down
3 changes: 1 addition & 2 deletions internal/node/test/esm/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ nodejs_binary(
entry_point = ":has-deps.mjs",
)


nodejs_binary(
name = "no_deps",
data = ["no-deps.mjs"],
entry_point = ":no-deps.mjs",
)
)
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@
"http-server": "^0.11.1",
"husky": "1.3.1",
"is-builtin-module": "3.0.0",
"jasmine": "~3.4.0",
"jasmine-core": "~3.4.0",
"jasmine": "~3.7.0",
"jasmine-core": "~3.7.0",
"jasmine-reporters": "~2.5.0",
"jest": "^27.0.4",
"jest-cli": "^27.0.4",
Expand Down
2 changes: 1 addition & 1 deletion packages/jasmine/jasmine_runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ if (TOTAL_SHARDS) {
// the maximum (See: https://nodejs.org/api/errors.html#errors_error_stacktracelimit)
Error.stackTraceLimit = Infinity;

const IS_TEST_FILE = /[^a-zA-Z0-9](spec|test)\.js$/i;
const IS_TEST_FILE = /[^a-zA-Z0-9](spec|test)\.(mjs|cjs|js)$/i;
const IS_NODE_MODULE = /\/node_modules\//

// We process arguments by splicing them out of the process.argv
Expand Down
6 changes: 6 additions & 0 deletions packages/jasmine/test/esm/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
load("//packages/jasmine:index.bzl", "jasmine_node_test")

jasmine_node_test(
name = "jasmine_import",
srcs = ["jasmine_import.spec.mjs"],
)
6 changes: 6 additions & 0 deletions packages/jasmine/test/esm/jasmine_import.spec.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

describe('spec in file ending with *.mjs', () => {
it('should run', () => {
expect(true).toBe(true);
});
});
19 changes: 7 additions & 12 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6231,7 +6231,7 @@ istanbul-reports@^3.0.2:
html-escaper "^2.0.0"
istanbul-lib-report "^3.0.0"

jasmine-core@^3.6.0:
jasmine-core@^3.6.0, jasmine-core@~3.7.0:
version "3.7.1"
resolved "https://registry.yarnpkg.com/jasmine-core/-/jasmine-core-3.7.1.tgz#0401327f6249eac993d47bbfa18d4e8efacfb561"
integrity sha512-DH3oYDS/AUvvr22+xUBW62m1Xoy7tUlY1tsxKEJvl5JeJ7q8zd1K5bUwiOxdH+erj6l2vAMM3hV25Xs9/WrmuQ==
Expand All @@ -6241,11 +6241,6 @@ jasmine-core@~2.8.0:
resolved "https://registry.yarnpkg.com/jasmine-core/-/jasmine-core-2.8.0.tgz#bcc979ae1f9fd05701e45e52e65d3a5d63f1a24e"
integrity sha1-vMl5rh+f0FcB5F5S5l06XWPxok4=

jasmine-core@~3.4.0:
version "3.4.0"
resolved "https://registry.yarnpkg.com/jasmine-core/-/jasmine-core-3.4.0.tgz#2a74618e966026530c3518f03e9f845d26473ce3"
integrity sha512-HU/YxV4i6GcmiH4duATwAbJQMlE0MsDIR5XmSVxURxKHn3aGAdbY1/ZJFmVRbKtnLwIxxMJD7gYaPsypcbYimg==

jasmine-reporters@~2.5.0:
version "2.5.0"
resolved "https://registry.yarnpkg.com/jasmine-reporters/-/jasmine-reporters-2.5.0.tgz#f9e2e0f82aaa2e07e8d553be56457efe0fd8b39e"
Expand All @@ -6263,13 +6258,13 @@ jasmine@2.8.0:
glob "^7.0.6"
jasmine-core "~2.8.0"

jasmine@~3.4.0:
version "3.4.0"
resolved "https://registry.yarnpkg.com/jasmine/-/jasmine-3.4.0.tgz#0fa68903ff0c9697459cd044b44f4dcef5ec8bdc"
integrity sha512-sR9b4n+fnBFDEd7VS2el2DeHgKcPiMVn44rtKFumq9q7P/t8WrxsVIZPob4UDdgcDNCwyDqwxCt4k9TDRmjPoQ==
jasmine@~3.7.0:
version "3.7.0"
resolved "https://registry.yarnpkg.com/jasmine/-/jasmine-3.7.0.tgz#d36638c0c815e6ad5666676e386d79e2ccb70835"
integrity sha512-wlzGQ+cIFzMEsI+wDqmOwvnjTvolLFwlcpYLCqSPPH0prOQaW3P+IzMhHYn934l1imNvw07oCyX+vGUv3wmtSQ==
dependencies:
glob "^7.1.3"
jasmine-core "~3.4.0"
glob "^7.1.6"
jasmine-core "~3.7.0"

jasminewd2@^2.1.0:
version "2.2.0"
Expand Down

0 comments on commit b4b2c74

Please sign in to comment.