Skip to content

Commit

Permalink
feat(jest-binary-data-matchers): remove unnecessary dependencies: jes…
Browse files Browse the repository at this point in the history
…t-matcher-utils (#425)

* feat(jest-binary-data-matchers): remove unnecessary dependencies from core code: jest-matcher-utils

    `this.utils` is injected into all the matchers. `this.utils` is almost equal to the exports from `jest-matcher-utils`.
    see https://jestjs.io/docs/expect#thisutils

    Therefore, no need to include `jest-matcher-utils` in the dependencies.

* chore(jest-binary-data-matchers): remove `jest-matcher-utils`

* test(jest-binary-data-matchers): force enable Jest highlighting output using `--color` option

    Tests that require color output fail. Probably due to Jest color output being disabled.

* test(jest-binary-data-matchers): force enable Jest highlighting output using `--color=true` option

    Tests that require color output fail. Probably due to Jest color output being disabled.

* test(jest-binary-data-matchers): force enable Jest highlighting output using `FORCE_COLOR` env

    Tests that require color output fail. Probably due to Jest color output being disabled.

* test(jest-binary-data-matchers): remove `jest.setup.cjs`

    The `jest.setup.cjs` file was used to set the `FORCE_COLOR` environment variable within Jest's test environment.
    However, this is no longer necessary since `cross-env` is now used instead.

* refactor(jest-binary-data-matchers): reduce lines in `createCompareByteSizeMatcher()` function

    Code Climate reported:
    + Function `createCompareByteSizeMatcher` has 27 lines of code (exceeds 25 allowed). Consider refactoring.

* refactor(jest-binary-data-matchers): reduce similar blocks

    Code Climate reported:
    + Similar blocks of code found in 2 locations. Consider refactoring.
  • Loading branch information
sounisi5011 authored May 27, 2022
1 parent 96b148f commit 7ea12c6
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 42 deletions.
3 changes: 0 additions & 3 deletions packages/jest-matchers/binary-data/jest.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ module.exports = {
tsconfig: '<rootDir>/tests/tsconfig.json',
},
},
setupFilesAfterEnv: [
'./jest.setup.cjs',
],
testEnvironment: 'node',
testMatch: ['<rootDir>/tests/**/*.ts'],
testPathIgnorePatterns: ['<rootDir>/tests/helpers/'],
Expand Down
2 changes: 0 additions & 2 deletions packages/jest-matchers/binary-data/jest.setup.cjs

This file was deleted.

6 changes: 3 additions & 3 deletions packages/jest-matchers/binary-data/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,17 @@
"lint:tsc:src": "tsc -p ./src/ --noEmit",
"lint:tsc:test": "tsc -p ./tests/ --noEmit",
"test": "run-if-supported --verbose run-p test:*",
"test:jest": "jest"
"test:jest": "cross-env FORCE_COLOR=1 jest"
},
"dependencies": {
"@sounisi5011/ts-type-util-is-readonly-array": "workspace:^1.0.0",
"jest-diff": "^28.0.0",
"jest-matcher-utils": "^27.3.1"
"jest-diff": "^28.0.0"
},
"devDependencies": {
"@sounisi5011/run-if-supported": "workspace:^1.0.0",
"@types/jest": "27.5.1",
"@types/node": "12.20.52",
"cross-env": "7.0.3",
"jest": "28.1.0",
"pretty-format": "28.1.0",
"ts-jest": "28.0.3",
Expand Down
31 changes: 14 additions & 17 deletions packages/jest-matchers/binary-data/src/matchers.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { EXPECTED_COLOR, matcherHint, RECEIVED_COLOR } from 'jest-matcher-utils';
import type { MatcherHintOptions } from 'jest-matcher-utils';

import { BytesData, bytesEqual, byteSize, isBytesData, padTextColumns, toMessageFn } from './utils';
import { ensureBytes, ensureByteSizeOrBytes, printBytesDiff } from './utils/jest';
import type { MatcherHintOptions } from './utils/jest';

function createCompareByteSizeMatcher(
opts: {
Expand Down Expand Up @@ -31,21 +29,20 @@ function createCompareByteSizeMatcher(
promise: this.promise,
};

ensureByteSizeOrBytes(received, expected, matcherName, options);
ensureByteSizeOrBytes(this.utils, received, expected, matcherName, options);
const expectedByteLength = isBytesData(expected) ? expected.byteLength : expected;
const receivedByteLength = isBytesData(received) ? received.byteLength : received;

return {
message: toMessageFn(() => [
matcherHint(matcherName, undefined, undefined, options),
``,
padTextColumns([
['Expected:', [isNot ? 'not' : '', operator], EXPECTED_COLOR(byteSize(expectedByteLength))],
['Received:', '', RECEIVED_COLOR(byteSize(receivedByteLength))],
]),
const message = toMessageFn(() => [
this.utils.matcherHint(matcherName, undefined, undefined, options),
``,
padTextColumns([
['Expected:', [isNot ? 'not' : '', operator], this.utils.EXPECTED_COLOR(byteSize(expectedByteLength))],
['Received:', '', this.utils.RECEIVED_COLOR(byteSize(receivedByteLength))],
]),
pass: passFn({ expected: expectedByteLength, received: receivedByteLength }),
};
]);
const pass = passFn({ expected: expectedByteLength, received: receivedByteLength });
return { message, pass };
};
}

Expand Down Expand Up @@ -106,12 +103,12 @@ export function toBytesEqual(
promise: this.promise,
};

ensureBytes(received, expected, matcherName, options);
ensureBytes(this.utils, received, expected, matcherName, options);
const pass = bytesEqual(expected, received);
const message = toMessageFn(() => [
matcherHint(matcherName, undefined, undefined, options),
this.utils.matcherHint(matcherName, undefined, undefined, options),
``,
printBytesDiff(expected, received, {
printBytesDiff(this.utils, expected, received, {
expectedLabel: 'Expected',
receivedLabel: 'Received',
expand: this.expand,
Expand Down
26 changes: 11 additions & 15 deletions packages/jest-matchers/binary-data/src/utils/jest.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
import { diffLinesUnified2 } from 'jest-diff';
import {
EXPECTED_COLOR,
matcherErrorMessage,
matcherHint,
printExpected,
printReceived,
printWithType,
RECEIVED_COLOR,
} from 'jest-matcher-utils';
import type { MatcherHintOptions } from 'jest-matcher-utils';

import { bytes2DataView, BytesData, inspectSingleline, isBytesData, isNonNegativeInteger, or, padTextColumns } from '.';

type MatcherUtils = jest.MatcherContext['utils'];
export type MatcherHintOptions = Exclude<Parameters<MatcherUtils['matcherHint']>[3], undefined>;

interface EnsureFuncPredicateOptions<T> {
predicate: (value: unknown) => value is T;
typeName: string;
}

type EnsureFunc<TActual> = (
utils: MatcherUtils,
actual: unknown,
expected: unknown,
matcherName: string,
Expand All @@ -37,13 +31,14 @@ function printValues(valueNameList: readonly string[]): string {
* @see https://github.com/facebook/jest/blob/v27.0.6/packages/jest-matcher-utils/src/index.ts#L218-L238
*/
function createEnsure<T>(opts: EnsureFuncPredicateOptions<T>): EnsureFunc<T> {
return (actual, expected, matcherName, options) => {
return (utils, actual, expected, matcherName, options) => {
const isValidActual = opts.predicate(actual);
const isValidExpected = opts.predicate(expected);
if (isValidActual && isValidExpected) return;

const labelList: string[] = [];
const specificList: string[] = [];
const { printWithType, printExpected, printReceived, EXPECTED_COLOR, RECEIVED_COLOR } = utils;
if (!isValidActual) {
labelList.push(RECEIVED_COLOR('received'));
specificList.push(printWithType('Received', actual, printReceived));
Expand All @@ -53,8 +48,8 @@ function createEnsure<T>(opts: EnsureFuncPredicateOptions<T>): EnsureFunc<T> {
specificList.push(printWithType('Expected', expected, printExpected));
}

throw new Error(matcherErrorMessage(
matcherHint(matcherName, undefined, undefined, options),
throw new Error(utils.matcherErrorMessage(
utils.matcherHint(matcherName, undefined, undefined, options),
`${printValues(labelList)} must be ${opts.typeName}`,
specificList.join('\n\n'),
));
Expand Down Expand Up @@ -100,6 +95,7 @@ function getByteDataLines(byteData: BytesData): {
* @see https://github.com/facebook/jest/blob/v27.0.6/packages/expect/src/matchers.ts#L955-L972
*/
export function printBytesDiff(
utils: MatcherUtils,
expected: BytesData,
received: BytesData,
options: {
Expand Down Expand Up @@ -127,9 +123,9 @@ export function printBytesDiff(
}

return padTextColumns([
['Expected:', 'not', EXPECTED_COLOR(inspectSingleline(expected))],
['Expected:', 'not', utils.EXPECTED_COLOR(inspectSingleline(expected))],
expected.constructor !== received.constructor
? ['Received:', '', RECEIVED_COLOR(inspectSingleline(received))]
? ['Received:', '', utils.RECEIVED_COLOR(inspectSingleline(received))]
: null,
]);
}
11 changes: 9 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7ea12c6

Please sign in to comment.