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` tests will not be loaded automatically.
93 */
94 loadTests?: boolean | undefined;
95
96 /**
97 * If `false` the test container will not be setup based on `devmode`,
98 * `dockcontainer`, or `nocontainer` URL params.
99 */
100 setupTestContainer?: boolean | undefined;
101
102 /**
103 * If `false` tests will not be automatically started (you must run
104 * `QUnit.start()` to kick them off).
105 */
106 startTests?: boolean | undefined;
107
108 /**
109 * If `false` the default Ember.Test adapter will not be updated.
110 */
111 setupTestAdapter?: boolean | undefined;
112
113 /**
114 * `false` opts out of the default behavior of setting `Ember.testing`
115 * to `true` before all tests and back to `false` after each test will.
116 */
117 setupEmberTesting?: boolean | undefined;
118
119 /**
120 * If `false` validation of `Ember.onerror` will be disabled.
121 */
122 setupEmberOnerrorValidation?: boolean | undefined;
123
124 /**
125 * If `false` test isolation validation will be disabled.
126 */
127 setupTestIsolationValidation?: boolean | undefined;
128}
129
130export function start(options?: QUnitStartOptions): void;
131
132// SAFETY: all of the `TC extends TestContext` generics below are just wildly,
133// impossibly unsafe. QUnit cannot -- ever! -- guarantee that the test context
134// is properly set up in a type-safe way to match this. However, it is the only
135// way to handle setting state in a TS-visible way prior to Ember RFC 0785,
136// which is slooooowly rolling out across the ecosystem in conjunction with the
137// `<template>` feature.
138
139declare global {
140 // NOTE: disables `no-unnecessary-generics` inline because, unfortunately,
141 // the design of Ember's test tooling (and indeed *QUnit's* test system)
142 // requires that we allow users to update the type of the context of the
143 // test. This is indeed strictly *wrong*! However, changing it will require
144 // changing how Ember handles testing. See [the PR][pr] for further details.
145 //
146 // [pr]: https://github.com/DefinitelyTyped/DefinitelyTyped/pull/56494
147
148 interface NestedHooks {
149 /**
150 * Runs after the last test. If additional tests are defined after the
151 * module's queue has emptied, it will not run this hook again.
152 */
153 after<TC extends TestContext>(
154 fn: (this: TC, assert: Assert) => void | Promise<void>
155 ): void;
156
157 /**
158 * Runs after each test.
159 */
160 afterEach<TC extends TestContext>(
161 fn: (this: TC, assert: Assert) => void | Promise<void>
162 ): void;
163
164 /**
165 * Runs before the first test.
166 */
167 // SAFETY: this is just wildly, impossibly unsafe. QUnit cannot -- ever! --
168 before<TC extends TestContext>(
169 fn: (this: TC, assert: Assert) => void | Promise<void>
170 ): void;
171
172 /**
173 * Runs before each test.
174 */
175 // SAFETY: this is just wildly, impossibly unsafe. QUnit cannot -- ever! --
176 beforeEach<TC extends TestContext>(
177 fn: (this: TC, assert: Assert) => void | Promise<void>
178 ): void;
179 }
180
181 interface QUnit {
182 /**
183 * Add a test to run.
184 *
185 * Add a test to run using `QUnit.test()`.
186 *
187 * The `assert` argument to the callback contains all of QUnit's assertion
188 * methods. Use this argument to call your test assertions.
189 *
190 * `QUnit.test()` can automatically handle the asynchronous resolution of a
191 * Promise on your behalf if you return a thenable Promise as the result of
192 * your callback function.
193 *
194 * @param name Title of unit being tested
195 * @param callback Function to close over assertions
196 */
197 // SAFETY: this is just wildly, impossibly unsafe. QUnit cannot -- ever! --
198 // provide this guarantee. However, it's also the only way to support TS
199 // in tests in Ember until we move the community over entirely to using
200 // `<template>` and local scope.
201 test<TC extends TestContext>(
202 name: string,
203 callback: (this: TC, assert: Assert) => void | Promise<unknown>
204 ): void;
205
206 /**
207 * Adds a test to exclusively run, preventing all other tests from running.
208 *
209 * Use this method to focus your test suite on a specific test. QUnit.only
210 * will cause any other tests in your suite to be ignored.
211 *
212 * Note, that if more than one QUnit.only is present only the first instance
213 * will run.
214 *
215 * This is an alternative to filtering tests to run in the HTML reporter. It
216 * is especially useful when you use a console reporter or in a codebase
217 * with a large set of long running tests.
218 *
219 * @param name Title of unit being tested
220 * @param callback Function to close over assertions
221 */
222 // SAFETY: this is just wildly, impossibly unsafe. QUnit cannot -- ever! --
223 // provide this guarantee. However, it's also the only way to support TS
224 // in tests in Ember until we move the community over entirely to using
225 // `<template>` and local scope.
226 only<TC extends TestContext>(
227 name: string,
228 callback: (this: TC, assert: Assert) => void | Promise<unknown>
229 ): void;
230
231 /**
232 * Use this method to test a unit of code which is still under development (in a “todo” state).
233 * The test will pass as long as one failing assertion is present.
234 *
235 * If all assertions pass, then the test will fail signaling that `QUnit.todo` should
236 * be replaced by `QUnit.test`.
237 *
238 * @param name Title of unit being tested
239 * @param callback Function to close over assertions
240 */
241 // SAFETY: this is just wildly, impossibly unsafe. QUnit cannot -- ever! --
242 // provide this guarantee. However, it's also the only way to support TS
243 // in tests in Ember until we move the community over entirely to using
244 // `<template>` and local scope.
245 todo<TC extends TestContext>(
246 name: string,
247 callback: (this: TC, assert: Assert) => void | Promise<unknown>
248 ): void;
249
250 /**
251 * Adds a test like object to be skipped.
252 *
253 * Use this method to replace QUnit.test() instead of commenting out entire
254 * tests.
255 *
256 * This test's prototype will be listed on the suite as a skipped test,
257 * ignoring the callback argument and the respective global and module's
258 * hooks.
259 *
260 * @param name Title of unit being tested
261 * @param callback Function to close over assertions
262 */
263 // SAFETY: this is just wildly, impossibly unsafe. QUnit cannot -- ever! --
264 // provide this guarantee. However, it's also the only way to support TS
265 // in tests in Ember until we move the community over entirely to using
266 // `<template>` and local scope.
267 skip<TC extends TestContext>(
268 name: string,
269 callback?: (this: TC, assert: Assert) => void | Promise<unknown>
270 ): void;
271 }
272}