UNPKG

9.13 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 EmberResolver from 'ember-resolver';
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: EmberResolver): void;
17
18interface SetupTestOptions {
19 /**
20 * The resolver to use when instantiating container-managed entities in the test.
21 */
22 resolver?: EmberResolver | undefined;
23}
24
25/**
26 * Sets up acceptance tests.
27 *
28 * The `setupApplicationTest` function is used for all acceptance tests. It
29 * is invoked in the callback scope of a QUnit module (aka "nested module").
30 *
31 * Once invoked, all subsequent hooks.beforeEach and test invocations will
32 * have access to the following:
33 * * `this.owner` - the owner object that been set on the test context.
34 * * `this.pauseTest` and `this.resumeTest` - allow easy pausing/resuming of tests.
35 * * `this.element` which returns the DOM element representing the application's root element.
36 */
37export function setupApplicationTest(hooks: NestedHooks, options?: SetupTestOptions): void;
38
39/**
40 * Sets up tests that need to render snippets of templates.
41 *
42 * The setupRenderingTest method is used for tests that need to render
43 * snippets of templates. It is also invoked in the callback scope of a
44 * QUnit module (aka "nested module").
45 *
46 * Once invoked, all subsequent hooks.beforeEach and test invocations will
47 * have access to the following:
48 * * All of the methods / properties listed for `setupTest`
49 * * this.render(...) - Renders the provided template snippet returning a
50 * promise that resolves once rendering has completed
51 * * An importable render function that de-sugars into this.render will be
52 * the default output of blueprints
53 * * this.element - Returns the native DOM element representing the element
54 * that was rendered via this.render
55 * * this.$(...) - When jQuery is present, executes a jQuery selector with
56 * the current this.element as its root
57 */
58export function setupRenderingTest(hooks: NestedHooks, options?: SetupTestOptions): void;
59
60/**
61 * Sets up tests that do not need to render snippets of templates.
62 *
63 * The `setupTest` method is used for all types of tests except for those
64 * that need to render snippets of templates. It is invoked in the callback
65 * scope of a QUnit module (aka "nested module").
66 *
67 * Once invoked, all subsequent hooks.beforeEach and test invocations will
68 * have access to the following:
69 * * this.owner - This exposes the standard "owner API" for the test environment.
70 * * this.set / this.setProperties - Allows setting values on the test context.
71 * * this.get / this.getProperties - Retrieves values from the test context.
72 */
73export function setupTest(hooks: NestedHooks, options?: SetupTestOptions): void;
74
75export class QUnitAdapter extends EmberTestAdapter {}
76
77export { module, test, skip, only, todo } from 'qunit';
78
79interface QUnitStartOptions {
80 /**
81 * If `false` tests will not be loaded automatically.
82 */
83 loadTests?: boolean | undefined;
84
85 /**
86 * If `false` the test container will not be setup based on `devmode`,
87 * `dockcontainer`, or `nocontainer` URL params.
88 */
89 setupTestContainer?: boolean | undefined;
90
91 /**
92 * If `false` tests will not be automatically started (you must run
93 * `QUnit.start()` to kick them off).
94 */
95 startTests?: boolean | undefined;
96
97 /**
98 * If `false` the default Ember.Test adapter will not be updated.
99 */
100 setupTestAdapter?: boolean | undefined;
101
102 /**
103 * `false` opts out of the default behavior of setting `Ember.testing`
104 * to `true` before all tests and back to `false` after each test will.
105 */
106 setupEmberTesting?: boolean | undefined;
107
108 /**
109 * If `false` validation of `Ember.onerror` will be disabled.
110 */
111 setupEmberOnerrorValidation?: boolean | undefined;
112
113 /**
114 * If `false` test isolation validation will be disabled.
115 */
116 setupTestIsolationValidation?: boolean | undefined;
117}
118
119export function start(options?: QUnitStartOptions): void;
120
121declare global {
122 // NOTE: disables `no-unnecessary-generics` inline because, unfortunately,
123 // the design of Ember's test tooling (and indeed *QUnit's* test system)
124 // requires that we allow users to update the type of the context of the
125 // test. This is indeed strictly *wrong*! However, changing it will require
126 // changing how Ember handles testing. See [the PR][pr] for further details.
127 //
128 // [pr]: https://github.com/DefinitelyTyped/DefinitelyTyped/pull/56494
129
130 interface NestedHooks {
131 /**
132 * Runs after the last test. If additional tests are defined after the
133 * module's queue has emptied, it will not run this hook again.
134 */
135 // tslint:disable-next-line no-unnecessary-generics
136 after<TC extends TestContext>(fn: (this: TC, assert: Assert) => void | Promise<void>): void;
137
138 /**
139 * Runs after each test.
140 */
141 // tslint:disable-next-line no-unnecessary-generics
142 afterEach<TC extends TestContext>(fn: (this: TC, assert: Assert) => void | Promise<void>): void;
143
144 /**
145 * Runs before the first test.
146 */
147 // tslint:disable-next-line no-unnecessary-generics
148 before<TC extends TestContext>(fn: (this: TC, assert: Assert) => void | Promise<void>): void;
149
150 /**
151 * Runs before each test.
152 */
153 // tslint:disable-next-line no-unnecessary-generics
154 beforeEach<TC extends TestContext>(fn: (this: TC, assert: Assert) => void | Promise<void>): void;
155 }
156
157 interface QUnit {
158 /**
159 * Add a test to run.
160 *
161 * Add a test to run using `QUnit.test()`.
162 *
163 * The `assert` argument to the callback contains all of QUnit's assertion
164 * methods. Use this argument to call your test assertions.
165 *
166 * `QUnit.test()` can automatically handle the asynchronous resolution of a
167 * Promise on your behalf if you return a thenable Promise as the result of
168 * your callback function.
169 *
170 * @param name Title of unit being tested
171 * @param callback Function to close over assertions
172 */
173 // tslint:disable-next-line no-unnecessary-generics
174 test<TC extends TestContext>(name: string, callback: (this: TC, assert: Assert) => void | Promise<void>): void;
175
176 /**
177 * Adds a test to exclusively run, preventing all other tests from running.
178 *
179 * Use this method to focus your test suite on a specific test. QUnit.only
180 * will cause any other tests in your suite to be ignored.
181 *
182 * Note, that if more than one QUnit.only is present only the first instance
183 * will run.
184 *
185 * This is an alternative to filtering tests to run in the HTML reporter. It
186 * is especially useful when you use a console reporter or in a codebase
187 * with a large set of long running tests.
188 *
189 * @param name Title of unit being tested
190 * @param callback Function to close over assertions
191 */
192 // tslint:disable-next-line no-unnecessary-generics
193 only<TC extends TestContext>(name: string, callback: (this: TC, assert: Assert) => void | Promise<void>): void;
194
195 /**
196 * Use this method to test a unit of code which is still under development (in a “todo” state).
197 * The test will pass as long as one failing assertion is present.
198 *
199 * If all assertions pass, then the test will fail signaling that `QUnit.todo` should
200 * be replaced by `QUnit.test`.
201 *
202 * @param name Title of unit being tested
203 * @param callback Function to close over assertions
204 */
205 // tslint:disable-next-line no-unnecessary-generics
206 todo<TC extends TestContext>(name: string, callback: (this: TC, assert: Assert) => void | Promise<void>): void;
207
208 /**
209 * Adds a test like object to be skipped.
210 *
211 * Use this method to replace QUnit.test() instead of commenting out entire
212 * tests.
213 *
214 * This test's prototype will be listed on the suite as a skipped test,
215 * ignoring the callback argument and the respective global and module's
216 * hooks.
217 *
218 * @param name Title of unit being tested
219 * @param callback Function to close over assertions
220 */
221 // tslint:disable-next-line no-unnecessary-generics
222 skip<TC extends TestContext>(name: string, callback?: (this: TC, assert: Assert) => void | Promise<void>): void;
223 }
224}
225
\No newline at end of file