Skip to content

Commit

Permalink
feat(jasmine): introduce config_file attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeagle committed Aug 5, 2019
1 parent 715ffc6 commit b0b2648
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 14 deletions.
25 changes: 22 additions & 3 deletions packages/jasmine/src/jasmine_node_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def jasmine_node_test(
deps = [],
expected_exit_code = 0,
tags = [],
config_file = None,
coverage = False,
jasmine = "@npm//@bazel/jasmine",
jasmine_entry_point = "@npm//:node_modules/@bazel/jasmine/jasmine_runner.js",
Expand All @@ -43,6 +44,15 @@ def jasmine_node_test(
deps: Other targets which produce JavaScript, such as ts_library
expected_exit_code: The expected exit code for the test.
tags: Bazel tags applied to test
config_file: (experimental) label of a file containing Jasmine JSON config.
Note that not all configuration options are honored, and
we expect some strange feature interations.
For example, if you list spec_files, they will be tested
but not instrumented for code coverage.
See https://jasmine.github.io/setup/nodejs.html#configuration
coverage: Enables code coverage collection and reporting.
jasmine: A label providing the `@bazel/jasmine` npm dependency.
jasmine_entry_point: A label providing the `@bazel/jasmine` entry point.
Expand All @@ -61,12 +71,21 @@ def jasmine_node_test(
all_data += [Label("@bazel_tools//tools/bash/runfiles")]

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

if coverage:
templated_args = templated_args + ["--coverage"]
templated_args.append("--coverage")
else:
templated_args.append("--nocoverage")

if config_file:
# Calculate a label relative to the user's BUILD file
pkg = Label("%s//%s:__pkg__" % (native.repository_name(), native.package_name()))
all_data.append(pkg.relative(config_file))
templated_args.append("$(location %s)" % config_file)
else:
templated_args = templated_args + ["--nocoverage"]
templated_args.append("--noconfig")

nodejs_test(
name = name,
Expand Down
30 changes: 19 additions & 11 deletions packages/jasmine/src/jasmine_runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,26 +51,34 @@ Error.stackTraceLimit = Infinity;
const IS_TEST_FILE = /[^a-zA-Z0-9](spec|test)\.js$/i;
const IS_NODE_MODULE = /\/node_modules\//

// We process arguments by splicing them out of the process.argv
// Users could set their own templated_args on their test, then
// the tested code might process the argv
// So it shouldn't see these Bazel-specific ones
function readArg() {
return process.argv.splice(2, 1)[0];
}

function main(args) {
if (!args.length) {
throw new Error('Spec file manifest expected argument missing');
if (args.length < 3) {
throw new Error('expected argument missing');
}


// first args is always the path to the manifest
const manifest = require.resolve(args[0]);
const manifest = require.resolve(readArg());
// second is always a flag to enable coverage or not
const coverageArg = args[1];
const coverageArg = readArg();
const enableCoverage = coverageArg === '--coverage';

// Remove the manifest, some tested code may process the argv.
// Also remove the --coverage flag
process.argv.splice(2, 2)[0];
// config file is the next arg
const configFile = readArg();

// the relative directory the coverage reporter uses to find anf filter the files
const cwd = process.cwd()
const cwd = process.cwd();

const jrunner = new JasmineRunner({jasmineCore: jasmineCore});
if (args.length == 3) {
jrunner.loadConfigFile(args[2]);
if (configFile !== '--noconfig') {
jrunner.loadConfigFile(require.resolve(configFile));
}
const allFiles = fs.readFileSync(manifest, UTF8)
.split('\n')
Expand Down
13 changes: 13 additions & 0 deletions packages/jasmine/test/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,16 @@ jasmine_node_test(
"--node_options=--experimental-modules",
],
)

# We have no srcs[] here because we set specs in the config file
jasmine_node_test(
name = "config_file_test",
config_file = "test_config_file.json",
# The file isn't named following our usual conventions
# but since it's configured in the json config file
# Jasmine will still load it
data = ["test_config_file.js"],
# TODO(alexeagle): on Windows CI we get no specs found
# Maybe Jasmine doesn't normalize the slashes in the config
tags = ["fix-windows"],
)
6 changes: 6 additions & 0 deletions packages/jasmine/test/test_config_file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
describe('configuring Jasmine', () => {
it('should accept a config file', () => {
// the config_file.json has random: false
expect(jasmine.getEnv().configuration().random).toBeFalsy();
});
});
6 changes: 6 additions & 0 deletions packages/jasmine/test/test_config_file.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"random": false,
"spec_files": [
"**/test_config_*.js"
]
}

0 comments on commit b0b2648

Please sign in to comment.