MCPcopy Create free account
hub / github.com/ampproject/amphtml / overwriteAlwaysUseSuper

Function overwriteAlwaysUseSuper

build-system/tasks/e2e/expect.js:165–226  ·  view source on GitHub ↗

* @param {Chai.ChaiUtils} utils * @return {function(Chai.AssertionStatic): function(): any}

(utils)

Source from the content-addressed store, hash-verified

163 * @return {function(Chai.AssertionStatic): function(): any}
164 */
165function overwriteAlwaysUseSuper(utils) {
166 const {flag} = utils;
167
168 /**
169 * @param {Chai.AssertionStatic} _super
170 * @return {function(): ReturnType<Chai.AssertionStatic>}
171 */
172 return function (_super) {
173 return function () {
174 // @ts-ignore
175 const that = this;
176 const obj = that._obj;
177 const isControllerPromise = obj instanceof ControllerPromise;
178 if (!isControllerPromise) {
179 return _super.apply(that, arguments);
180 }
181 const {waitForValue} = obj;
182 if (!waitForValue) {
183 return obj.then((result) => {
184 flag(that, 'object', result);
185 return _super.apply(that, arguments);
186 });
187 }
188
189 /**
190 * When passed to `waitForValue`, this method causes the Promise
191 * returned by `waitForValue` to resolve only when the value it
192 * polls matches expectation set by the `expect` chain.
193 * @param {*} value
194 * @return {boolean} true if the ControllerPromise polling value
195 * satisfies the `expect` chain.
196 */
197 const valueSatisfiesExpectation = (value) => {
198 try {
199 // Tell chai to use value as the subject of the expect chain.
200 flag(that, 'object', value);
201
202 // Run the code that checks the condition.
203 _super.apply(that, arguments);
204
205 clearLastExpectError();
206 // Let waitForValue know we are done.
207 return true;
208 } catch (e) {
209 // The condition checking code threw an error, so we are not done
210 // yet. Save the error so that it can be grabbed in case the test
211 // times out.
212 lastExpectError = e;
213 return false;
214 } finally {
215 flag(that, 'object', resultPromise);
216 }
217 };
218
219 const resultPromise = /** @type {(arg: *) => Promise<*>} */ (
220 waitForValue
221 )(valueSatisfiesExpectation);
222 flag(that, 'object', resultPromise);

Callers 2

installWrappersFunction · 0.85
installBrowserWrappersFunction · 0.85

Calls 2

applyMethod · 0.80
thenMethod · 0.45

Tested by

no test coverage detected