Skip to content

Commit

Permalink
chore: update eslint-plugin-unicorn (#15336)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB authored Oct 4, 2024
1 parent b0eb836 commit 0a0a9f7
Show file tree
Hide file tree
Showing 24 changed files with 800 additions and 816 deletions.
1 change: 1 addition & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ module.exports = {
'unicorn/no-static-only-class': 'off',
'unicorn/prefer-number-properties': 'off',
'unicorn/prefer-string-raw': 'off',
'unicorn/prefer-global-this': 'off',
},
},
// demonstration of matchers usage
Expand Down
26 changes: 13 additions & 13 deletions e2e/__tests__/__snapshots__/wrongEnv.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ exports[`Wrong globals for environment on node <=18 print useful error for navig
ReferenceError: navigator is not defined
30 |
31 | test('use navigator', () => {
> 32 | const userAgent = navigator.userAgent;
31 |
32 | test('use navigator', () => {
> 33 | const userAgent = navigator.userAgent;
| ^
33 |
34 | console.log(userAgent);
35 |
34 |
35 | console.log(userAgent);
36 |
at Object.navigator (__tests__/node.js:32:21)"
at Object.navigator (__tests__/node.js:33:21)"
`;
exports[`Wrong globals for environment print useful error for document 1`] = `
Expand Down Expand Up @@ -83,15 +83,15 @@ exports[`Wrong globals for environment print useful error for window 1`] = `
ReferenceError: window is not defined
22 |
23 | test('use window', () => {
> 24 | const location = window.location;
24 | // eslint-disable-next-line unicorn/prefer-global-this
> 25 | const location = window.location;
| ^
25 |
26 | console.log(location);
27 |
26 |
27 | console.log(location);
28 |
at Object.window (__tests__/node.js:24:20)"
at Object.window (__tests__/node.js:25:20)"
`;
exports[`Wrong globals for environment print useful error when it explodes during evaluation 1`] = `
Expand Down
4 changes: 2 additions & 2 deletions e2e/console-jsdom/__tests__/console.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ test('can mock console.error calls from jsdom', () => {

function onError(event) {}

window.addEventListener('error', onError);
globalThis.addEventListener('error', onError);
fakeNode.addEventListener(evtType, callCallback, false);
evt.initEvent(evtType, false, false);
fakeNode.dispatchEvent(evt);
window.removeEventListener('error', onError);
globalThis.removeEventListener('error', onError);

expect(console.error).toHaveBeenCalledTimes(1);
expect(console.error).toHaveBeenCalledWith(
Expand Down
1 change: 1 addition & 0 deletions e2e/env-test/__tests__/equivalent.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

const {isArrayBuffer} = require('util').types;
const isJSDOM =
// eslint-disable-next-line unicorn/prefer-global-this
typeof window !== 'undefined' && typeof document !== 'undefined';

const skipTestJSDOM = isJSDOM ? test.skip : test;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
/*eslint-env browser */

test('found url jestjs.io', () => {
expect(window.location.href).toBe('https://jestjs.io/');
expect(globalThis.location.href).toBe('https://jestjs.io/');
});
6 changes: 3 additions & 3 deletions e2e/fake-timers/do-not-fake/__tests__/doNotFake.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@
'use strict';

const mockPerformanceMark = jest.fn();
window.performance.mark = mockPerformanceMark;
globalThis.performance.mark = mockPerformanceMark;

test('fakes all APIs', () => {
jest.useFakeTimers();

expect(window.performance.mark).toBeUndefined();
expect(globalThis.performance.mark).toBeUndefined();
});

test('does not fake `performance` instance', () => {
jest.useFakeTimers({doNotFake: ['performance']});

expect(window.performance.mark).toBe(mockPerformanceMark);
expect(globalThis.performance.mark).toBe(mockPerformanceMark);
});
2 changes: 1 addition & 1 deletion e2e/nested-event-loop/__tests__/nestedEventLoop.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ it('can assert on errors across nested event loops', () => {
throw new Error('This should be caught.');
});
let caught = null;
window.addEventListener('error', e => {
globalThis.addEventListener('error', e => {
caught = e.error;
});
expect(() => {
Expand Down
6 changes: 2 additions & 4 deletions e2e/override-globals/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,8 @@ describe('parent', () => {
});

it('can override atob and btoa', () => {
// eslint-disable-next-line no-restricted-globals
global.atob = () => 'hello';
// eslint-disable-next-line no-restricted-globals
global.btoa = () => 'there';
globalThis.atob = () => 'hello';
globalThis.btoa = () => 'there';

expect(`${atob()} ${btoa()}`).toBe('hello there');
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
/*eslint-env browser */

test('use jsdom and set the URL in this test file', () => {
expect(window.location.href).toBe('https://jestjs.io/');
expect(globalThis.location.href).toBe('https://jestjs.io/');
});
1 change: 1 addition & 0 deletions e2e/wrong-env/__tests__/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ test('use document', () => {
});

test('use window', () => {
// eslint-disable-next-line unicorn/prefer-global-this
const location = window.location;

console.log(location);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"eslint-plugin-markdown": "^3.0.0",
"eslint-plugin-prettier": "^5.0.0",
"eslint-plugin-promise": "^6.1.1",
"eslint-plugin-unicorn": "^55.0.0",
"eslint-plugin-unicorn": "^56.0.0",
"execa": "^5.0.0",
"find-process": "^1.4.1",
"glob": "^10.3.10",
Expand Down
8 changes: 4 additions & 4 deletions packages/diff-sequences/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ const extendPathsF = (
);

// Optimization: skip diagonals in which paths cannot ever overlap.
const nF = d < iMaxF ? d : iMaxF;
const nF = Math.min(d, iMaxF);

// The diagonals kF are odd when d is odd and even when d is even.
for (iF += 1, kF += 2; iF <= nF; iF += 1, kF += 2) {
Expand Down Expand Up @@ -217,7 +217,7 @@ const extendPathsR = (
);

// Optimization: skip diagonals in which paths cannot ever overlap.
const nR = d < iMaxR ? d : iMaxR;
const nR = Math.min(d, iMaxR);

// The diagonals kR are odd when d is odd and even when d is even.
for (iR += 1, kR -= 2; iR <= nR; iR += 1, kR -= 2) {
Expand Down Expand Up @@ -278,7 +278,7 @@ const extendOverlappablePathsF = (
let aIndexPrev1 = NOT_YET_SET; // prev value of [iF - 1] in next iteration

// Optimization: skip diagonals in which paths cannot ever overlap.
const nF = d < iMaxF ? d : iMaxF;
const nF = Math.min(d, iMaxF);

// The diagonals kF = 2 * iF - d are odd when d is odd and even when d is even.
for (let iF = 0, kF = -d; iF <= nF; iF += 1, kF += 2) {
Expand Down Expand Up @@ -411,7 +411,7 @@ const extendOverlappablePathsR = (
let aIndexPrev1 = NOT_YET_SET; // prev value of [iR - 1] in next iteration

// Optimization: skip diagonals in which paths cannot ever overlap.
const nR = d < iMaxR ? d : iMaxR;
const nR = Math.min(d, iMaxR);

// The diagonals kR = d - 2 * iR are odd when d is odd and even when d is even.
for (let iR = 0, kR = d; iR <= nR; iR += 1, kR -= 2) {
Expand Down
11 changes: 5 additions & 6 deletions packages/jest-circus/src/eventHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,8 @@ const eventHandler: Circus.EventHandler = (event, state) => {
}
case 'test_retry': {
const logErrorsBeforeRetry: boolean =
// eslint-disable-next-line no-restricted-globals
((global as Global.Global)[LOG_ERRORS_BEFORE_RETRY] as any) || false;
((globalThis as Global.Global)[LOG_ERRORS_BEFORE_RETRY] as any) ||
false;
if (logErrorsBeforeRetry) {
event.test.retryReasons.push(...event.test.errors);
}
Expand All @@ -226,13 +226,12 @@ const eventHandler: Circus.EventHandler = (event, state) => {
}
case 'run_start': {
state.hasStarted = true;
/* eslint-disable no-restricted-globals */
if ((global as Global.Global)[TEST_TIMEOUT_SYMBOL]) {
state.testTimeout = (global as Global.Global)[
if ((globalThis as Global.Global)[TEST_TIMEOUT_SYMBOL]) {
state.testTimeout = (globalThis as Global.Global)[
TEST_TIMEOUT_SYMBOL
] as number;
}
/* eslint-enable */

break;
}
case 'run_finish': {
Expand Down
10 changes: 4 additions & 6 deletions packages/jest-circus/src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,17 @@ const _runTestsForDescribeBlock = async (

// Tests that fail and are retried we run after other tests
const retryTimes =
// eslint-disable-next-line no-restricted-globals
Number.parseInt((global as Global.Global)[RETRY_TIMES] as string, 10) || 0;
Number.parseInt((globalThis as Global.Global)[RETRY_TIMES] as string, 10) ||
0;

const waitBeforeRetry =
Number.parseInt(
// eslint-disable-next-line no-restricted-globals
(global as Global.Global)[WAIT_BEFORE_RETRY] as string,
(globalThis as Global.Global)[WAIT_BEFORE_RETRY] as string,
10,
) || 0;

const retryImmediately: boolean =
// eslint-disable-next-line no-restricted-globals
((global as Global.Global)[RETRY_IMMEDIATELY] as any) || false;
((globalThis as Global.Global)[RETRY_IMMEDIATELY] as any) || false;

const deferredRetryTests = [];

Expand Down
8 changes: 3 additions & 5 deletions packages/jest-circus/src/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,16 @@ const createState = (): Circus.State => {
};
};

/* eslint-disable no-restricted-globals */
export const resetState = (): void => {
(global as Global.Global)[STATE_SYM] = createState();
(globalThis as Global.Global)[STATE_SYM] = createState();
};

resetState();

export const getState = (): Circus.State =>
(global as Global.Global)[STATE_SYM] as Circus.State;
(globalThis as Global.Global)[STATE_SYM] as Circus.State;
export const setState = (state: Circus.State): Circus.State =>
((global as Global.Global)[STATE_SYM] = state);
/* eslint-enable */
((globalThis as Global.Global)[STATE_SYM] = state);

export const dispatch = async (event: Circus.AsyncEvent): Promise<void> => {
for (const handler of eventHandlers) {
Expand Down
6 changes: 2 additions & 4 deletions packages/jest-jasmine2/src/jasmine/jasmineLight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,13 @@ export const create = function (createOptions: Record<string, any>): Jasmine {
enumerable: true,
get() {
return (
// eslint-disable-next-line no-restricted-globals
(global as Global.Global)[testTimeoutSymbol] ||
(globalThis as Global.Global)[testTimeoutSymbol] ||
createOptions.testTimeout ||
5000
);
},
set(value) {
// eslint-disable-next-line no-restricted-globals
(global as Global.Global)[testTimeoutSymbol] = value;
(globalThis as Global.Global)[testTimeoutSymbol] = value;
},
});

Expand Down
6 changes: 2 additions & 4 deletions packages/jest-jasmine2/src/jestExpect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@ import type {Global} from '@jest/types';
import type {JasmineMatchersObject} from './types';

export default function jestExpectAdapter(config: {expand: boolean}): void {
// eslint-disable-next-line no-restricted-globals
(global as Global.Global).expect = jestExpect;
(globalThis as Global.Global).expect = jestExpect;
jestExpect.setState({expand: config.expand});

// eslint-disable-next-line no-restricted-globals
const jasmine = (global as Global.Global).jasmine;
const jasmine = (globalThis as Global.Global).jasmine;
jasmine.anything = jestExpect.anything;
jasmine.any = jestExpect.any;
jasmine.objectContaining = jestExpect.objectContaining;
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-matcher-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ type PrintLabel = (string: string) => string;

export const getLabelPrinter = (...strings: Array<string>): PrintLabel => {
const maxLength = strings.reduce(
(max, string) => (string.length > max ? string.length : max),
(max, string) => Math.max(string.length, max),
0,
);
return (string: string): string =>
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-mock/src/__tests__/window-spy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
/* eslint-env browser*/

function exampleDispatch() {
window.dispatchEvent(new CustomEvent('event', {}));
globalThis.dispatchEvent(new CustomEvent('event', {}));
}

describe('spy on `dispatchEvent`', () => {
const dispatchEventSpy = jest.spyOn(window, 'dispatchEvent');
const dispatchEventSpy = jest.spyOn(globalThis, 'dispatchEvent');

it('should be called', () => {
exampleDispatch();
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-resolve/src/nodeModulesPaths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ function findGlobalPaths(): Array<string> {
if (resolvePaths) {
// the global paths start one after the root node_modules
const rootIndex = resolvePaths.indexOf(globalPath);
return rootIndex > -1 ? resolvePaths.slice(rootIndex + 1) : [];
return rootIndex === -1 ? [] : resolvePaths.slice(rootIndex + 1);
}
return [];
}
Expand Down
6 changes: 3 additions & 3 deletions packages/jest-snapshot/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -411,10 +411,10 @@ const traverseAst = (
[],
);

if (snapshotIndex > -1) {
args[snapshotIndex] = replacementNode;
} else {
if (snapshotIndex === -1) {
args.push(replacementNode);
} else {
args[snapshotIndex] = replacementNode;
}
});

Expand Down
2 changes: 1 addition & 1 deletion packages/pretty-format/src/__tests__/DOMElement.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ setPrettyPrint([DOMElement]);
describe('pretty-format', () => {
// Test is not related to plugin but is related to jsdom testing environment.
it('prints global window as constructor name alone', () => {
expect(prettyFormat(window)).toBe('[Window]');
expect(prettyFormat(globalThis)).toBe('[Window]');
});
});

Expand Down
1 change: 1 addition & 0 deletions packages/pretty-format/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ const getConstructorName = (val: new (...args: Array<any>) => unknown) =>
/* global window */
/** Is val is equal to global window object? Works even if it does not exist :) */
const isWindow = (val: unknown) =>
// eslint-disable-next-line unicorn/prefer-global-this
typeof window !== 'undefined' && val === window;

const SYMBOL_REGEXP = /^Symbol\((.*)\)(.*)$/;
Expand Down
Loading

0 comments on commit 0a0a9f7

Please sign in to comment.