Skip to content

Commit b0b2648

Browse files
committed
feat(jasmine): introduce config_file attribute
1 parent 715ffc6 commit b0b2648

5 files changed

Lines changed: 66 additions & 14 deletions

File tree

packages/jasmine/src/jasmine_node_test.bzl

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ def jasmine_node_test(
2828
deps = [],
2929
expected_exit_code = 0,
3030
tags = [],
31+
config_file = None,
3132
coverage = False,
3233
jasmine = "@npm//@bazel/jasmine",
3334
jasmine_entry_point = "@npm//:node_modules/@bazel/jasmine/jasmine_runner.js",
@@ -43,6 +44,15 @@ def jasmine_node_test(
4344
deps: Other targets which produce JavaScript, such as ts_library
4445
expected_exit_code: The expected exit code for the test.
4546
tags: Bazel tags applied to test
47+
config_file: (experimental) label of a file containing Jasmine JSON config.
48+
49+
Note that not all configuration options are honored, and
50+
we expect some strange feature interations.
51+
For example, if you list spec_files, they will be tested
52+
but not instrumented for code coverage.
53+
54+
See https://jasmine.github.io/setup/nodejs.html#configuration
55+
4656
coverage: Enables code coverage collection and reporting.
4757
jasmine: A label providing the `@bazel/jasmine` npm dependency.
4858
jasmine_entry_point: A label providing the `@bazel/jasmine` entry point.
@@ -61,12 +71,21 @@ def jasmine_node_test(
6171
all_data += [Label("@bazel_tools//tools/bash/runfiles")]
6272

6373
# If the target specified templated_args, pass it through.
64-
templated_args = kwargs.pop("templated_args", []) + ["$(location :%s_devmode_srcs.MF)" % name]
74+
templated_args = kwargs.pop("templated_args", [])
75+
templated_args.append("$(location :%s_devmode_srcs.MF)" % name)
6576

6677
if coverage:
67-
templated_args = templated_args + ["--coverage"]
78+
templated_args.append("--coverage")
79+
else:
80+
templated_args.append("--nocoverage")
81+
82+
if config_file:
83+
# Calculate a label relative to the user's BUILD file
84+
pkg = Label("%s//%s:__pkg__" % (native.repository_name(), native.package_name()))
85+
all_data.append(pkg.relative(config_file))
86+
templated_args.append("$(location %s)" % config_file)
6887
else:
69-
templated_args = templated_args + ["--nocoverage"]
88+
templated_args.append("--noconfig")
7089

7190
nodejs_test(
7291
name = name,

packages/jasmine/src/jasmine_runner.js

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,26 +51,34 @@ Error.stackTraceLimit = Infinity;
5151
const IS_TEST_FILE = /[^a-zA-Z0-9](spec|test)\.js$/i;
5252
const IS_NODE_MODULE = /\/node_modules\//
5353

54+
// We process arguments by splicing them out of the process.argv
55+
// Users could set their own templated_args on their test, then
56+
// the tested code might process the argv
57+
// So it shouldn't see these Bazel-specific ones
58+
function readArg() {
59+
return process.argv.splice(2, 1)[0];
60+
}
61+
5462
function main(args) {
55-
if (!args.length) {
56-
throw new Error('Spec file manifest expected argument missing');
63+
if (args.length < 3) {
64+
throw new Error('expected argument missing');
5765
}
66+
67+
5868
// first args is always the path to the manifest
59-
const manifest = require.resolve(args[0]);
69+
const manifest = require.resolve(readArg());
6070
// second is always a flag to enable coverage or not
61-
const coverageArg = args[1];
71+
const coverageArg = readArg();
6272
const enableCoverage = coverageArg === '--coverage';
63-
64-
// Remove the manifest, some tested code may process the argv.
65-
// Also remove the --coverage flag
66-
process.argv.splice(2, 2)[0];
73+
// config file is the next arg
74+
const configFile = readArg();
6775

6876
// the relative directory the coverage reporter uses to find anf filter the files
69-
const cwd = process.cwd()
77+
const cwd = process.cwd();
7078

7179
const jrunner = new JasmineRunner({jasmineCore: jasmineCore});
72-
if (args.length == 3) {
73-
jrunner.loadConfigFile(args[2]);
80+
if (configFile !== '--noconfig') {
81+
jrunner.loadConfigFile(require.resolve(configFile));
7482
}
7583
const allFiles = fs.readFileSync(manifest, UTF8)
7684
.split('\n')

packages/jasmine/test/BUILD.bazel

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,16 @@ jasmine_node_test(
6363
"--node_options=--experimental-modules",
6464
],
6565
)
66+
67+
# We have no srcs[] here because we set specs in the config file
68+
jasmine_node_test(
69+
name = "config_file_test",
70+
config_file = "test_config_file.json",
71+
# The file isn't named following our usual conventions
72+
# but since it's configured in the json config file
73+
# Jasmine will still load it
74+
data = ["test_config_file.js"],
75+
# TODO(alexeagle): on Windows CI we get no specs found
76+
# Maybe Jasmine doesn't normalize the slashes in the config
77+
tags = ["fix-windows"],
78+
)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
describe('configuring Jasmine', () => {
2+
it('should accept a config file', () => {
3+
// the config_file.json has random: false
4+
expect(jasmine.getEnv().configuration().random).toBeFalsy();
5+
});
6+
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"random": false,
3+
"spec_files": [
4+
"**/test_config_*.js"
5+
]
6+
}

0 commit comments

Comments
 (0)