Skip to content

fix(test): handle snippet containing Deno.test in doc test #29631

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

Merged
merged 1 commit into from
Jun 10, 2025
Merged
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
116 changes: 108 additions & 8 deletions cli/util/extract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ enum WrapKind {
NoWrap,
}

struct TestOrSnippet {
file: File,
has_deno_test: bool,
}

fn extract_inner(
file: File,
wrap_kind: WrapKind,
Expand Down Expand Up @@ -86,8 +91,13 @@ fn extract_inner(

extracted_files
.into_iter()
.map(|extracted_file| {
generate_pseudo_file(extracted_file, &file.specifier, &exports, wrap_kind)
.map(|extracted| {
let wrap_kind = if extracted.has_deno_test {
WrapKind::NoWrap
} else {
wrap_kind
};
generate_pseudo_file(extracted.file, &file.specifier, &exports, wrap_kind)
})
.collect::<Result<_, _>>()
}
Expand All @@ -96,7 +106,7 @@ fn extract_files_from_fenced_blocks(
specifier: &ModuleSpecifier,
source: &str,
media_type: MediaType,
) -> Result<Vec<File>, AnyError> {
) -> Result<Vec<TestOrSnippet>, AnyError> {
// The pattern matches code blocks as well as anything in HTML comment syntax,
// but it stores the latter without any capturing groups. This way, a simple
// check can be done to see if a block is inside a comment (and skip typechecking)
Expand All @@ -119,7 +129,7 @@ fn extract_files_from_source_comments(
specifier: &ModuleSpecifier,
source: Arc<str>,
media_type: MediaType,
) -> Result<Vec<File>, AnyError> {
) -> Result<Vec<TestOrSnippet>, AnyError> {
let parsed_source = deno_ast::parse_module(deno_ast::ParseParams {
specifier: specifier.clone(),
text: source,
Expand Down Expand Up @@ -165,7 +175,9 @@ fn extract_files_from_regex_blocks(
file_line_index: usize,
blocks_regex: &Regex,
lines_regex: &Regex,
) -> Result<Vec<File>, AnyError> {
) -> Result<Vec<TestOrSnippet>, AnyError> {
let tests_regex = lazy_regex::regex!(r"(?m)^\s*Deno\.test\(");

let files = blocks_regex
.captures_iter(source)
.filter_map(|block| {
Expand Down Expand Up @@ -229,12 +241,16 @@ fn extract_files_from_regex_blocks(
mapped_specifier_for_tsc(&file_specifier, file_media_type)
.map(|s| ModuleSpecifier::parse(&s).unwrap())
.unwrap_or(file_specifier);

Some(File {
let has_deno_test = tests_regex.is_match(&file_source);
let file = File {
url: file_specifier,
mtime: None,
maybe_headers: None,
source: file_source.into_bytes().into(),
};
Some(TestOrSnippet {
file,
has_deno_test,
})
})
.collect();
Expand Down Expand Up @@ -846,7 +862,7 @@ mod tests {
/**
* ```ts
* import { assertEquals } from "@std/assert/equal";
*
*
* assertEquals(add(1, 2), 3);
* ```
*/
Expand Down Expand Up @@ -1199,6 +1215,90 @@ Deno.test("file:///main.ts$3-7.ts", async ()=>{
media_type: MediaType::TypeScript,
}],
},
// https://github.com/denoland/deno/issues/29629
Test {
input: Input {
source: r#"
# Title

```ts
import { assertEquals } from "@std/assert/equals";

Deno.test("add", () => {
assertEquals(1 + 2, 3);
});
```
"#,
specifier: "file:///main.md",
},
expected: vec![Expected {
source: r#"import { assertEquals } from "@std/assert/equals";
Deno.test("add", ()=>{
assertEquals(1 + 2, 3);
});
"#,
specifier: "file:///main.md$4-11.ts",
media_type: MediaType::TypeScript,
}],
},
Test {
input: Input {
source: r#"
/**
* ```ts
* import { assertEquals } from "@std/assert/equals";
*
* Deno.test("add", () => {
* assertEquals(add(1, 2), 3);
* });
* ```
*/
export function add(a: number, b: number): number {
return a + b;
}
"#,
specifier: "file:///main.ts",
},
expected: vec![Expected {
source: r#"import { assertEquals } from "@std/assert/equals";
import { add } from "file:///main.ts";
Deno.test("add", ()=>{
assertEquals(add(1, 2), 3);
});
"#,
specifier: "file:///main.ts$3-10.ts",
media_type: MediaType::TypeScript,
}],
},
// commented out `Deno.test` should be ignored
Test {
input: Input {
source: r#"
/**
* ```ts
* import { assertEquals } from "@std/assert/equals";
* // Deno.test("add", () => {});
* assertEquals(add(1, 2), 3);
* ```
*/
export function add(a: number, b: number): number {
return a + b;
}
"#,
specifier: "file:///main.ts",
},
expected: vec![Expected {
source: r#"import { assertEquals } from "@std/assert/equals";
import { add } from "file:///main.ts";
Deno.test("file:///main.ts$3-8.ts", async ()=>{
// Deno.test("add", () => {});
assertEquals(add(1, 2), 3);
});
"#,
specifier: "file:///main.ts$3-8.ts",
media_type: MediaType::TypeScript,
}],
},
];

for test in tests {
Expand Down
Loading