Skip to content

Commit

Permalink
add e2e
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabian Wiles committed Mar 19, 2019
1 parent d65e11d commit bc5ade8
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 26 deletions.
9 changes: 9 additions & 0 deletions e2e/jasmine/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
10 changes: 10 additions & 0 deletions e2e/jasmine/coverage.spec.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
9 changes: 9 additions & 0 deletions e2e/jasmine/coverage_source.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function isString(input) {
if (typeof input === 'string') {
return true;
} else {
return false;
}
}

exports.isString = isString;
2 changes: 1 addition & 1 deletion e2e/jasmine/package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
14 changes: 9 additions & 5 deletions packages/jasmine/src/jasmine_node_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
36 changes: 18 additions & 18 deletions packages/jasmine/src/jasmine_runner.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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];
Expand All @@ -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`
Expand All @@ -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();
Expand All @@ -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);
Expand All @@ -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'));
Expand Down Expand Up @@ -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));
}
1 change: 1 addition & 0 deletions packages/jasmine/test/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,5 @@ jasmine_node_test(
],
coverage = True,
jasmine = "@npm//jasmine",
deps = ["//:jasmine_runner"],
)
4 changes: 2 additions & 2 deletions packages/jasmine/test/coverage_source.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
function isString(input) {
if(typeof input === 'string') {
return true;
if (typeof input === 'string') {
return true;
} else {
return false;
}
Expand Down

0 comments on commit bc5ade8

Please sign in to comment.