UNPKG

7.59 kBTypeScriptView Raw
1import type { Resolver } from '@ember/owner';
2import { Owner } from './build-owner';
3import { DeprecationFailure } from './-internal/deprecations';
4import { Warning } from './-internal/warnings';
5export interface SetupContextOptions {
6 resolver?: Resolver | undefined;
7}
8export type BaseContext = object;
9/**
10 * The public API for the test context, which test authors can depend on being
11 * available.
12 *
13 * Note: this is *not* user-constructible; it becomes available by calling
14 * `setupContext()` with a base context object.
15 */
16export interface TestContext extends BaseContext {
17 owner: Owner;
18 set<T>(key: string, value: T): T;
19 setProperties<T extends Record<string, unknown>>(hash: T): T;
20 get(key: string): unknown;
21 getProperties(...args: string[]): Record<string, unknown>;
22 pauseTest(): Promise<void>;
23 resumeTest(): void;
24}
25export declare function isTestContext(context: BaseContext): context is TestContext;
26/**
27 Stores the provided context as the "global testing context".
28
29 Generally setup automatically by `setupContext`.
30
31 @public
32 @param {Object} context the context to use
33*/
34export declare function setContext(context: BaseContext): void;
35/**
36 Retrieve the "global testing context" as stored by `setContext`.
37
38 @public
39 @returns {Object} the previously stored testing context
40*/
41export declare function getContext(): BaseContext | undefined;
42/**
43 Clear the "global testing context".
44
45 Generally invoked from `teardownContext`.
46
47 @public
48*/
49export declare function unsetContext(): void;
50/**
51 * Returns a promise to be used to pauses the current test (due to being
52 * returned from the test itself). This is useful for debugging while testing
53 * or for test-driving. It allows you to inspect the state of your application
54 * at any point.
55 *
56 * The test framework wrapper (e.g. `ember-qunit` or `ember-mocha`) should
57 * ensure that when `pauseTest()` is used, any framework specific test timeouts
58 * are disabled.
59 *
60 * @public
61 * @returns {Promise<void>} resolves _only_ when `resumeTest()` is invoked
62 * @example <caption>Usage via ember-qunit</caption>
63 *
64 * import { setupRenderingTest } from 'ember-qunit';
65 * import { render, click, pauseTest } from '@ember/test-helpers';
66 *
67 *
68 * module('awesome-sauce', function(hooks) {
69 * setupRenderingTest(hooks);
70 *
71 * test('does something awesome', async function(assert) {
72 * await render(hbs`{{awesome-sauce}}`);
73 *
74 * // added here to visualize / interact with the DOM prior
75 * // to the interaction below
76 * await pauseTest();
77 *
78 * click('.some-selector');
79 *
80 * assert.equal(this.element.textContent, 'this sauce is awesome!');
81 * });
82 * });
83 */
84export declare function pauseTest(): Promise<void>;
85/**
86 Resumes a test previously paused by `await pauseTest()`.
87
88 @public
89*/
90export declare function resumeTest(): void;
91/**
92 * Returns deprecations which have occurred so far for a the current test context
93 *
94 * @public
95 * @returns {Array<DeprecationFailure>} An array of deprecation messages
96 * @example <caption>Usage via ember-qunit</caption>
97 *
98 * import { getDeprecations } from '@ember/test-helpers';
99 *
100 * module('awesome-sauce', function(hooks) {
101 * setupRenderingTest(hooks);
102 *
103 * test('does something awesome', function(assert) {
104 const deprecations = getDeprecations() // => returns deprecations which have occurred so far in this test
105 * });
106 * });
107 */
108export declare function getDeprecations(): Array<DeprecationFailure>;
109export type { DeprecationFailure };
110/**
111 * Returns deprecations which have occurred so far for a the current test context
112 *
113 * @public
114 * @param {Function} [callback] The callback that when executed will have its DeprecationFailure recorded
115 * @returns {Array<DeprecationFailure> | Promise<Array<DeprecationFailure>>} An array of deprecation messages
116 * @example <caption>Usage via ember-qunit</caption>
117 *
118 * import { getDeprecationsDuringCallback } from '@ember/test-helpers';
119 *
120 * module('awesome-sauce', function(hooks) {
121 * setupRenderingTest(hooks);
122 *
123 * test('does something awesome', function(assert) {
124 * const deprecations = getDeprecationsDuringCallback(() => {
125 * // code that might emit some deprecations
126 *
127 * }); // => returns deprecations which occurred while the callback was invoked
128 * });
129 *
130 *
131 * test('does something awesome', async function(assert) {
132 * const deprecations = await getDeprecationsDuringCallback(async () => {
133 * // awaited code that might emit some deprecations
134 * }); // => returns deprecations which occurred while the callback was invoked
135 * });
136 * });
137 */
138export declare function getDeprecationsDuringCallback(callback: () => void): Array<DeprecationFailure> | Promise<Array<DeprecationFailure>>;
139/**
140 * Returns warnings which have occurred so far for a the current test context
141 *
142 * @public
143 * @returns {Array<Warning>} An array of warnings
144 * @example <caption>Usage via ember-qunit</caption>
145 *
146 * import { getWarnings } from '@ember/test-helpers';
147 *
148 * module('awesome-sauce', function(hooks) {
149 * setupRenderingTest(hooks);
150 *
151 * test('does something awesome', function(assert) {
152 const warnings = getWarnings() // => returns warnings which have occurred so far in this test
153 * });
154 * });
155 */
156export declare function getWarnings(): Array<Warning>;
157export type { Warning };
158/**
159 * Returns warnings which have occurred so far for a the current test context
160 *
161 * @public
162 * @param {Function} [callback] The callback that when executed will have its warnings recorded
163 * @returns {Array<Warning> | Promise<Array<Warning>>} An array of warnings information
164 * @example <caption>Usage via ember-qunit</caption>
165 *
166 * import { getWarningsDuringCallback } from '@ember/test-helpers';
167 * import { warn } from '@ember/debug';
168 *
169 * module('awesome-sauce', function(hooks) {
170 * setupRenderingTest(hooks);
171 *
172 * test('does something awesome', function(assert) {
173 * const warnings = getWarningsDuringCallback(() => {
174 * warn('some warning');
175 *
176 * }); // => returns warnings which occurred while the callback was invoked
177 * });
178 *
179 * test('does something awesome', async function(assert) {
180 * warn('some warning');
181 *
182 * const warnings = await getWarningsDuringCallback(async () => {
183 * warn('some other warning');
184 * }); // => returns warnings which occurred while the callback was invoked
185 * });
186 * });
187 */
188export declare function getWarningsDuringCallback(callback: () => void): Array<Warning> | Promise<Array<Warning>>;
189export declare const ComponentRenderMap: WeakMap<object, true>;
190export declare const SetUsage: WeakMap<object, string[]>;
191/**
192 Used by test framework addons to setup the provided context for testing.
193
194 Responsible for:
195
196 - sets the "global testing context" to the provided context (`setContext`)
197 - create an owner object and set it on the provided context (e.g. `this.owner`)
198 - setup `this.set`, `this.setProperties`, `this.get`, and `this.getProperties` to the provided context
199 - setting up AJAX listeners
200 - setting up `pauseTest` (also available as `this.pauseTest()`) and `resumeTest` helpers
201
202 @public
203 @param {Object} base the context to setup
204 @param {Object} [options] options used to override defaults
205 @param {Resolver} [options.resolver] a resolver to use for customizing normal resolution
206 @returns {Promise<Object>} resolves with the context that was setup
207*/
208export default function setupContext<T extends object>(base: T, options?: SetupContextOptions): Promise<T & TestContext>;
209//# sourceMappingURL=setup-context.d.ts.map
\No newline at end of file