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 9 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: 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
183 changes: 183 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,183 @@
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(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,
};
},

pathToMatch(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,
};
},
BioPhoton marked this conversation as resolved.
Show resolved Hide resolved

toStartWithPath(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 starts with path ${expected}`,
pass: true,
actual,
expected,
}
: {
message: () => `expected ${actual} to starts with path ${expected}`,
BioPhoton marked this conversation as resolved.
Show resolved Hide resolved
pass: false,
actual,
expected,
};
},

pathToStartWith(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 starts with path ${expected}`,
pass: true,
actual,
expected,
}
: {
message: () => `expected ${actual} to starts with path ${expected}`,
BioPhoton marked this conversation as resolved.
Show resolved Hide resolved
pass: false,
actual,
expected,
};
},

toContainPath(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,
};
},

pathToContain(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,
};
},

toEndWithPath(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 ends with path ${expected}`,
pass: true,
actual,
expected,
}
: {
message: () => `expected ${actual} to ends with path ${expected}`,
BioPhoton marked this conversation as resolved.
Show resolved Hide resolved
pass: false,
actual,
expected,
};
},

pathToEndWith(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 ends with path ${expected}`,
pass: true,
actual,
expected,
}
: {
message: () => `expected ${actual} to ends with path ${expected}`,
BioPhoton marked this conversation as resolved.
Show resolved Hide resolved
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 = '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 = '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 = '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 = '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 = '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 = '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 = '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 = '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);
});
});
10 changes: 10 additions & 0 deletions testing/test-setup/src/vitest.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* eslint-disable @typescript-eslint/consistent-type-definitions,@typescript-eslint/no-empty-interface */

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.
matejchalk marked this conversation as resolved.
Show resolved Hide resolved
import type {
CustomAsymmetricPathMatchers,
CustomPathMatchers,
} from './lib/extend/path.matcher.js';

declare module 'vitest' {
interface Assertion extends CustomPathMatchers {}
interface AsymmetricMatchersContaining extends CustomAsymmetricPathMatchers {}
}

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

View workflow job for this annotation

GitHub Actions / Code PushUp

<✓> Code coverage | Line coverage

Lines 1-10 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"
]
}
28 changes: 28 additions & 0 deletions testing/test-setup/vite.config.unit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/// <reference types="vitest" />
import { defineConfig } from 'vite';
import { tsconfigPathAliases } from '../../tools/vitest-tsconfig-path-aliases';
BioPhoton marked this conversation as resolved.
Show resolved Hide resolved

export default defineConfig({
cacheDir: '../node_modules/.vite/test-setup',
test: {
reporters: ['basic'],
globals: true,
cache: {
dir: '../node_modules/.vitest',
},
alias: tsconfigPathAliases(),
pool: 'threads',
poolOptions: { threads: { singleThread: true } },
coverage: {
reporter: ['text', 'lcov'],
reportsDirectory: '../../coverage/test-setup/unit-tests',
exclude: ['**/*.mock.{mjs,ts}', '**/*.config.{js,mjs,ts}'],
},
environment: 'node',
include: ['src/**/*.unit.test.ts'],
setupFiles: [
'../test-setup/src/lib/reset.mocks.ts',
'../test-setup/src/lib/extend/path.matcher.ts',
],
},
});
Loading