@@ -8,8 +8,8 @@ import { getHooks, setFn, setHooks } from './map'
88// apis
99export const suite = createSuite ( )
1010export const test = createTest (
11- function ( name : string , fn ?: TestFunction , options ?: number | TestOptions ) {
12- getCurrentSuite ( ) . test . fn . call ( this , name , fn , options )
11+ function ( name : string | Function , fn ?: TestFunction , options ?: number | TestOptions ) {
12+ getCurrentSuite ( ) . test . fn . call ( this , formatName ( name ) , fn , options )
1313 } ,
1414)
1515
@@ -59,7 +59,7 @@ function createSuiteCollector(name: string, factory: SuiteFactory = () => { }, m
5959
6060 initSuite ( )
6161
62- const test = createTest ( function ( name : string , fn = noop , options ) {
62+ const test = createTest ( function ( name : string | Function , fn = noop , options ) {
6363 const mode = this . only ? 'only' : this . skip ? 'skip' : this . todo ? 'todo' : 'run'
6464
6565 if ( typeof options === 'number' )
@@ -78,7 +78,7 @@ function createSuiteCollector(name: string, factory: SuiteFactory = () => { }, m
7878 const test : Test = {
7979 id : '' ,
8080 type : 'test' ,
81- name,
81+ name : formatName ( name ) ,
8282 each : this . each ,
8383 mode,
8484 suite : undefined ! ,
@@ -189,7 +189,7 @@ function createSuiteCollector(name: string, factory: SuiteFactory = () => { }, m
189189}
190190
191191function createSuite ( ) {
192- function suiteFn ( this : Record < string , boolean | undefined > , name : string , factory ?: SuiteFactory , options ?: number | TestOptions ) {
192+ function suiteFn ( this : Record < string , boolean | undefined > , name : string | Function , factory ?: SuiteFactory , options ?: number | TestOptions ) {
193193 const mode : RunMode = this . only ? 'only' : this . skip ? 'skip' : this . todo ? 'todo' : 'run'
194194 const currentSuite = getCurrentSuite ( )
195195
@@ -200,7 +200,7 @@ function createSuite() {
200200 if ( currentSuite ?. options )
201201 options = { ...currentSuite . options , ...options }
202202
203- return createSuiteCollector ( name , factory , mode , this . concurrent , this . shuffle , this . each , options )
203+ return createSuiteCollector ( formatName ( name ) , factory , mode , this . concurrent , this . shuffle , this . each , options )
204204 }
205205
206206 suiteFn . each = function < T > ( this : { withContext : ( ) => SuiteAPI ; setContext : ( key : string , value : boolean | undefined ) => SuiteAPI } , cases : ReadonlyArray < T > , ...args : any [ ] ) {
@@ -210,13 +210,14 @@ function createSuite() {
210210 if ( Array . isArray ( cases ) && args . length )
211211 cases = formatTemplateString ( cases , args )
212212
213- return ( name : string , fn : ( ...args : T [ ] ) => void , options ?: number | TestOptions ) => {
213+ return ( name : string | Function , fn : ( ...args : T [ ] ) => void , options ?: number | TestOptions ) => {
214+ const _name = formatName ( name )
214215 const arrayOnlyCases = cases . every ( Array . isArray )
215216 cases . forEach ( ( i , idx ) => {
216217 const items = Array . isArray ( i ) ? i : [ i ]
217218 arrayOnlyCases
218- ? suite ( formatTitle ( name , items , idx ) , ( ) => fn ( ...items ) , options )
219- : suite ( formatTitle ( name , items , idx ) , ( ) => fn ( i ) , options )
219+ ? suite ( formatTitle ( _name , items , idx ) , ( ) => fn ( ...items ) , options )
220+ : suite ( formatTitle ( _name , items , idx ) , ( ) => fn ( i ) , options )
220221 } )
221222
222223 this . setContext ( 'each' , undefined )
@@ -249,14 +250,15 @@ function createTest(fn: (
249250 if ( Array . isArray ( cases ) && args . length )
250251 cases = formatTemplateString ( cases , args )
251252
252- return ( name : string , fn : ( ...args : T [ ] ) => void , options ?: number | TestOptions ) => {
253+ return ( name : string | Function , fn : ( ...args : T [ ] ) => void , options ?: number | TestOptions ) => {
254+ const _name = formatName ( name )
253255 const arrayOnlyCases = cases . every ( Array . isArray )
254256 cases . forEach ( ( i , idx ) => {
255257 const items = Array . isArray ( i ) ? i : [ i ]
256258
257259 arrayOnlyCases
258- ? test ( formatTitle ( name , items , idx ) , ( ) => fn ( ...items ) , options )
259- : test ( formatTitle ( name , items , idx ) , ( ) => fn ( i ) , options )
260+ ? test ( formatTitle ( _name , items , idx ) , ( ) => fn ( ...items ) , options )
261+ : test ( formatTitle ( _name , items , idx ) , ( ) => fn ( i ) , options )
260262 } )
261263
262264 this . setContext ( 'each' , undefined )
@@ -272,6 +274,10 @@ function createTest(fn: (
272274 ) as TestAPI
273275}
274276
277+ function formatName ( name : string | Function ) {
278+ return typeof name === 'string' ? name : name instanceof Function ? name . name : String ( name )
279+ }
280+
275281function formatTitle ( template : string , items : any [ ] , idx : number ) {
276282 if ( template . includes ( '%#' ) ) {
277283 // '%#' match index of the test case
0 commit comments