Skip to content

Commit

Permalink
feat: add __tests__ to test file detection defaults (#24443)
Browse files Browse the repository at this point in the history
The `jest` test runner popularized putting tests into a `__tests__`
folder. Whilst many have switched to going with a `.test` suffix in the
file name these days, there are still many jest projects that have
`__tests__`. By adding this to the default test detection logic it makes
`deno test` discover those out of the box.
  • Loading branch information
marvinhagemeister committed Jul 8, 2024
1 parent 74ac29b commit 1e97f0f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
2 changes: 1 addition & 1 deletion cli/args/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2591,7 2591,7 @@ report results to standard output:
deno test src/fetch_test.ts src/signal_test.ts
Directory arguments are expanded to all contained files matching the glob
{*_,*.,}test.{js,mjs,ts,mts,jsx,tsx}:
{*_,*.,}test.{js,mjs,ts,mts,jsx,tsx} or **/__tests__/**:
deno test src/",
)
Expand Down
21 changes: 20 additions & 1 deletion cli/tools/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1611,9 1611,16 @@ pub(crate) fn is_supported_test_path(path: &Path) -> bool {
fn has_supported_test_path_name(path: &Path) -> bool {
if let Some(name) = path.file_stem() {
let basename = name.to_string_lossy();
basename.ends_with("_test")
if basename.ends_with("_test")
|| basename.ends_with(".test")
|| basename == "test"
{
return true;
}

path
.components()
.any(|seg| seg.as_os_str().to_str() == Some("__tests__"))
} else {
false
}
Expand Down Expand Up @@ -2077,6 2084,18 @@ mod inner_test {
assert!(is_supported_test_path(Path::new("foo/bar/test.jsx")));
assert!(is_supported_test_path(Path::new("foo/bar/test.ts")));
assert!(is_supported_test_path(Path::new("foo/bar/test.tsx")));
assert!(is_supported_test_path(Path::new(
"foo/bar/__tests__/foo.js"
)));
assert!(is_supported_test_path(Path::new(
"foo/bar/__tests__/foo.jsx"
)));
assert!(is_supported_test_path(Path::new(
"foo/bar/__tests__/foo.ts"
)));
assert!(is_supported_test_path(Path::new(
"foo/bar/__tests__/foo.tsx"
)));
assert!(!is_supported_test_path(Path::new("README.md")));
assert!(!is_supported_test_path(Path::new("lib/typescript.d.ts")));
assert!(!is_supported_test_path(Path::new("notatest.js")));
Expand Down

0 comments on commit 1e97f0f

Please sign in to comment.