Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: exclude internal JS files from coverage #20448

Merged
merged 3 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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