Commit 61db02c2 authored by Vitaly Slobodin's avatar Vitaly Slobodin

Merge branch '344247-replace-setimmediate-in-tests' into 'master'

Lint against setImmediate usage in specs

See merge request gitlab-org/gitlab!80328
parents 73495489 ddfddd67
...@@ -105,6 +105,8 @@ overrides: ...@@ -105,6 +105,8 @@ overrides:
message: 'Avoid using "setData" on VTU wrapper' message: 'Avoid using "setData" on VTU wrapper'
- selector: MemberExpression[object.type!='ThisExpression'][property.type='Identifier'][property.name='$nextTick'] - 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.' 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: - files:
- 'config/**/*' - 'config/**/*'
- 'scripts/**/*' - 'scripts/**/*'
......
export default function flushPromises() { export default function flushPromises() {
// eslint-disable-next-line no-restricted-syntax
return new Promise(setImmediate); return new Promise(setImmediate);
} }
...@@ -25,6 +25,7 @@ const onRequest = () => { ...@@ -25,6 +25,7 @@ const onRequest = () => {
// Use setImmediate to alloow the response interceptor to finish // Use setImmediate to alloow the response interceptor to finish
const onResponse = (config) => { const onResponse = (config) => {
activeRequests -= 1; activeRequests -= 1;
// eslint-disable-next-line no-restricted-syntax
setImmediate(() => { setImmediate(() => {
events.emit('response', config); events.emit('response', config);
}); });
...@@ -43,6 +44,7 @@ const subscribeToResponse = (predicate = () => true) => ...@@ -43,6 +44,7 @@ const subscribeToResponse = (predicate = () => true) =>
// If a request has been made synchronously, setImmediate waits for it to be // If a request has been made synchronously, setImmediate waits for it to be
// processed and the counter incremented. // processed and the counter incremented.
// eslint-disable-next-line no-restricted-syntax
setImmediate(listener); setImmediate(listener);
}); });
......
...@@ -116,6 +116,7 @@ export default ( ...@@ -116,6 +116,7 @@ export default (
payload, payload,
); );
// eslint-disable-next-line no-restricted-syntax
return (result || new Promise((resolve) => setImmediate(resolve))) return (result || new Promise((resolve) => setImmediate(resolve)))
.catch((error) => { .catch((error) => {
validateResults(); validateResults();
......
...@@ -123,6 +123,7 @@ class CustomEnvironment extends JSDOMEnvironment { ...@@ -123,6 +123,7 @@ class CustomEnvironment extends JSDOMEnvironment {
// Reset `Date` so that Jest can report timing accurately *roll eyes*... // Reset `Date` so that Jest can report timing accurately *roll eyes*...
setGlobalDateToRealDate(); setGlobalDateToRealDate();
// eslint-disable-next-line no-restricted-syntax
await new Promise(setImmediate); await new Promise(setImmediate);
if (this.rejectedPromises.length > 0) { if (this.rejectedPromises.length > 0) {
......
...@@ -8,6 +8,7 @@ initializeTestTimeout(process.env.CI ? 6000 : 500); ...@@ -8,6 +8,7 @@ initializeTestTimeout(process.env.CI ? 6000 : 500);
afterEach(() => afterEach(() =>
// give Promises a bit more time so they fail the right test // give Promises a bit more time so they fail the right test
// eslint-disable-next-line no-restricted-syntax
new Promise(setImmediate).then(() => { new Promise(setImmediate).then(() => {
// wait for pending setTimeout()s // wait for pending setTimeout()s
jest.runOnlyPendingTimers(); 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