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

test(test-setup): add custom path matcher to vitest, add tests #876

Merged
merged 23 commits into from
Jan 6, 2025
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
6 changes: 2 additions & 4 deletions e2e/cli-e2e/tests/collect.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe('CLI collect', () => {
});

it('should create report.md', async () => {
const { code, stderr } = await executeProcess({
const { code } = await executeProcess({
command: 'npx',
args: [
'@code-pushup/cli',
Expand All @@ -50,7 +50,6 @@ describe('CLI collect', () => {
});

expect(code).toBe(0);
expect(stderr).toBe('');

const md = await readTextFile(path.join(dummyOutputDir, 'report.md'));

Expand All @@ -60,14 +59,13 @@ describe('CLI collect', () => {
});

it('should print report summary to stdout', async () => {
const { code, stdout, stderr } = await executeProcess({
const { code, stdout } = await executeProcess({
command: 'npx',
args: ['@code-pushup/cli', '--no-progress', 'collect'],
cwd: dummyDir,
});

expect(code).toBe(0);
expect(stderr).toBe('');

expect(stdout).toContain('Code PushUp Report');
expect(stdout).not.toContain('Generated reports');
Expand Down
3 changes: 1 addition & 2 deletions e2e/cli-e2e/tests/help.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ describe('CLI help', () => {
const envRoot = path.join(E2E_ENVIRONMENTS_DIR, nxTargetProject());

it('should print help with help command', async () => {
const { code, stdout, stderr } = await executeProcess({
const { code, stdout } = await executeProcess({
command: 'npx',
args: ['@code-pushup/cli', 'help'],
cwd: envRoot,
});
expect(code).toBe(0);
expect(stderr).toBe('');
expect(removeColorCodes(stdout)).toMatchSnapshot();
});

Expand Down
3 changes: 2 additions & 1 deletion e2e/create-cli-e2e/tests/init.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import { executeProcess, readJsonFile, readTextFile } from '@code-pushup/utils';
const fakeCacheFolderName = () =>
`fake-cache-${new Date().toISOString().replace(/[:.]/g, '-')}`;

describe('create-cli-init', () => {
/* after a new release of the nx-verdaccio plugin we can enable the test again. For now, it is too flaky to be productive. (5.jan.2025) */
describe.todo('create-cli-init', () => {
const workspaceRoot = path.join(E2E_ENVIRONMENTS_DIR, nxTargetProject());
const testFileDir = path.join(workspaceRoot, TEST_OUTPUT_DIR, 'init');

Expand Down
6 changes: 2 additions & 4 deletions e2e/plugin-eslint-e2e/tests/collect.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,13 @@ describe('PLUGIN collect report with eslint-plugin NPM package', () => {
});

it('should run ESLint plugin for flat config and create report.json', async () => {
const { code, stderr } = await executeProcess({
const { code } = await executeProcess({
command: 'npx',
args: ['@code-pushup/cli', 'collect', '--no-progress'],
cwd: flatConfigDir,
});

expect(code).toBe(0);
expect(stderr).toBe('');

const report = await readJsonFile(
path.join(flatConfigOutputDir, 'report.json'),
Expand All @@ -65,15 +64,14 @@ describe('PLUGIN collect report with eslint-plugin NPM package', () => {
});

it('should run ESLint plugin for legacy config and create report.json', async () => {
const { code, stderr } = await executeProcess({
const { code } = await executeProcess({
command: 'npx',
args: ['@code-pushup/cli', 'collect', '--no-progress'],
cwd: legacyConfigDir,
env: { ...process.env, ESLINT_USE_FLAT_CONFIG: 'false' },
});

expect(code).toBe(0);
expect(stderr).toBe('');

const report = await readJsonFile(
path.join(legacyConfigOutputDir, 'report.json'),
Expand Down
6 changes: 6 additions & 0 deletions testing/test-setup/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
"assets": ["testing/test-setup/*.md"]
}
},
"unit-test": {
"executor": "@nx/vite:test",
"options": {
"configFile": "testing/test-setup/vite.config.unit.ts"
}
},
"lint": {
"executor": "@nx/linter:eslint",
"outputs": ["{options.outputFile}"],
Expand Down
122 changes: 122 additions & 0 deletions testing/test-setup/src/lib/extend/path.matcher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import type { SyncExpectationResult } from '@vitest/expect';
import { expect } from 'vitest';
import { osAgnosticPath } from '@code-pushup/test-utils';

export type CustomPathMatchers = {
toMatchPath: (path: string) => void;
toStartWithPath: (path: string) => void;
toContainPath: (path: string) => void;
toEndWithPath: (path: string) => void;
};

export type CustomAsymmetricPathMatchers = {
/* eslint-disable @typescript-eslint/no-explicit-any */
pathToMatch: (path: string) => any;
pathToStartWith: (path: string) => any;
pathToContain: (path: string) => any;
pathToEndWith: (path: string) => any;
/* eslint-enable @typescript-eslint/no-explicit-any */
};

expect.extend({
toMatchPath: assertPathMatch,
pathToMatch: assertPathMatch,
toStartWithPath: assertPathStartWith,
pathToStartWith: assertPathStartWith,
toContainPath: assertPathContain,
pathToContain: assertPathContain,
toEndWithPath: assertPathEndWith,
pathToEndWith: assertPathEndWith,
});

function assertPathMatch(
actual: string,
expected: string,
): SyncExpectationResult {
const normalizedReceived = osAgnosticPath(actual);
const normalizedExpected = osAgnosticPath(expected);

const pass = normalizedReceived === normalizedExpected;
return pass
? {
message: () => `expected ${actual} not to match path ${expected}`,
pass: true,
actual,
expected,
}
: {
message: () => `expected ${actual} to match path ${expected}`,
pass: false,
actual,
expected,
};
}

function assertPathStartWith(
actual: string,
expected: string,
): SyncExpectationResult {
const normalizedReceived = osAgnosticPath(actual);
const normalizedExpected = osAgnosticPath(expected);

const pass = normalizedReceived.startsWith(normalizedExpected);
return pass
? {
message: () => `expected ${actual} not to start with path ${expected}`,
pass: true,
actual,
expected,
}
: {
message: () => `expected ${actual} to start with path ${expected}`,
pass: false,
actual,
expected,
};
}

function assertPathContain(
actual: string,
expected: string,
): SyncExpectationResult {
const normalizedReceived = osAgnosticPath(actual);
const normalizedExpected = osAgnosticPath(expected);

const pass = normalizedReceived.includes(normalizedExpected);
return pass
? {
message: () => `expected ${actual} not to contain path ${expected}`,
pass: true,
actual,
expected,
}
: {
message: () => `expected ${actual} to contain path ${expected}`,
pass: false,
actual,
expected,
};
}

function assertPathEndWith(
actual: string,
expected: string,
): SyncExpectationResult {
const normalizedReceived = osAgnosticPath(actual);
const normalizedExpected = osAgnosticPath(expected);

const pass = normalizedReceived.endsWith(normalizedExpected);
return pass
? {
message: () => `expected ${actual} not to end with path ${expected}`,
pass: true,
actual,
expected,
}
: {
message: () => `expected ${actual} to end with path ${expected}`,
pass: false,
actual,
expected,
};
}
102 changes: 102 additions & 0 deletions testing/test-setup/src/lib/extend/path.matcher.unit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import { describe, expect, it, vi } from 'vitest';
import * as testUtils from '@code-pushup/test-utils';

describe('path-matcher', () => {
const osAgnosticPathSpy = vi.spyOn(testUtils, 'osAgnosticPath');

it('should provide "toMatchPath" as expect matcher', () => {
const actual = String.raw`tmp\path\to\file.txt`;
const expected = 'tmp/path/to/file.txt';

expect(actual).toMatchPath(expected);

expect(osAgnosticPathSpy).toHaveBeenCalledTimes(2);
expect(osAgnosticPathSpy).toHaveBeenCalledWith(actual);
expect(osAgnosticPathSpy).toHaveBeenCalledWith(expected);
});

it('should provide "pathToMatch" as expect matcher', () => {
const actual = String.raw`tmp\path\to\file.txt`;
const expected = 'tmp/path/to/file.txt';

expect({ path: actual }).toStrictEqual({
path: expect.pathToMatch(expected),
});

expect(osAgnosticPathSpy).toHaveBeenCalledTimes(2);
expect(osAgnosticPathSpy).toHaveBeenCalledWith(actual);
expect(osAgnosticPathSpy).toHaveBeenCalledWith(expected);
});

it('should provide "toStartWithPath" as expect matcher', () => {
const actual = String.raw`tmp\path\to\file.txt`;
const expected = 'tmp/path/to';

expect(actual).toStartWithPath(expected);

expect(osAgnosticPathSpy).toHaveBeenCalledTimes(2);
expect(osAgnosticPathSpy).toHaveBeenCalledWith(actual);
expect(osAgnosticPathSpy).toHaveBeenCalledWith(expected);
});

it('should provide "pathToStartWith" as expect matcher', () => {
const actual = String.raw`tmp\path\to\file.txt`;
const expected = 'tmp/path/to';

expect({ path: actual }).toStrictEqual({
path: expect.pathToStartWith(expected),
});

expect(osAgnosticPathSpy).toHaveBeenCalledTimes(2);
expect(osAgnosticPathSpy).toHaveBeenCalledWith(actual);
expect(osAgnosticPathSpy).toHaveBeenCalledWith(expected);
});

it('should provide "toContainPath" as expect matcher', () => {
const actual = String.raw`tmp\path\to\file.txt`;
const expected = 'path/to';

expect(actual).toContainPath(expected);

expect(osAgnosticPathSpy).toHaveBeenCalledTimes(2);
expect(osAgnosticPathSpy).toHaveBeenCalledWith(actual);
expect(osAgnosticPathSpy).toHaveBeenCalledWith(expected);
});

it('should provide "pathToContain" as expect matcher', () => {
const actual = String.raw`tmp\path\to\file.txt`;
const expected = 'path/to';

expect({ path: actual }).toStrictEqual({
path: expect.pathToContain(expected),
});

expect(osAgnosticPathSpy).toHaveBeenCalledTimes(2);
expect(osAgnosticPathSpy).toHaveBeenCalledWith(actual);
expect(osAgnosticPathSpy).toHaveBeenCalledWith(expected);
});

it('should provide "toEndWithPath" as expect matcher', () => {
const actual = String.raw`tmp\path\to\file.txt`;
const expected = 'path/to/file.txt';

expect(actual).toEndWithPath(expected);

expect(osAgnosticPathSpy).toHaveBeenCalledTimes(2);
expect(osAgnosticPathSpy).toHaveBeenCalledWith(actual);
expect(osAgnosticPathSpy).toHaveBeenCalledWith(expected);
});

it('should provide "pathToEndWith" as expect matcher', () => {
const actual = String.raw`tmp\path\to\file.txt`;
const expected = 'path/to/file.txt';

expect({ path: actual }).toStrictEqual({
path: expect.pathToEndWith(expected),
});

expect(osAgnosticPathSpy).toHaveBeenCalledTimes(2);
expect(osAgnosticPathSpy).toHaveBeenCalledWith(actual);
expect(osAgnosticPathSpy).toHaveBeenCalledWith(expected);
});
});
13 changes: 13 additions & 0 deletions testing/test-setup/src/vitest.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* eslint-disable @typescript-eslint/consistent-type-definitions */

Check failure on line 1 in testing/test-setup/src/vitest.d.ts

View workflow job for this annotation

GitHub Actions / Code PushUp

<✓> Code coverage | Branch coverage

1st branch is not taken in any test case.
import type {
CustomAsymmetricPathMatchers,
CustomPathMatchers,
} from './lib/extend/path.matcher.js';

declare module 'vitest' {
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
interface Assertion extends CustomPathMatchers {}
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
interface AsymmetricMatchersContaining extends CustomAsymmetricPathMatchers {}
}
/* eslint-enable @typescript-eslint/consistent-type-definitions */

Check warning on line 13 in testing/test-setup/src/vitest.d.ts

View workflow job for this annotation

GitHub Actions / Code PushUp

<✓> Code coverage | Line coverage

Lines 1-13 are not covered in any test case.
3 changes: 3 additions & 0 deletions testing/test-setup/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
"references": [
{
"path": "./tsconfig.lib.json"
},
{
"path": "./tsconfig.test.json"
}
]
}
7 changes: 6 additions & 1 deletion testing/test-setup/tsconfig.lib.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,10 @@
"types": ["node"]
},
"include": ["src/**/*.ts"],
"exclude": []
"exclude": [
"vite.config.unit.ts",
"src/vitest.d.ts",
"src/**/*.unit.test.ts",
"src/**/*.integration.test.ts"
]
}
14 changes: 14 additions & 0 deletions testing/test-setup/tsconfig.test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"types": ["vitest/globals", "vitest/importMeta", "vite/client", "node"]
},
"include": [
"vite.config.unit.ts",
"src/vitest.d.ts",
"src/**/*.unit.test.ts",
"src/**/*.d.ts",
"src/**/*.integration.test.ts"
]
}
Loading
Loading