From bc5ade821a71375b707eab79d7d8b7e79955fa2b Mon Sep 17 00:00:00 2001 From: Fabian Wiles Date: Tue, 19 Mar 2019 22:29:14 +1300 Subject: [PATCH] add e2e --- e2e/jasmine/BUILD.bazel | 9 ++++++ e2e/jasmine/coverage.spec.js | 10 ++++++ e2e/jasmine/coverage_source.js | 9 ++++++ e2e/jasmine/package.json | 2 +- packages/jasmine/src/jasmine_node_test.bzl | 14 ++++++--- packages/jasmine/src/jasmine_runner.js | 36 +++++++++++----------- packages/jasmine/test/BUILD.bazel | 1 + packages/jasmine/test/coverage_source.js | 4 +-- 8 files changed, 59 insertions(+), 26 deletions(-) create mode 100644 e2e/jasmine/coverage.spec.js create mode 100644 e2e/jasmine/coverage_source.js diff --git a/e2e/jasmine/BUILD.bazel b/e2e/jasmine/BUILD.bazel index e5c91f2bc2..c30a61c3f2 100644 --- a/e2e/jasmine/BUILD.bazel +++ b/e2e/jasmine/BUILD.bazel @@ -14,3 +14,12 @@ jasmine_node_test( "@npm//zone.js", ], ) + +jasmine_node_test( + name = "coverage_test", + srcs = [ + "coverage.spec.js", + "coverage_source.js", + ], + coverage = True, +) \ No newline at end of file diff --git a/e2e/jasmine/coverage.spec.js b/e2e/jasmine/coverage.spec.js new file mode 100644 index 0000000000..a0151a1df6 --- /dev/null +++ b/e2e/jasmine/coverage.spec.js @@ -0,0 +1,10 @@ +const {isString} = require('./coverage_source'); + +describe('coverage function', () => { + it('should cover one branch', () => { + expect(isString(2)).toBe(false); + }); + it('should cover the other branch', () => { + expect(isString('some string')).toBe(true); + }); +}); diff --git a/e2e/jasmine/coverage_source.js b/e2e/jasmine/coverage_source.js new file mode 100644 index 0000000000..7d29ee1ceb --- /dev/null +++ b/e2e/jasmine/coverage_source.js @@ -0,0 +1,9 @@ +function isString(input) { + if (typeof input === 'string') { + return true; + } else { + return false; + } +} + +exports.isString = isString; diff --git a/e2e/jasmine/package.json b/e2e/jasmine/package.json index e361b4246d..8734ef5f75 100644 --- a/e2e/jasmine/package.json +++ b/e2e/jasmine/package.json @@ -1,7 +1,7 @@ { "name": "e2e-jasmine", "dependencies": { - "@bazel/jasmine": "bazel://@npm_bazel_jasmine//:npm_package", + "@bazel/jasmine": "file:///Users/fabianwiles/Documents/rules_nodejs/dist/npm_bazel_jasmine", "zone.js": "0.8.29" }, "scripts": { diff --git a/packages/jasmine/src/jasmine_node_test.bzl b/packages/jasmine/src/jasmine_node_test.bzl index f698790e74..60f7c1ff46 100644 --- a/packages/jasmine/src/jasmine_node_test.bzl +++ b/packages/jasmine/src/jasmine_node_test.bzl @@ -54,22 +54,26 @@ def jasmine_node_test( tags = tags, ) - # append on any user args to the end - args = ["--coverage=%s" % coverage] + args - all_data = data + srcs + deps + [jasmine] + all_data += ["@npm//v8-coverage"] + all_data += [":%s_devmode_srcs.MF" % name] all_data += [Label("@bazel_tools//tools/bash/runfiles")] entry_point = "@bazel/jasmine/src/jasmine_runner.js" + templated_args = ["$(location :%s_devmode_srcs.MF)" % name] + if coverage: + templated_args = templated_args + ["--coverage"] + else: + templated_args = templated_args + ["--nocoverage"] + nodejs_test( name = name, data = all_data, entry_point = entry_point, - templated_args = ["$(location :%s_devmode_srcs.MF)" % name], + templated_args = templated_args, testonly = 1, expected_exit_code = expected_exit_code, tags = tags, - args = args, **kwargs ) diff --git a/packages/jasmine/src/jasmine_runner.js b/packages/jasmine/src/jasmine_runner.js index 92aec0276f..1744e82c76 100644 --- a/packages/jasmine/src/jasmine_runner.js +++ b/packages/jasmine/src/jasmine_runner.js @@ -1,11 +1,8 @@ const fs = require('fs'); const path = require('path'); -const crypto = require('crypto'); let jasmineCore = null let JasmineRunner = require('jasmine/lib/jasmine'); -const Execute = require('v8-coverage/src/execute'); -const Report = require('v8-coverage/src/report'); if (global.jasmine) { @@ -72,11 +69,8 @@ function main(args) { const manifest = require.resolve(args[0]); // second is always a flag to enable coverage or not const coverageArg = args[1]; - let enableCoverage = false; - if (coverageArg.startsWith('--coverage')) { - // Python has capital booleans - enableCoverage = coverageArg.split('=')[1] === 'True'; - } + const enableCoverage = coverageArg.startsWith('--coverage'); + // Remove the manifest, some tested code may process the argv. // Also remove the --coverage flag process.argv.splice(2, 2)[0]; @@ -92,13 +86,15 @@ function main(args) { const cwd = process.cwd() const sourceFiles = allFiles - // Filter out all .spec and .test files so we only report - // coverage against the source files - .filter(f => !IS_TEST_FILE.test(f)) - .map(f => require.resolve(f)) - // the reporting lib resolves the relative path instead of using the absolute one - // so match it here - .map(f => path.relative(cwd, f)) + // Filter out all .spec and .test files so we only report + // coverage against the source files + .filter(f => !IS_TEST_FILE.test(f)) + // the jasmine_runner.js gets in here as a file to run + .filter(f => !f.endsWith('jasmine_runner.js')) + .map(f => require.resolve(f)) + // the reporting lib resolves the relative path instead of using the + // absolute one so match it here + .map(f => path.relative(cwd, f)) allFiles // Filter here so that only files ending in `spec.js` and `test.js` @@ -123,6 +119,9 @@ function main(args) { let covExecutor; let covDir; if (enableCoverage) { + const crypto = require('crypto'); + const Execute = require('v8-coverage/src/execute'); + console.log(sourceFiles) covDir = path.join(process.env['TEST_TMPDIR'], String(crypto.randomBytes(4).readUInt32LE(0))); covExecutor = new Execute({include: sourceFiles, exclude: []}); covExecutor.startProfiler(); @@ -133,6 +132,7 @@ function main(args) { if (noSpecsFound) exitCode = BAZEL_EXIT_NO_TESTS_FOUND; if (enableCoverage) { + const Report = require('v8-coverage/src/report'); covExecutor.stopProfiler((err, data) => { if (err) { console.error(err); @@ -141,8 +141,8 @@ function main(args) { const sourceCoverge = covExecutor.filterResult(data.result); // we could do this all in memory if we wanted // just take a look at v8-coverage/src/report.js and reimplement some of those methods - // but we're goign to have to write a file at some point for bazel coverage - // so mayaswell support it now + // but we're going to have to write a file at some point for bazel coverage + // so may as well support it now // the lib expects these paths to exist for some reason fs.mkdirSync(covDir); fs.mkdirSync(path.join(covDir, 'tmp')); @@ -197,5 +197,5 @@ function getAllSpecs(jasmineEnv) { } if (require.main === module) { - process.exitCode = main(process.argv.slice(2)); + process.exitCode = main(process.argv.slice(2)); } diff --git a/packages/jasmine/test/BUILD.bazel b/packages/jasmine/test/BUILD.bazel index 6a43d92173..fb55a58658 100644 --- a/packages/jasmine/test/BUILD.bazel +++ b/packages/jasmine/test/BUILD.bazel @@ -55,4 +55,5 @@ jasmine_node_test( ], coverage = True, jasmine = "@npm//jasmine", + deps = ["//:jasmine_runner"], ) diff --git a/packages/jasmine/test/coverage_source.js b/packages/jasmine/test/coverage_source.js index 87af24750b..aac2ee3afe 100644 --- a/packages/jasmine/test/coverage_source.js +++ b/packages/jasmine/test/coverage_source.js @@ -1,6 +1,6 @@ function isString(input) { - if(typeof input === 'string') { - return true; + if (typeof input === 'string') { + return true; } else { return false; }