-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(test-setup): add custom path matcher to vitest, add tests (#876)
- Loading branch information
Showing
12 changed files
with
301 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
102
testing/test-setup/src/lib/extend/path.matcher.unit.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/* eslint-disable @typescript-eslint/consistent-type-definitions */ | ||
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 */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,9 @@ | |
"references": [ | ||
{ | ||
"path": "./tsconfig.lib.json" | ||
}, | ||
{ | ||
"path": "./tsconfig.test.json" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} |
Oops, something went wrong.