UNPKG

10.3 kBTypeScriptView Raw
1// Type definitions for ember-qunit 5.0
2// Project: https://github.com/emberjs/ember-qunit#readme
3// Definitions by: Dan Freeman <https://github.com/dfreeman>
4// Chris Krycho <https://github.com/chriskrycho>
5// James C. Davis <https://github.com/jamescdavis>
6// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
7// Minimum TypeScript Version: 4.4
8
9import EmberTestAdapter from '@ember/test/adapter';
10import { Resolver } from '@ember/owner';
11import { TestContext } from '@ember/test-helpers';
12
13/**
14 * Sets a Resolver globally which will be used to look up objects from each test's container.
15 */
16export function setResolver(resolver: Resolver): void;
17
18/**
19 * Options for configuring the test runner. Normally, you will not need to
20 * customize this. It is exported primarily so that end user app code can name
21 * it when passing it back to the framework.
22 */
23export interface SetupTestOptions {
24 /**
25 * The resolver to use when instantiating container-managed entities in the test.
26 */
27 resolver?: Resolver | undefined;
28}
29
30/**
31 * Sets up acceptance tests.
32 *
33 * The `setupApplicationTest` function is used for all acceptance tests. It
34 * is invoked in the callback scope of a QUnit module (aka "nested module").
35 *
36 * Once invoked, all subsequent hooks.beforeEach and test invocations will
37 * have access to the following:
38 * * `this.owner` - the owner object that been set on the test context.
39 * * `this.pauseTest` and `this.resumeTest` - allow easy pausing/resuming of tests.
40 * * `this.element` which returns the DOM element representing the application's root element.
41 */
42export function setupApplicationTest(
43 hooks: NestedHooks,
44 options?: SetupTestOptions
45): void;
46
47/**
48 * Sets up tests that need to render snippets of templates.
49 *
50 * The setupRenderingTest method is used for tests that need to render
51 * snippets of templates. It is also invoked in the callback scope of a
52 * QUnit module (aka "nested module").
53 *
54 * Once invoked, all subsequent hooks.beforeEach and test invocations will
55 * have access to the following:
56 * * All of the methods / properties listed for `setupTest`
57 * * this.render(...) - Renders the provided template snippet returning a
58 * promise that resolves once rendering has completed
59 * * An importable render function that de-sugars into this.render will be
60 * the default output of blueprints
61 * * this.element - Returns the native DOM element representing the element
62 * that was rendered via this.render
63 * * this.$(...) - When jQuery is present, executes a jQuery selector with
64 * the current this.element as its root
65 */
66export function setupRenderingTest(
67 hooks: NestedHooks,
68 options?: SetupTestOptions
69): void;
70
71/**
72 * Sets up tests that do not need to render snippets of templates.
73 *
74 * The `setupTest` method is used for all types of tests except for those
75 * that need to render snippets of templates. It is invoked in the callback
76 * scope of a QUnit module (aka "nested module").
77 *
78 * Once invoked, all subsequent hooks.beforeEach and test invocations will
79 * have access to the following:
80 * * this.owner - This exposes the standard "owner API" for the test environment.
81 * * this.set / this.setProperties - Allows setting values on the test context.
82 * * this.get / this.getProperties - Retrieves values from the test context.
83 */
84export function setupTest(hooks: NestedHooks, options?: SetupTestOptions): void;
85
86export class QUnitAdapter extends EmberTestAdapter {}
87
88export { module, test, skip, only, todo } from 'qunit';
89
90interface QUnitStartOptions {
91 /**
92 * If `false` the test container will not be setup based on `devmode`,
93 * `dockcontainer`, or `nocontainer` URL params.
94 */
95 setupTestContainer?: boolean | undefined;
96
97 /**
98 * If `false` tests will not be automatically started (you must run
99 * `QUnit.start()` to kick them off).
100 */
101 startTests?: boolean | undefined;
102
103 /**
104 * If `false` the default Ember.Test adapter will not be updated.
105 */
106 setupTestAdapter?: boolean | undefined;
107
108 /**
109 * `false` opts out of the default behavior of setting `Ember.testing`
110 * to `true` before all tests and back to `false` after each test will.
111 */
112 setupEmberTesting?: boolean | undefined;
113
114 /**
115 * If `false` test isolation validation will be disabled.
116 */
117 setupTestIsolationValidation?: boolean | undefined;
118}
119
120export function setupEmberOnerrorValidation(): void;
121
122export function start(options?: QUnitStartOptions): void;
123
124// SAFETY: all of the `TC extends TestContext` generics below are just wildly,
125// impossibly unsafe. QUnit cannot -- ever! -- guarantee that the test context
126// is properly set up in a type-safe way to match this. However, it is the only
127// way to handle setting state in a TS-visible way prior to Ember RFC 0785,
128// which is slooooowly rolling out across the ecosystem in conjunction with the
129// `<template>` feature.
130
131declare global {
132 // NOTE: disables `no-unnecessary-generics` inline because, unfortunately,
133 // the design of Ember's test tooling (and indeed *QUnit's* test system)
134 // requires that we allow users to update the type of the context of the
135 // test. This is indeed strictly *wrong*! However, changing it will require
136 // changing how Ember handles testing. See [the PR][pr] for further details.
137 //
138 // [pr]: https://github.com/DefinitelyTyped/DefinitelyTyped/pull/56494
139
140 interface NestedHooks {
141 /**
142 * Runs after the last test. If additional tests are defined after the
143 * module's queue has emptied, it will not run this hook again.
144 */
145 after<TC extends TestContext>(
146 fn: (this: TC, assert: Assert) => void | Promise<void>
147 ): void;
148
149 /**
150 * Runs after each test.
151 */
152 afterEach<TC extends TestContext>(
153 fn: (this: TC, assert: Assert) => void | Promise<void>
154 ): void;
155
156 /**
157 * Runs before the first test.
158 */
159 // SAFETY: this is just wildly, impossibly unsafe. QUnit cannot -- ever! --
160 before<TC extends TestContext>(
161 fn: (this: TC, assert: Assert) => void | Promise<void>
162 ): void;
163
164 /**
165 * Runs before each test.
166 */
167 // SAFETY: this is just wildly, impossibly unsafe. QUnit cannot -- ever! --
168 beforeEach<TC extends TestContext>(
169 fn: (this: TC, assert: Assert) => void | Promise<void>
170 ): void;
171 }
172
173 interface QUnit {
174 /**
175 * Adds a test to exclusively run, preventing all other tests from running.
176 *
177 * Use this method to focus your test suite on a specific test. QUnit.only
178 * will cause any other tests in your suite to be ignored.
179 *
180 * Note, that if more than one QUnit.only is present only the first instance
181 * will run.
182 *
183 * This is an alternative to filtering tests to run in the HTML reporter. It
184 * is especially useful when you use a console reporter or in a codebase
185 * with a large set of long running tests.
186 *
187 * @param name Title of unit being tested
188 * @param callback Function to close over assertions
189 */
190 // SAFETY: this is just wildly, impossibly unsafe. QUnit cannot -- ever! --
191 // provide this guarantee. However, it's also the only way to support TS
192 // in tests in Ember until we move the community over entirely to using
193 // `<template>` and local scope.
194 only<TC extends TestContext>(
195 name: string,
196 callback: (this: TC, assert: Assert) => void | Promise<unknown>
197 ): void;
198
199 /**
200 * Use this method to test a unit of code which is still under development (in a “todo” state).
201 * The test will pass as long as one failing assertion is present.
202 *
203 * If all assertions pass, then the test will fail signaling that `QUnit.todo` should
204 * be replaced by `QUnit.test`.
205 *
206 * @param name Title of unit being tested
207 * @param callback Function to close over assertions
208 */
209 // SAFETY: this is just wildly, impossibly unsafe. QUnit cannot -- ever! --
210 // provide this guarantee. However, it's also the only way to support TS
211 // in tests in Ember until we move the community over entirely to using
212 // `<template>` and local scope.
213 todo<TC extends TestContext>(
214 name: string,
215 callback: (this: TC, assert: Assert) => void | Promise<unknown>
216 ): void;
217
218 /**
219 * Adds a test like object to be skipped.
220 *
221 * Use this method to replace QUnit.test() instead of commenting out entire
222 * tests.
223 *
224 * This test's prototype will be listed on the suite as a skipped test,
225 * ignoring the callback argument and the respective global and module's
226 * hooks.
227 *
228 * @param name Title of unit being tested
229 * @param callback Function to close over assertions
230 */
231 // SAFETY: this is just wildly, impossibly unsafe. QUnit cannot -- ever! --
232 // provide this guarantee. However, it's also the only way to support TS
233 // in tests in Ember until we move the community over entirely to using
234 // `<template>` and local scope.
235 skip<TC extends TestContext>(
236 name: string,
237 callback?: (this: TC, assert: Assert) => void | Promise<unknown>
238 ): void;
239 }
240
241 namespace QUnit {
242 interface TestFunction {
243 // SAFETY: this is just wildly, impossibly unsafe. QUnit cannot -- ever! --
244 // provide this guarantee. However, it's also the only way to support TS
245 // in tests in Ember until we move the community over entirely to using
246 // `<template>` and local scope.
247 <TC extends TestContext>(
248 name: string,
249 callback: (this: TC, assert: Assert) => void | Promise<unknown>
250 ): void;
251 }
252
253 interface SkipFunction {
254 <TC extends TestContext>(
255 name: string,
256 callback?: (this: TC, assert: Assert) => void | Promise<unknown>
257 ): void;
258 }
259
260 interface TodoFunction {
261 <TC extends TestContext>(
262 name: string,
263 callback?: (this: TC, assert: Assert) => void | Promise<unknown>
264 ): void;
265 }
266
267 interface OnlyFunction {
268 <TC extends TestContext>(
269 name: string,
270 callback: (this: TC, assert: Assert) => void | Promise<unknown>
271 ): void;
272 }
273
274 interface EachFunction {
275 <TC extends TestContext, T>(
276 name: string,
277 dataset: T[],
278 callback: (this: TC, assert: Assert, data: T) => void | Promise<unknown>
279 ): void;
280 }
281 }
282}