Skip to content

Commit

Permalink
fix: exclude internal JS files from coverage (#20448)
Browse files Browse the repository at this point in the history
  • Loading branch information
marvinhagemeister committed Sep 11, 2023
1 parent 4460336 commit 9d13858
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 0 deletions.
65 changes: 65 additions & 0 deletions cli/tests/integration/coverage_tests.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.

use deno_core::serde_json;
use std::fs;
use test_util as util;
use test_util::TempDir;
use util::assert_starts_with;
use util::env_vars_for_npm_tests;
use util::TestContext;
use util::TestContextBuilder;
Expand Down Expand Up @@ -439,3 +441,66 @@ fn no_transpiled_lines() {

output.assert_exit_code(0);
}

#[test]
fn no_internal_code() {
let context = TestContext::default();
let tempdir = context.temp_dir();
let tempdir = tempdir.path().join("cov");

let output = context
.new_command()
.args_vec(vec![
"test".to_string(),
"--quiet".to_string(),
format!("--coverage={}", tempdir),
"coverage/no_internal_code_test.ts".to_string(),
])
.run();

output.assert_exit_code(0);
output.skip_output_check();

// Check that coverage files contain no internal urls
let paths = fs::read_dir(tempdir).unwrap();
for path in paths {
let unwrapped = path.unwrap().path();
let data = fs::read_to_string(&unwrapped.clone()).unwrap();

let value: serde_json::Value = serde_json::from_str(&data).unwrap();
let url = value["url"].as_str().unwrap();
assert_starts_with!(url, "file:");
}
}

#[test]
fn no_internal_node_code() {
let context = TestContext::default();
let tempdir = context.temp_dir();
let tempdir = tempdir.path().join("cov");

let output = context
.new_command()
.args_vec(vec![
"test".to_string(),
"--quiet".to_string(),
"--no-check".to_string(),
format!("--coverage={}", tempdir),
"coverage/no_internal_node_code_test.ts".to_string(),
])
.run();

output.assert_exit_code(0);
output.skip_output_check();

// Check that coverage files contain no internal urls
let paths = fs::read_dir(tempdir).unwrap();
for path in paths {
let unwrapped = path.unwrap().path();
let data = fs::read_to_string(&unwrapped.clone()).unwrap();

let value: serde_json::Value = serde_json::from_str(&data).unwrap();
let url = value["url"].as_str().unwrap();
assert_starts_with!(url, "file:");
}
}
7 changes: 7 additions & 0 deletions cli/tests/testdata/coverage/no_internal_code_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const add = (a: number, b: number) => a + b;

Deno.test(function addTest() {
if (add(2, 3) !== 5) {
throw new Error("fail");
}
});
8 changes: 8 additions & 0 deletions cli/tests/testdata/coverage/no_internal_node_code_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import * as path from "node:path";

Deno.test(function test() {
const res = path.join("foo", "bar");
if (!res.includes("foo")) {
throw new Error("fail");
}
});
7 changes: 7 additions & 0 deletions cli/tools/coverage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,13 @@ impl CoverageCollector {

let script_coverages = self.take_precise_coverage().await?.result;
for script_coverage in script_coverages {
// Filter out internal JS files from being included in coverage reports
if script_coverage.url.starts_with("ext:")
|| script_coverage.url.starts_with("[ext:")
{
continue;
}

let filename = format!("{}.json", Uuid::new_v4());
let filepath = self.dir.join(filename);

Expand Down

0 comments on commit 9d13858

Please sign in to comment.