-
Notifications
You must be signed in to change notification settings - Fork 393
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: remove remaining vestiges of jest types #4435
Changes from all commits
9c9884f
7424f6d
b96cf53
0cce6ee
6549691
f7c247b
0c69be3
95ae427
fe51b25
72905df
db5b2c1
b26cf61
e7b7649
e199902
b5d5178
d629f81
d286cb1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,19 +17,18 @@ const ERROR_CODE_RANGES = { | |
}, | ||
}; | ||
|
||
declare global { | ||
// eslint-disable-next-line @typescript-eslint/no-namespace | ||
namespace jest { | ||
interface Matchers<R> { | ||
__type: R; // unused, but makes TypeScript happy | ||
toBeUniqueCode: (key: string, seenErrorCodes: Set<number>) => object; | ||
toBeInRange: (min: number, max: number, key: string) => object; | ||
} | ||
} | ||
interface CustomMatchers<R = unknown> { | ||
toBeUniqueCode: (key: string, seenErrorCodes: Set<number>) => R; | ||
toBeInRange: (range: { min: number; max: number }, key: string) => R; | ||
} | ||
|
||
declare module 'vitest' { | ||
interface Assertion<T = any> extends CustomMatchers<T> {} | ||
interface AsymmetricMatchersContaining extends CustomMatchers {} | ||
} | ||
|
||
expect.extend({ | ||
toBeInRange(code, min, max, key) { | ||
toBeInRange(code, { min, max }, key) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typing will only work here if there's no more than 3 parameters, although tests would pass. VSCode shows errors otherwise. |
||
const pass = Number.isInteger(code) && code >= min && code <= max; | ||
const message = () => | ||
`expected ${key}'s error code '${code}'${ | ||
|
@@ -68,11 +67,7 @@ function traverseErrorInfo( | |
describe('error validation', () => { | ||
it('compiler error codes are in the correct range', () => { | ||
function validate(errorInfo: LWCErrorInfo, key: string) { | ||
expect(errorInfo.code).toBeInRange( | ||
ERROR_CODE_RANGES.compiler.min, | ||
ERROR_CODE_RANGES.compiler.max, | ||
key | ||
); | ||
expect(errorInfo.code).toBeInRange(ERROR_CODE_RANGES.compiler, key); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice |
||
} | ||
|
||
traverseErrorInfo(CompilerErrors, validate, 'compiler'); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,11 +5,11 @@ | |
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT | ||
*/ | ||
|
||
import diff from 'jest-diff'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was getting pulled in by |
||
import MatcherUtils = jest.MatcherUtils; | ||
import diff from '@vitest/utils/diff'; | ||
import type { MatcherState } from '@vitest/expect'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These packages both already come from |
||
|
||
export function toThrowErrorWithCode( | ||
this: MatcherUtils, | ||
this: MatcherState, | ||
received: any, | ||
code: string, | ||
message?: string | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ const expectExportDefaultFromPackageInFile = (pkgName: string, ext: string) => { | |
}; | ||
|
||
/* | ||
* This comment needs to be updated: | ||
* Jest uses CommonJS, which means that packages with no explicit export statements actually export | ||
* the default `module.exports` empty object. That export is an empty object with the prototype set | ||
* to an empty object with null prototype. | ||
cardoso marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
@@ -59,6 +60,7 @@ describe('default exports are not forgotten', () => { | |
'dist/index.js' | ||
); | ||
const realModule = await import(pathToEsmDistFile); | ||
// The commend below needs to be updated: | ||
// When jest properly supports ESM, this will be a lot simpler | ||
// const aliasedModule = await import(`lwc/${pkg}`); | ||
// expect(aliasedModule.default).toBe(realModule.default); | ||
Comment on lines
62
to
66
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This comment and related code should be updated |
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,21 +6,16 @@ | |
*/ | ||
|
||
import fs from 'fs'; | ||
import MatcherUtils = jest.MatcherUtils; | ||
import CustomMatcherResult = jest.CustomMatcherResult; | ||
import type { MatcherState } from '@vitest/expect'; | ||
|
||
/** | ||
* Jest matcher to assert that the content received matches the content in fixture file. | ||
* Vitest matcher to assert that the content received matches the content in fixture file. | ||
* @param receivedContent the fixture content | ||
* @param filename the fixture absolute path | ||
* @returns matcher result | ||
* @example expect(content).toMatchFile(outputPath) | ||
*/ | ||
function toMatchFile( | ||
this: MatcherUtils, | ||
receivedContent: string, | ||
filename: string | ||
): CustomMatcherResult { | ||
function toMatchFile(this: MatcherState, receivedContent: string, filename: string) { | ||
const { snapshotState, expand, utils } = this; | ||
|
||
const fileExists = fs.existsSync(filename); | ||
|
@@ -29,10 +24,10 @@ function toMatchFile( | |
const expectedContent = fs.readFileSync(filename, 'utf-8'); | ||
|
||
if (receivedContent === null || receivedContent === undefined) { | ||
// If the file exists but the expected content is undefined or null. If the Jest is | ||
// If the file exists but the expected content is undefined or null. If Vitest is | ||
// running with the update snapshot flag the file should be deleted. Otherwise fails | ||
// the assertion stating that the file is not expected to be there. | ||
if (snapshotState._updateSnapshot === 'all') { | ||
if (snapshotState['_updateSnapshot'] === 'all') { | ||
fs.unlinkSync(filename); | ||
|
||
snapshotState.updated++; | ||
|
@@ -57,10 +52,10 @@ function toMatchFile( | |
// content everything is fine. | ||
return { pass: true, message: () => '' }; | ||
} else { | ||
// If the expected file is present but the content is not matching. if Jest is running | ||
// If the expected file is present but the content is not matching. if Vitest is running | ||
// with the update snapshot flag override the expected content. Otherwise fails the | ||
// assertion with a diff. | ||
if (snapshotState._updateSnapshot === 'all') { | ||
if (snapshotState['_updateSnapshot'] === 'all') { | ||
fs.writeFileSync(filename, receivedContent); | ||
|
||
snapshotState.updated++; | ||
|
@@ -93,7 +88,10 @@ function toMatchFile( | |
|
||
// If expected file doesn't exists but got a received content and if the snapshots | ||
// should be updated, create the new snapshot. Otherwise fails the assertion. | ||
if (snapshotState._updateSnapshot === 'new' || snapshotState._updateSnapshot === 'all') { | ||
if ( | ||
snapshotState['_updateSnapshot'] === 'new' || | ||
snapshotState['_updateSnapshot'] === 'all' | ||
) { | ||
Comment on lines
+91
to
+94
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. _updateSnapshot is private in vitest which means there's probably a better way to do this, but for now this is needed to avoid annoying errors in vscode. |
||
fs.writeFileSync(filename, receivedContent); | ||
|
||
snapshotState.added++; | ||
|
@@ -113,15 +111,16 @@ function toMatchFile( | |
} | ||
} | ||
|
||
declare global { | ||
// eslint-disable-next-line @typescript-eslint/no-namespace | ||
namespace jest { | ||
interface Matchers<R> { | ||
__type: R; // unused, but makes TypeScript happy | ||
toMatchFile(receivedContent: string, filename?: string): CustomMatcherResult; | ||
} | ||
} | ||
import 'vitest'; | ||
|
||
interface CustomMatchers<R = unknown> { | ||
toMatchFile: (receivedContent: string, filename?: string) => R; | ||
} | ||
|
||
declare module 'vitest' { | ||
interface Assertion<T = any> extends CustomMatchers<T> {} | ||
interface AsymmetricMatchersContaining extends CustomMatchers {} | ||
Comment on lines
+114
to
+122
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This might fit better in a .d.ts file |
||
} | ||
|
||
// Register jest matcher. | ||
// Register vitest matcher. | ||
expect.extend({ toMatchFile }); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1015,9 +1015,11 @@ | |
|
||
"@lwc/eslint-plugin-lwc-internal@link:./scripts/eslint-plugin": | ||
version "0.0.0" | ||
uid "" | ||
|
||
"@lwc/test-utils-lwc-internals@link:./scripts/test-utils": | ||
version "0.0.0" | ||
uid "" | ||
|
||
"@napi-rs/[email protected]": | ||
version "0.2.4" | ||
|
@@ -1961,7 +1963,7 @@ | |
test-exclude "^7.0.1" | ||
tinyrainbow "^1.2.0" | ||
|
||
"@vitest/[email protected]": | ||
"@vitest/[email protected]", "@vitest/expect@^2.0.5": | ||
version "2.0.5" | ||
resolved "https://registry.yarnpkg.com/@vitest/expect/-/expect-2.0.5.tgz#f3745a6a2c18acbea4d39f5935e913f40d26fa86" | ||
integrity sha512-yHZtwuP7JZivj65Gxoi8upUN2OzHTi3zVfjwdpu2WrvCZPLwsJ2Ey5ILIPccoW23dd/zQBlJ4/dhi7DWNyXCpA== | ||
|
@@ -2024,7 +2026,7 @@ | |
sirv "^2.0.4" | ||
tinyrainbow "^1.2.0" | ||
|
||
"@vitest/[email protected]": | ||
"@vitest/[email protected]", "@vitest/utils@^2.0.5": | ||
version "2.0.5" | ||
resolved "https://registry.yarnpkg.com/@vitest/utils/-/utils-2.0.5.tgz#6f8307a4b6bc6ceb9270007f73c67c915944e926" | ||
integrity sha512-d8HKbqIcya+GR67mkZbrzhS5kKhtp8dQLcmRZLGTscGVg7yImT82cIrhtn2L8+VujWcy6KZweApgNmPsTAO/UQ== | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a reminder here, maybe for another PR? I reckon this is related to #4386