From 354b3fcf047cb1f33df2de30d19acb7b830c9319 Mon Sep 17 00:00:00 2001 From: Dan Freeman Date: Tue, 20 Dec 2022 14:48:18 -0700 Subject: [PATCH] Remove the index signature from `TestContext` --- .../@ember/test-helpers/setup-context.ts | 36 ++++++++++--------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/addon-test-support/@ember/test-helpers/setup-context.ts b/addon-test-support/@ember/test-helpers/setup-context.ts index f335e9991..c4daadb4b 100644 --- a/addon-test-support/@ember/test-helpers/setup-context.ts +++ b/addon-test-support/@ember/test-helpers/setup-context.ts @@ -59,16 +59,14 @@ registerWarnHandler((message, options, next) => { next.apply(null, [message, options]); }); -export interface BaseContext { - [key: string]: unknown; -} +export type BaseContext = object; /** * The public API for the test context, which test authors can depend on being * available. * * Note: this is *not* user-constructible; it becomes available by calling - * `setupContext()` with a `BaseContext`. + * `setupContext()` with a base context object. */ export interface TestContext extends BaseContext { owner: Owner; @@ -76,7 +74,7 @@ export interface TestContext extends BaseContext { set(key: string, value: T): T; setProperties>(hash: T): T; get(key: string): unknown; - getProperties(...args: string[]): Pick; + getProperties(...args: string[]): Record; pauseTest(): Promise; resumeTest(): void; @@ -84,9 +82,10 @@ export interface TestContext extends BaseContext { // eslint-disable-next-line require-jsdoc export function isTestContext(context: BaseContext): context is TestContext { + let maybeContext = context as Record; return ( - typeof context['pauseTest'] === 'function' && - typeof context['resumeTest'] === 'function' + typeof maybeContext['pauseTest'] === 'function' && + typeof maybeContext['resumeTest'] === 'function' ); } @@ -366,15 +365,17 @@ export const SetUsage = new WeakMap>(); - setting up `pauseTest` (also available as `this.pauseTest()`) and `resumeTest` helpers @public - @param {Object} context the context to setup + @param {Object} base the context to setup @param {Object} [options] options used to override defaults @param {Resolver} [options.resolver] a resolver to use for customizing normal resolution @returns {Promise} resolves with the context that was setup */ -export default function setupContext( - context: BaseContext, +export default function setupContext( + base: T, options: { resolver?: Resolver } = {} -): Promise { +): Promise { + let context = base as T & TestContext; + // SAFETY: this is intimate API *designed* for us to override. (Ember as any).testing = true; setContext(context); @@ -425,7 +426,8 @@ export default function setupContext( Object.defineProperty(context, 'set', { configurable: true, enumerable: true, - value(key: string, value: unknown): unknown { + // SAFETY: in all of these `defineProperty` calls, we can't actually guarantee any safety w.r.t. the corresponding field's type in `TestContext` + value(key: any, value: any): unknown { let ret = run(function () { if (ComponentRenderMap.has(context)) { assert( @@ -453,7 +455,8 @@ export default function setupContext( Object.defineProperty(context, 'setProperties', { configurable: true, enumerable: true, - value(hash: { [key: string]: any }): { [key: string]: any } { + // SAFETY: in all of these `defineProperty` calls, we can't actually guarantee any safety w.r.t. the corresponding field's type in `TestContext` + value(hash: any): unknown { let ret = run(function () { if (ComponentRenderMap.has(context)) { assert( @@ -489,13 +492,14 @@ export default function setupContext( Object.defineProperty(context, 'getProperties', { configurable: true, enumerable: true, - value(...args: string[]): Pick { + // SAFETY: in all of these `defineProperty` calls, we can't actually guarantee any safety w.r.t. the corresponding field's type in `TestContext` + value(...args: any[]): Record { return getProperties(context, args); }, writable: false, }); - let resume: ((value?: unknown) => void) | undefined; + let resume: ((value?: void | PromiseLike) => void) | undefined; context['resumeTest'] = function resumeTest() { assert( 'Testing has not been paused. There is nothing to resume.', @@ -516,6 +520,6 @@ export default function setupContext( _setupAJAXHooks(); - return context as TestContext; + return context; }); }