mirror of https://github.com/facebook/jest.git
chore: update `eslint-plugin-unicorn` (#15336)
This commit is contained in:
parent
b0eb836868
commit
0a0a9f7375
|
@ -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
|
||||
|
|
|
@ -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`] = `
|
||||
|
@ -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`] = `
|
||||
|
|
|
@ -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(
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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/');
|
||||
});
|
||||
|
|
|
@ -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);
|
||||
});
|
||||
|
|
|
@ -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(() => {
|
||||
|
|
|
@ -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');
|
||||
});
|
||||
|
|
|
@ -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/');
|
||||
});
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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) {
|
||||
|
@ -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) {
|
||||
|
@ -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) {
|
||||
|
@ -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) {
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
@ -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': {
|
||||
|
|
|
@ -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 = [];
|
||||
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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;
|
||||
},
|
||||
});
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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 =>
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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 [];
|
||||
}
|
||||
|
|
|
@ -411,10 +411,10 @@ const traverseAst = (
|
|||
[],
|
||||
);
|
||||
|
||||
if (snapshotIndex > -1) {
|
||||
args[snapshotIndex] = replacementNode;
|
||||
} else {
|
||||
if (snapshotIndex === -1) {
|
||||
args.push(replacementNode);
|
||||
} else {
|
||||
args[snapshotIndex] = replacementNode;
|
||||
}
|
||||
});
|
||||
|
||||
|
|
|
@ -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]');
|
||||
});
|
||||
});
|
||||
|
||||
|
|
|
@ -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\((.*)\)(.*)$/;
|
||||
|
|
Loading…
Reference in New Issue