forked from vitest-dev/vitest
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
89 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import type { Arrayable, Nullable, Suite, Task, Test } from '../types' | ||
|
||
export function notNullish<T>(v: T | null | undefined): v is NonNullable<T> { | ||
return v != null | ||
} | ||
|
||
export function slash(str: string) { | ||
return str.replace(/\\/g, '/') | ||
} | ||
|
||
export function mergeSlashes(str: string) { | ||
return str.replace(/\/\//g, '/') | ||
} | ||
|
||
export const noop = () => {} | ||
|
||
/** | ||
* Convert `Arrayable<T>` to `Array<T>` | ||
* | ||
* @category Array | ||
*/ | ||
export function toArray<T>(array?: Nullable<Arrayable<T>>): Array<T> { | ||
array = array || [] | ||
if (Array.isArray(array)) | ||
return array | ||
return [array] | ||
} | ||
|
||
export function getTests(suite: Arrayable<Task>): Test[] { | ||
return toArray(suite).flatMap(s => s.type === 'test' ? [s] : s.tasks.flatMap(c => c.type === 'test' ? [c] : getTests(c))) | ||
} | ||
|
||
export function getTasks(tasks: Arrayable<Task>): Task[] { | ||
return toArray(tasks).flatMap(s => s.type === 'test' ? [s] : [s, ...getTasks(s.tasks)]) | ||
} | ||
|
||
export function getSuites(suite: Arrayable<Task>): Suite[] { | ||
return toArray(suite).flatMap(s => s.type === 'suite' ? [s, ...getSuites(s.tasks)] : []) | ||
} | ||
|
||
export function hasTests(suite: Arrayable<Suite>): boolean { | ||
return toArray(suite).some(s => s.tasks.some(c => c.type === 'test' || hasTests(c as Suite))) | ||
} | ||
|
||
export function hasFailed(suite: Arrayable<Task>): boolean { | ||
return toArray(suite).some(s => s.result?.state === 'fail' || (s.type === 'suite' && hasFailed(s.tasks))) | ||
} | ||
|
||
export function getNames(task: Task) { | ||
const names = [task.name] | ||
let current: Task | undefined = task | ||
|
||
while (current?.suite || current?.file) { | ||
current = current.suite || current.file | ||
if (current?.name) | ||
names.unshift(current.name) | ||
} | ||
|
||
return names | ||
} |