@@ -48,6 +48,11 @@ enum WrapKind {
48
48
NoWrap ,
49
49
}
50
50
51
+ struct TestOrSnippet {
52
+ file : File ,
53
+ has_deno_test : bool ,
54
+ }
55
+
51
56
fn extract_inner (
52
57
file : File ,
53
58
wrap_kind : WrapKind ,
@@ -86,8 +91,13 @@ fn extract_inner(
86
91
87
92
extracted_files
88
93
. into_iter ( )
89
- . map ( |extracted_file| {
90
- generate_pseudo_file ( extracted_file, & file. specifier , & exports, wrap_kind)
94
+ . map ( |extracted| {
95
+ let wrap_kind = if extracted. has_deno_test {
96
+ WrapKind :: NoWrap
97
+ } else {
98
+ wrap_kind
99
+ } ;
100
+ generate_pseudo_file ( extracted. file , & file. specifier , & exports, wrap_kind)
91
101
} )
92
102
. collect :: < Result < _ , _ > > ( )
93
103
}
@@ -96,7 +106,7 @@ fn extract_files_from_fenced_blocks(
96
106
specifier : & ModuleSpecifier ,
97
107
source : & str ,
98
108
media_type : MediaType ,
99
- ) -> Result < Vec < File > , AnyError > {
109
+ ) -> Result < Vec < TestOrSnippet > , AnyError > {
100
110
// The pattern matches code blocks as well as anything in HTML comment syntax,
101
111
// but it stores the latter without any capturing groups. This way, a simple
102
112
// check can be done to see if a block is inside a comment (and skip typechecking)
@@ -119,7 +129,7 @@ fn extract_files_from_source_comments(
119
129
specifier : & ModuleSpecifier ,
120
130
source : Arc < str > ,
121
131
media_type : MediaType ,
122
- ) -> Result < Vec < File > , AnyError > {
132
+ ) -> Result < Vec < TestOrSnippet > , AnyError > {
123
133
let parsed_source = deno_ast:: parse_module ( deno_ast:: ParseParams {
124
134
specifier : specifier. clone ( ) ,
125
135
text : source,
@@ -165,7 +175,9 @@ fn extract_files_from_regex_blocks(
165
175
file_line_index : usize ,
166
176
blocks_regex : & Regex ,
167
177
lines_regex : & Regex ,
168
- ) -> Result < Vec < File > , AnyError > {
178
+ ) -> Result < Vec < TestOrSnippet > , AnyError > {
179
+ let tests_regex = lazy_regex:: regex!( r"(?m)^\s*Deno\.test\(" ) ;
180
+
169
181
let files = blocks_regex
170
182
. captures_iter ( source)
171
183
. filter_map ( |block| {
@@ -229,12 +241,16 @@ fn extract_files_from_regex_blocks(
229
241
mapped_specifier_for_tsc ( & file_specifier, file_media_type)
230
242
. map ( |s| ModuleSpecifier :: parse ( & s) . unwrap ( ) )
231
243
. unwrap_or ( file_specifier) ;
232
-
233
- Some ( File {
244
+ let has_deno_test = tests_regex . is_match ( & file_source ) ;
245
+ let file = File {
234
246
url : file_specifier,
235
247
mtime : None ,
236
248
maybe_headers : None ,
237
249
source : file_source. into_bytes ( ) . into ( ) ,
250
+ } ;
251
+ Some ( TestOrSnippet {
252
+ file,
253
+ has_deno_test,
238
254
} )
239
255
} )
240
256
. collect ( ) ;
@@ -846,7 +862,7 @@ mod tests {
846
862
/**
847
863
* ```ts
848
864
* import { assertEquals } from "@std/assert/equal";
849
- *
865
+ *
850
866
* assertEquals(add(1, 2), 3);
851
867
* ```
852
868
*/
@@ -1199,6 +1215,90 @@ Deno.test("file:///main.ts$3-7.ts", async ()=>{
1199
1215
media_type: MediaType :: TypeScript ,
1200
1216
} ] ,
1201
1217
} ,
1218
+ // https://github.com/denoland/deno/issues/29629
1219
+ Test {
1220
+ input : Input {
1221
+ source : r#"
1222
+ # Title
1223
+
1224
+ ```ts
1225
+ import { assertEquals } from "@std/assert/equals";
1226
+
1227
+ Deno.test("add", () => {
1228
+ assertEquals(1 + 2, 3);
1229
+ });
1230
+ ```
1231
+ "# ,
1232
+ specifier : "file:///main.md" ,
1233
+ } ,
1234
+ expected : vec ! [ Expected {
1235
+ source: r#"import { assertEquals } from "@std/assert/equals";
1236
+ Deno.test("add", ()=>{
1237
+ assertEquals(1 + 2, 3);
1238
+ });
1239
+ "# ,
1240
+ specifier: "file:///main.md$4-11.ts" ,
1241
+ media_type: MediaType :: TypeScript ,
1242
+ } ] ,
1243
+ } ,
1244
+ Test {
1245
+ input : Input {
1246
+ source : r#"
1247
+ /**
1248
+ * ```ts
1249
+ * import { assertEquals } from "@std/assert/equals";
1250
+ *
1251
+ * Deno.test("add", () => {
1252
+ * assertEquals(add(1, 2), 3);
1253
+ * });
1254
+ * ```
1255
+ */
1256
+ export function add(a: number, b: number): number {
1257
+ return a + b;
1258
+ }
1259
+ "# ,
1260
+ specifier : "file:///main.ts" ,
1261
+ } ,
1262
+ expected : vec ! [ Expected {
1263
+ source: r#"import { assertEquals } from "@std/assert/equals";
1264
+ import { add } from "file:///main.ts";
1265
+ Deno.test("add", ()=>{
1266
+ assertEquals(add(1, 2), 3);
1267
+ });
1268
+ "# ,
1269
+ specifier: "file:///main.ts$3-10.ts" ,
1270
+ media_type: MediaType :: TypeScript ,
1271
+ } ] ,
1272
+ } ,
1273
+ // commented out `Deno.test` should be ignored
1274
+ Test {
1275
+ input : Input {
1276
+ source : r#"
1277
+ /**
1278
+ * ```ts
1279
+ * import { assertEquals } from "@std/assert/equals";
1280
+ * // Deno.test("add", () => {});
1281
+ * assertEquals(add(1, 2), 3);
1282
+ * ```
1283
+ */
1284
+ export function add(a: number, b: number): number {
1285
+ return a + b;
1286
+ }
1287
+ "# ,
1288
+ specifier : "file:///main.ts" ,
1289
+ } ,
1290
+ expected : vec ! [ Expected {
1291
+ source: r#"import { assertEquals } from "@std/assert/equals";
1292
+ import { add } from "file:///main.ts";
1293
+ Deno.test("file:///main.ts$3-8.ts", async ()=>{
1294
+ // Deno.test("add", () => {});
1295
+ assertEquals(add(1, 2), 3);
1296
+ });
1297
+ "# ,
1298
+ specifier: "file:///main.ts$3-8.ts" ,
1299
+ media_type: MediaType :: TypeScript ,
1300
+ } ] ,
1301
+ } ,
1202
1302
] ;
1203
1303
1204
1304
for test in tests {
0 commit comments