File tree Expand file tree Collapse file tree 2 files changed +51
-0
lines changed Expand file tree Collapse file tree 2 files changed +51
-0
lines changed Original file line number Diff line number Diff line change @@ -214,6 +214,32 @@ class TestContext {
214214 return this . #test. signal ;
215215 }
216216
217+ /**
218+ * Get the path of the current test file.
219+ *
220+ * @example '/tmp/test.js'
221+ */
222+ get file ( ) {
223+ return this . #test. loc . file ;
224+ }
225+
226+ /**
227+ * Get the name for the current context and all ancestors (outputs a string from root to tip).
228+ *
229+ * @example 'Animals › Cat › sleep() should be possible at any time'
230+ */
231+ get fullName ( ) {
232+ let fullName = this . #test. name ;
233+ let parent = this . #test. parent ;
234+
235+ while ( parent . parent ) { // null when at the root (which has no name), so abort when we get there
236+ fullName = `${ parent . name } › ${ fullName } ` ;
237+ ( { parent } = parent ) ;
238+ }
239+
240+ return fullName ;
241+ }
242+
217243 get name ( ) {
218244 return this . #test. name ;
219245 }
Original file line number Diff line number Diff line change 1+ 'use strict' ;
2+ require ( '../common' ) ;
3+
4+ const assert = require ( 'node:assert/strict' ) ;
5+ const { resolve } = require ( 'node:path' ) ;
6+ const { describe, test } = require ( 'node:test' ) ;
7+
8+
9+ // These props are needed to support jest-like snapshots (which is not itself a feature in Core).
10+
11+ const rootName = 'props for snapshots' ;
12+ describe ( rootName , ( ) => {
13+ test ( 'test context has "file" property' , ( ctx ) => {
14+ assert . equal ( ctx . file , resolve ( __filename ) ) ;
15+ } ) ;
16+
17+ const nestedName = '"fullName" contains both case and ancestor names' ;
18+ test ( nestedName , ( ctx ) => {
19+ assert . match ( ctx . fullName , new RegExp ( rootName ) ) ;
20+ assert . match ( ctx . fullName , new RegExp ( nestedName ) ) ;
21+
22+ // ensure root appears before tip
23+ assert . ok ( ctx . fullName . indexOf ( rootName ) < ctx . fullName . indexOf ( nestedName ) ) ;
24+ } ) ;
25+ } ) ;
You can’t perform that action at this time.
0 commit comments