Commit ddfddd67 authored by Mark Florian's avatar Mark Florian

Lint against setImmediate usage in specs

This does not replace some instances of `setImmediate` in helpers and
test environment configuration files, as these are well-contained, and
can be rewritten later, if need be. The addition of the lint rule should
prevent more usages of `setImmediate` appearing.

This is the final part of
https://gitlab.com/gitlab-org/gitlab/-/issues/344247.
parent ce5fcf69
......@@ -105,6 +105,8 @@ overrides:
message: 'Avoid using "setData" on VTU wrapper'
- selector: MemberExpression[object.type!='ThisExpression'][property.type='Identifier'][property.name='$nextTick']
message: 'Using $nextTick from a component instance is discouraged. Import nextTick directly from the Vue package.'
- selector: Identifier[name='setImmediate']
message: 'Prefer explicit waitForPromises (or equivalent), or jest.runAllTimers (or equivalent) to vague setImmediate calls.'
- files:
- 'config/**/*'
- 'scripts/**/*'
......
export default function flushPromises() {
// eslint-disable-next-line no-restricted-syntax
return new Promise(setImmediate);
}
......@@ -25,6 +25,7 @@ const onRequest = () => {
// Use setImmediate to alloow the response interceptor to finish
const onResponse = (config) => {
activeRequests -= 1;
// eslint-disable-next-line no-restricted-syntax
setImmediate(() => {
events.emit('response', config);
});
......@@ -43,6 +44,7 @@ const subscribeToResponse = (predicate = () => true) =>
// If a request has been made synchronously, setImmediate waits for it to be
// processed and the counter incremented.
// eslint-disable-next-line no-restricted-syntax
setImmediate(listener);
});
......
......@@ -116,6 +116,7 @@ export default (
payload,
);
// eslint-disable-next-line no-restricted-syntax
return (result || new Promise((resolve) => setImmediate(resolve)))
.catch((error) => {
validateResults();
......
......@@ -123,6 +123,7 @@ class CustomEnvironment extends JSDOMEnvironment {
// Reset `Date` so that Jest can report timing accurately *roll eyes*...
setGlobalDateToRealDate();
// eslint-disable-next-line no-restricted-syntax
await new Promise(setImmediate);
if (this.rejectedPromises.length > 0) {
......
......@@ -8,6 +8,7 @@ initializeTestTimeout(process.env.CI ? 6000 : 500);
afterEach(() =>
// give Promises a bit more time so they fail the right test
// eslint-disable-next-line no-restricted-syntax
new Promise(setImmediate).then(() => {
// wait for pending setTimeout()s
jest.runOnlyPendingTimers();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment