@@ -60,34 +60,33 @@ registerWarnHandler((message, options, next) => {
60
60
next . apply ( null , [ message , options ] ) ;
61
61
} ) ;
62
62
63
- export interface BaseContext {
64
- [ key : string ] : unknown ;
65
- }
63
+ export type BaseContext = object ;
66
64
67
65
/**
68
66
* The public API for the test context, which test authors can depend on being
69
67
* available.
70
68
*
71
69
* Note: this is *not* user-constructible; it becomes available by calling
72
- * `setupContext()` with a `BaseContext` .
70
+ * `setupContext()` with a base context object .
73
71
*/
74
72
export interface TestContext extends BaseContext {
75
73
owner : Owner ;
76
74
77
75
set < T > ( key : string , value : T ) : T ;
78
76
setProperties < T extends Record < string , unknown > > ( hash : T ) : T ;
79
77
get ( key : string ) : unknown ;
80
- getProperties ( ...args : string [ ] ) : Pick < BaseContext , string > ;
78
+ getProperties ( ...args : string [ ] ) : Record < string , unknown > ;
81
79
82
80
pauseTest ( ) : Promise < void > ;
83
81
resumeTest ( ) : void ;
84
82
}
85
83
86
84
// eslint-disable-next-line require-jsdoc
87
85
export function isTestContext ( context : BaseContext ) : context is TestContext {
86
+ let maybeContext = context as Record < string , unknown > ;
88
87
return (
89
- typeof context [ 'pauseTest' ] === 'function' &&
90
- typeof context [ 'resumeTest' ] === 'function'
88
+ typeof maybeContext [ 'pauseTest' ] === 'function' &&
89
+ typeof maybeContext [ 'resumeTest' ] === 'function'
91
90
) ;
92
91
}
93
92
@@ -367,15 +366,17 @@ export const SetUsage = new WeakMap<BaseContext, Array<string>>();
367
366
- setting up `pauseTest` (also available as `this.pauseTest()`) and `resumeTest` helpers
368
367
369
368
@public
370
- @param {Object } context the context to setup
369
+ @param {Object } base the context to setup
371
370
@param {Object } [options] options used to override defaults
372
371
@param {Resolver } [options.resolver] a resolver to use for customizing normal resolution
373
372
@returns {Promise<Object> } resolves with the context that was setup
374
373
*/
375
- export default function setupContext (
376
- context : BaseContext ,
374
+ export default function setupContext < T extends object > (
375
+ base : T ,
377
376
options : { resolver ?: Resolver } = { }
378
- ) : Promise < TestContext > {
377
+ ) : Promise < T & TestContext > {
378
+ let context = base as T & TestContext ;
379
+
379
380
// SAFETY: this is intimate API *designed* for us to override.
380
381
( Ember as any ) . testing = true ;
381
382
setContext ( context ) ;
@@ -426,7 +427,8 @@ export default function setupContext(
426
427
Object . defineProperty ( context , 'set' , {
427
428
configurable : true ,
428
429
enumerable : true ,
429
- value ( key : string , value : unknown ) : unknown {
430
+ // SAFETY: in all of these `defineProperty` calls, we can't actually guarantee any safety w.r.t. the corresponding field's type in `TestContext`
431
+ value ( key : any , value : any ) : unknown {
430
432
let ret = run ( function ( ) {
431
433
if ( ComponentRenderMap . has ( context ) ) {
432
434
assert (
@@ -454,7 +456,8 @@ export default function setupContext(
454
456
Object . defineProperty ( context , 'setProperties' , {
455
457
configurable : true ,
456
458
enumerable : true ,
457
- value ( hash : { [ key : string ] : any } ) : { [ key : string ] : any } {
459
+ // SAFETY: in all of these `defineProperty` calls, we can't actually guarantee any safety w.r.t. the corresponding field's type in `TestContext`
460
+ value ( hash : any ) : unknown {
458
461
let ret = run ( function ( ) {
459
462
if ( ComponentRenderMap . has ( context ) ) {
460
463
assert (
@@ -490,13 +493,14 @@ export default function setupContext(
490
493
Object . defineProperty ( context , 'getProperties' , {
491
494
configurable : true ,
492
495
enumerable : true ,
493
- value ( ...args : string [ ] ) : Pick < BaseContext , string > {
496
+ // SAFETY: in all of these `defineProperty` calls, we can't actually guarantee any safety w.r.t. the corresponding field's type in `TestContext`
497
+ value ( ...args : any [ ] ) : Record < string , unknown > {
494
498
return getProperties ( context , args ) ;
495
499
} ,
496
500
writable : false ,
497
501
} ) ;
498
502
499
- let resume : ( ( value ?: unknown ) => void ) | undefined ;
503
+ let resume : ( ( value ?: void | PromiseLike < void > ) => void ) | undefined ;
500
504
context [ 'resumeTest' ] = function resumeTest ( ) {
501
505
assert (
502
506
'Testing has not been paused. There is nothing to resume.' ,
@@ -517,6 +521,6 @@ export default function setupContext(
517
521
518
522
_setupAJAXHooks ( ) ;
519
523
520
- return context as TestContext ;
524
+ return context ;
521
525
} ) ;
522
526
}
0 commit comments