Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: enforce no negated first condition #14532

Merged
merged 1 commit into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,7 @@ module.exports = {
'wrap-regex': 'off',
yoda: 'off',

'unicorn/no-negated-condition': 'error',
'unicorn/prefer-includes': 'error',
'unicorn/template-indent': 'error',
},
Expand Down
24 changes: 12 additions & 12 deletions docs/ExpectAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -1186,10 +1186,10 @@ function areVolumesEqual(a, b) {

if (isAVolume && isBVolume) {
return a.equals(b);
} else if (isAVolume !== isBVolume) {
return false;
} else {
} else if (isAVolume === isBVolume) {
return undefined;
} else {
return false;
}
}

Expand Down Expand Up @@ -1243,10 +1243,10 @@ function areVolumesEqual(a: unknown, b: unknown): boolean | undefined {

if (isAVolume && isBVolume) {
return a.equals(b);
} else if (isAVolume !== isBVolume) {
return false;
} else {
} else if (isAVolume === isBVolume) {
return undefined;
} else {
return false;
}
}

Expand Down Expand Up @@ -1297,10 +1297,10 @@ function areAuthorEqual(a, b) {
if (isAAuthor && isBAuthor) {
// Authors are equal if they have the same name
return a.name === b.name;
} else if (isAAuthor !== isBAuthor) {
return false;
} else {
} else if (isAAuthor === isBAuthor) {
return undefined;
} else {
return false;
}
}

Expand All @@ -1315,10 +1315,10 @@ function areBooksEqual(a, b, customTesters) {
return (
a.name === b.name && this.equals(a.authors, b.authors, customTesters)
);
} else if (isABook !== isBBook) {
return false;
} else {
} else if (isABook === isBBook) {
return undefined;
} else {
return false;
}
}

Expand Down
6 changes: 3 additions & 3 deletions e2e/custom-matcher-stack-trace/__tests__/sync.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
function toCustomMatch(callback, expectation) {
const actual = callback();

if (actual !== expectation) {
if (actual === expectation) {
return {pass: true};
} else {
return {
message: () => `Expected "${expectation}" but got "${actual}"`,
pass: false,
};
} else {
return {pass: true};
}
}

Expand Down
2 changes: 1 addition & 1 deletion e2e/custom-reporters/reporters/TestCaseStartReporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
class TestCaseStartReporter {
onTestCaseStart(test, testCaseStartInfo) {
const mode =
testCaseStartInfo.mode != null ? testCaseStartInfo.mode : 'undefined';
testCaseStartInfo.mode == null ? 'undefined' : testCaseStartInfo.mode;
console.log(
`onTestCaseStart: ${testCaseStartInfo.title}, ` +
`mode: ${mode}, ` +
Expand Down
6 changes: 3 additions & 3 deletions packages/create-jest/src/runCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,9 @@ export async function runCreate(rootDir = process.cwd()): Promise<void> {

// Determine Jest config path
const jestConfigPath =
existingJestConfigExt != null
? getConfigFilename(existingJestConfigExt)
: path.join(rootDir, getConfigFilename(jestConfigFileExt));
existingJestConfigExt == null
? path.join(rootDir, getConfigFilename(jestConfigFileExt))
: getConfigFilename(existingJestConfigExt);

const shouldModifyScripts = results.scripts;

Expand Down
6 changes: 3 additions & 3 deletions packages/expect/src/__tests__/customEqualityTesters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ const areVolumesEqual: Tester = (

if (isAVolume && isBVolume) {
return a.equals(b);
} else if (isAVolume !== isBVolume) {
return false;
} else {
} else if (isAVolume === isBVolume) {
return undefined;
} else {
return false;
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ const areAuthorsEqual: Tester = (a: unknown, b: unknown) => {

if (isAAuthor && isBAuthor) {
return a.name === b.name;
} else if (isAAuthor !== isBAuthor) {
return false;
} else {
} else if (isAAuthor === isBAuthor) {
return undefined;
} else {
return false;
}
};

Expand All @@ -64,10 +64,10 @@ const areBooksEqual: Tester = function (
return (
a.name === b.name && this.equals(a.authors, b.authors, customTesters)
);
} else if (isABook !== isBBook) {
return false;
} else {
} else if (isABook === isBBook) {
return undefined;
} else {
return false;
}
};

Expand Down
40 changes: 20 additions & 20 deletions packages/expect/src/matchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,11 @@ const matchers: MatchersObject = {
// eslint-disable-next-line prefer-template
matcherHint(matcherName, undefined, undefined, options) +
'\n\n' +
(deepEqualityName !== null
? `${DIM_COLOR(
(deepEqualityName === null
? ''
: `${DIM_COLOR(
`If it should pass with deep equality, replace "${matcherName}" with "${deepEqualityName}"`,
)}\n\n`
: '') +
)}\n\n`) +
printDiffOrStringify(
expected,
received,
Expand Down Expand Up @@ -330,12 +330,12 @@ const matchers: MatchersObject = {
? `\nReceived value has no prototype\nReceived value: ${printReceived(
received,
)}`
: typeof received.constructor !== 'function'
? `\nReceived value: ${printReceived(received)}`
: printReceivedConstructorName(
: typeof received.constructor === 'function'
? printReceivedConstructorName(
'Received constructor',
received.constructor,
));
)
: `\nReceived value: ${printReceived(received)}`);

return {message, pass};
},
Expand Down Expand Up @@ -628,9 +628,9 @@ const matchers: MatchersObject = {
matcherHint(matcherName, undefined, undefined, options) +
'\n\n' +
`Expected: not ${printExpected(expected)}\n` +
(stringify(expected) !== stringify(received)
? `Received: ${printReceived(received)}`
: '')
(stringify(expected) === stringify(received)
? ''
: `Received: ${printReceived(received)}`)
: () =>
// eslint-disable-next-line prefer-template
matcherHint(matcherName, undefined, undefined, options) +
Expand Down Expand Up @@ -777,9 +777,9 @@ const matchers: MatchersObject = {
(hasValue
? `Expected path: ${printExpected(expectedPath)}\n\n` +
`Expected value: not ${printExpected(expectedValue)}${
stringify(expectedValue) !== stringify(receivedValue)
? `\nReceived value: ${printReceived(receivedValue)}`
: ''
stringify(expectedValue) === stringify(receivedValue)
? ''
: `\nReceived value: ${printReceived(receivedValue)}`
}`
: `Expected path: not ${printExpected(expectedPath)}\n\n` +
`Received value: ${printReceived(receivedValue)}`)
Expand Down Expand Up @@ -926,9 +926,9 @@ const matchers: MatchersObject = {
matcherHint(matcherName, undefined, undefined, options) +
'\n\n' +
`Expected: not ${printExpected(expected)}` +
(stringify(expected) !== stringify(received)
? `\nReceived: ${printReceived(received)}`
: '')
(stringify(expected) === stringify(received)
? ''
: `\nReceived: ${printReceived(received)}`)
: () =>
// eslint-disable-next-line prefer-template
matcherHint(matcherName, undefined, undefined, options) +
Expand Down Expand Up @@ -965,9 +965,9 @@ const matchers: MatchersObject = {
matcherHint(matcherName, undefined, undefined, options) +
'\n\n' +
`Expected: not ${printExpected(expected)}\n` +
(stringify(expected) !== stringify(received)
? `Received: ${printReceived(received)}`
: '')
(stringify(expected) === stringify(received)
? ''
: `Received: ${printReceived(received)}`)
: () =>
// eslint-disable-next-line prefer-template
matcherHint(matcherName, undefined, undefined, options) +
Expand Down
18 changes: 9 additions & 9 deletions packages/expect/src/print.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,12 @@ const printConstructorName = (
isNot: boolean,
isExpected: boolean,
): string =>
typeof constructor.name !== 'string'
? `${label} name is not a string`
: constructor.name.length === 0
? `${label} name is an empty string`
: `${label}: ${!isNot ? '' : isExpected ? 'not ' : ' '}${
isExpected
? EXPECTED_COLOR(constructor.name)
: RECEIVED_COLOR(constructor.name)
}`;
typeof constructor.name === 'string'
? constructor.name.length === 0
? `${label} name is an empty string`
: `${label}: ${isNot ? (isExpected ? 'not ' : ' ') : ''}${
isExpected
? EXPECTED_COLOR(constructor.name)
: RECEIVED_COLOR(constructor.name)
}`
: `${label} name is not a string`;
38 changes: 19 additions & 19 deletions packages/expect/src/spyMatchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ const printNumberOfReturns = (
countCalls: number,
): string =>
`\nNumber of returns: ${printReceived(countReturns)}${
countCalls !== countReturns
? `\nNumber of calls: ${printReceived(countCalls)}`
: ''
countCalls === countReturns
? ''
: `\nNumber of calls: ${printReceived(countCalls)}`
}`;

type PrintLabel = (string: string, isExpectedCall: boolean) => string;
Expand Down Expand Up @@ -439,22 +439,22 @@ const createToReturnMatcher = (
return lines;
}, [])
.join('\n') +
(received.mock.calls.length !== count
? `\n\nReceived number of calls: ${printReceived(
(received.mock.calls.length === count
? ''
: `\n\nReceived number of calls: ${printReceived(
received.mock.calls.length,
)}`
: '')
)}`)
: () =>
// eslint-disable-next-line prefer-template
matcherHint(matcherName, receivedName, expectedArgument, options) +
'\n\n' +
`Expected number of returns: >= ${printExpected(1)}\n` +
`Received number of returns: ${printReceived(count)}` +
(received.mock.calls.length !== count
? `\nReceived number of calls: ${printReceived(
(received.mock.calls.length === count
? ''
: `\nReceived number of calls: ${printReceived(
received.mock.calls.length,
)}`
: '');
)}`);

return {message, pass};
};
Expand Down Expand Up @@ -523,22 +523,22 @@ const createToReturnTimesMatcher = (
matcherHint(matcherName, receivedName, expectedArgument, options) +
'\n\n' +
`Expected number of returns: not ${printExpected(expected)}` +
(received.mock.calls.length !== count
? `\n\nReceived number of calls: ${printReceived(
(received.mock.calls.length === count
? ''
: `\n\nReceived number of calls: ${printReceived(
received.mock.calls.length,
)}`
: '')
)}`)
: () =>
// eslint-disable-next-line prefer-template
matcherHint(matcherName, receivedName, expectedArgument, options) +
'\n\n' +
`Expected number of returns: ${printExpected(expected)}\n` +
`Received number of returns: ${printReceived(count)}` +
(received.mock.calls.length !== count
? `\nReceived number of calls: ${printReceived(
(received.mock.calls.length === count
? ''
: `\nReceived number of calls: ${printReceived(
received.mock.calls.length,
)}`
: '');
)}`);

return {message, pass};
};
Expand Down
16 changes: 8 additions & 8 deletions packages/expect/src/toThrowMatchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,13 @@ export const createMatcher = (
if (fromPromise && isError(received)) {
thrown = getThrown(received);
} else {
if (typeof received !== 'function') {
if (typeof received === 'function') {
try {
received();
} catch (e) {
thrown = getThrown(e);
}
} else {
if (!fromPromise) {
const placeholder = expected === undefined ? '' : 'expected';
throw new Error(
Expand All @@ -99,12 +105,6 @@ export const createMatcher = (
),
);
}
} else {
try {
received();
} catch (e) {
thrown = getThrown(e);
}
}
}

Expand Down Expand Up @@ -227,7 +227,7 @@ const toThrowExpectedObject = (
): SyncExpectationResult => {
const expectedMessageAndCause = createMessageAndCause(expected);
const thrownMessageAndCause =
thrown !== null ? createMessageAndCause(thrown.value) : null;
thrown === null ? null : createMessageAndCause(thrown.value);
const pass =
thrown !== null &&
thrown.message === expected.message &&
Expand Down
6 changes: 3 additions & 3 deletions packages/jest-circus/src/formatNodeAssertErrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ const formatNodeAssertErrors = (

if (originalError == null) {
error = asyncError;
} else if (!originalError.stack) {
} else if (originalError.stack) {
error = originalError;
} else {
error = asyncError;

error.message = originalError.message
? originalError.message
: `thrown: ${prettyFormat(originalError, {maxDepth: 3})}`;
} else {
error = originalError;
}
} else {
error = errors;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ const _addSnapshotData = (
results.snapshot.matched = snapshotState.matched;
results.snapshot.unmatched = snapshotState.unmatched;
results.snapshot.updated = snapshotState.updated;
results.snapshot.unchecked = !status.deleted ? uncheckedCount : 0;
results.snapshot.unchecked = status.deleted ? 0 : uncheckedCount;
// Copy the array to prevent memory leaks
results.snapshot.uncheckedKeys = Array.from(uncheckedKeys);
};
Expand Down
Loading