Skip to content

Commit

Permalink
chore: correctly use new for JS builtins (jestjs#14800)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB authored Dec 28, 2023
1 parent ab3f9ae commit 175665e
Show file tree
Hide file tree
Showing 10 changed files with 10 additions and 14 deletions.
1 change: 0 additions & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,6 @@ module.exports = {
'unicorn/consistent-function-scoping': 'off',
'unicorn/error-message': 'off',
'unicorn/escape-case': 'off',
'unicorn/new-for-builtins': 'off',
'unicorn/no-array-method-this-argument': 'off',
'unicorn/no-array-push-push': 'off',
'unicorn/no-await-expression-member': 'off',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ exports[`works with custom matchers 1`] = `
qux
44 | const baz = () => {
45 | // eslint-disable-next-line unicorn/throw-new-error
45 | // eslint-disable-next-line unicorn/throw-new-error,unicorn/new-for-builtins
> 46 | throw Error('qux');
| ^
47 | };
Expand Down
2 changes: 1 addition & 1 deletion e2e/custom-matcher-stack-trace/__tests__/sync.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe('Custom matcher', () => {
const foo = () => bar();
const bar = () => baz();
const baz = () => {
// eslint-disable-next-line unicorn/throw-new-error
// eslint-disable-next-line unicorn/throw-new-error,unicorn/new-for-builtins
throw Error('qux');
};

Expand Down
2 changes: 1 addition & 1 deletion packages/expect-utils/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ export const pathAsArray = (propertyPath: string): Array<any> => {
}

// will match everything that's not a dot or a bracket, and "" for consecutive dots.
const pattern = RegExp('[^.[\\]]+|(?=(?:\\.)(?:\\.|$))', 'g');
const pattern = new RegExp('[^.[\\]]+|(?=(?:\\.)(?:\\.|$))', 'g');

// Because the regex won't match a dot in the beginning of the path, if present.
if (propertyPath[0] === '.') {
Expand Down
5 changes: 2 additions & 3 deletions packages/expect/src/__tests__/asymmetricMatchers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,15 @@ test('Any.asymmetricMatch()', () => {

test('Any.asymmetricMatch() on primitive wrapper classes', () => {
for (const test of [
// eslint-disable-next-line no-new-wrappers
/* eslint-disable no-new-wrappers, unicorn/new-for-builtins */
any(String).asymmetricMatch(new String('jest')),
// eslint-disable-next-line no-new-wrappers
any(Number).asymmetricMatch(new Number(1)),
// eslint-disable-next-line no-new-func
any(Function).asymmetricMatch(new Function('() => {}')),
// eslint-disable-next-line no-new-wrappers
any(Boolean).asymmetricMatch(new Boolean(true)),
any(BigInt).asymmetricMatch(Object(1n)),
any(Symbol).asymmetricMatch(Object(Symbol())),
/* eslint-enable */
]) {
jestExpect(test).toBe(true);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/expect/src/__tests__/matchers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ describe('.toStrictEqual()', () => {
});

describe('.toEqual()', () => {
/* eslint-disable no-new-wrappers */
/* eslint-disable no-new-wrappers, unicorn/new-for-builtins */
for (const [a, b] of [
[true, false],
[1, 2],
Expand Down
4 changes: 1 addition & 3 deletions packages/jest-circus/src/__tests__/shuffleArray.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ describe('rngBuilder', () => {
// Some people will be using seeds relying on a particular order
test.each([1, 2, 4, 8, 16])('creates a randomizer given seed %s', seed => {
const rng = rngBuilder(seed);
const results = Array(10)
.fill(0)
.map(() => rng.next(0, 10));
const results = new Array(10).fill(0).map(() => rng.next(0, 10));
expect(results).toMatchSnapshot();
});
});
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-core/src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ const buildContextsAndHasteMaps = async (
globalConfig: Config.GlobalConfig,
outputStream: WriteStream,
) => {
const hasteMapInstances = Array(configs.length);
const hasteMapInstances = new Array(configs.length);
const contexts = await Promise.all(
configs.map(async (config, index) => {
createDirectory(config.cacheDirectory);
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-matcher-utils/src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ describe('stringify()', () => {
test('reduces maxWidth if stringifying very large arrays', () => {
const big: any = [];
const small: any = [];
const testString = Array(1000).join('x');
const testString = new Array(1000).join('x');

for (let i = 0; i < 100; i += 1) {
big[i] = testString;
Expand Down
2 changes: 1 addition & 1 deletion packages/pretty-format/src/__tests__/prettyFormat.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ describe('prettyFormat()', () => {

describe('maxWidth option', () => {
it('applies to arrays', () => {
const val = Array(1_000_000).fill('x');
const val = new Array(1_000_000).fill('x');
expect(prettyFormat(val, {maxWidth: 5})).toEqual(
[
'Array [',
Expand Down

0 comments on commit 175665e

Please sign in to comment.