Skip to content

Commit

Permalink
refactor(tests): update snapshots and clean indicators for consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
Shuunen committed Dec 7, 2024
1 parent 7ba79b2 commit 00229c5
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/__snapshots__/check.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ exports[`check C ts-project folder 1`] = `
"package-json-could-have-a-check-script-like-check-pnpm-build-pnpm-lint",
"package-json-could-have-a-final-echo-for-task-build-build-echo-wsbuild-w",
"package-json-one-coverage-dependency-from-nyc-c8-v8",
"package-json-could-have-latest-version-of-repo-checker-like-repo-check-1-39-43m30m-fixable-39m49m",
"package-json-could-have-latest-version-of-repo-checker-like-repo-check-x-yy-43m30m-fixable-39m49m",
"package-json-ts-node-without-transpile-only-detected-in-files-package-json",
"package-json-could-have-not-fat-ts-runner-use-typescript-run-like-dev-ts-run-src-watch-or-ts-run-src-w-src-another-folder-not-fat-ts-runner-use-typescript-run-like-dev-ts-run-src-watch-or-ts-run-src-w-src-another-folder",
"readme-md-could-have-a-logo-image-logofolder-any-file-ext",
Expand Down Expand Up @@ -345,7 +345,7 @@ exports[`check E mocks-projects folders and not throw 1`] = `
"package-json-could-have-a-check-script-like-check-pnpm-build-pnpm-lint",
"package-json-could-have-a-final-echo-for-task-build-build-echo-wsbuild-w",
"package-json-one-coverage-dependency-from-nyc-c8-v8",
"package-json-could-have-latest-version-of-repo-checker-like-repo-check-1-39-43m30m-fixable-39m49m",
"package-json-could-have-latest-version-of-repo-checker-like-repo-check-x-yy-43m30m-fixable-39m49m",
"package-json-ts-node-without-transpile-only-detected-in-files-package-json",
"package-json-could-have-not-fat-ts-runner-use-typescript-run-like-dev-ts-run-src-watch-or-ts-run-src-w-src-another-folder-not-fat-ts-runner-use-typescript-run-like-dev-ts-run-src-watch-or-ts-run-src-w-src-another-folder",
"readme-md-could-have-a-logo-image-logofolder-any-file-ext",
Expand Down
8 changes: 4 additions & 4 deletions src/check.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { expect, it } from 'vitest'
import { check } from './check'
import { ProjectData, repoCheckerPath } from './constants'
import { mocksProjectsFolder, tsProjectFolder } from './mock'
import { cleanIndicatorsForSnap, mocksProjectsFolder, tsProjectFolder } from './mock'

it('check A repo-checker folder fails with low max size', async () => {
const data = new ProjectData({ isPublishedPackage: true, isQuiet: true, maxSizeKo: 2 })
Expand All @@ -13,13 +13,13 @@ it('check A repo-checker folder fails with low max size', async () => {
it('check B repo-checker folder succeed', async () => {
const data = new ProjectData({ isModule: true, isPublishedPackage: true, isQuiet: true, maxSizeKo: 120 })
const indicators = await check({ data, folderPath: repoCheckerPath })
expect(indicators).toMatchSnapshot()
expect(cleanIndicatorsForSnap(indicators)).toMatchSnapshot()
})

it('check C ts-project folder', async () => {
const data = new ProjectData({ isQuiet: true })
const indicators = await check({ canThrow: false, data, folderPath: tsProjectFolder })
expect(indicators).toMatchSnapshot()
expect(cleanIndicatorsForSnap(indicators)).toMatchSnapshot()
})

it('check D mocks-projects folders and throw', async () => {
Expand All @@ -32,5 +32,5 @@ it('check D mocks-projects folders and throw', async () => {
it('check E mocks-projects folders and not throw', async () => {
const data = new ProjectData({ isQuiet: true })
const indicators = await check({ canFailStop: true, canThrow: false, data, folderPath: mocksProjectsFolder })
expect(indicators).toMatchSnapshot()
expect(cleanIndicatorsForSnap(indicators)).toMatchSnapshot()
})
8 changes: 4 additions & 4 deletions src/files/__snapshots__/package.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ exports[`package A on repo checker 1`] = `
],
"warnings": [
"package-json-main-file-maximum-size-is-specified-in-data-file-ex-max-size-ko-100",
"package-json-could-have-latest-version-of-repo-checker-like-repo-check-1-39-43m30m-fixable-39m49m",
"package-json-could-have-latest-version-of-repo-checker-like-repo-check-x-yy-43m30m-fixable-39m49m",
],
}
`;
Expand Down Expand Up @@ -196,7 +196,7 @@ exports[`package B on ts project 1`] = `
"package-json-could-have-a-check-script-like-check-pnpm-build-pnpm-lint",
"package-json-could-have-a-final-echo-for-task-build-build-echo-wsbuild-w",
"package-json-one-coverage-dependency-from-nyc-c8-v8",
"package-json-could-have-latest-version-of-repo-checker-like-repo-check-1-39-43m30m-fixable-39m49m",
"package-json-could-have-latest-version-of-repo-checker-like-repo-check-x-yy-43m30m-fixable-39m49m",
"package-json-ts-node-without-transpile-only-detected-in-files-package-json",
"package-json-could-have-not-fat-ts-runner-use-typescript-run-like-dev-ts-run-src-watch-or-ts-run-src-w-src-another-folder-not-fat-ts-runner-use-typescript-run-like-dev-ts-run-src-watch-or-ts-run-src-w-src-another-folder",
],
Expand Down Expand Up @@ -327,7 +327,7 @@ exports[`package C ts project isUsing* 1`] = `
"package-json-could-have-a-check-script-like-check-pnpm-build-pnpm-lint",
"package-json-could-have-a-final-echo-for-task-build-build-echo-wsbuild-w",
"package-json-one-coverage-dependency-from-nyc-c8-v8",
"package-json-could-have-latest-version-of-repo-checker-like-repo-check-1-39-43m30m-fixable-39m49m",
"package-json-could-have-latest-version-of-repo-checker-like-repo-check-x-yy-43m30m-fixable-39m49m",
"package-json-ts-node-without-transpile-only-detected-in-files-package-json",
"package-json-could-have-not-fat-ts-runner-use-typescript-run-like-dev-ts-run-src-watch-or-ts-run-src-w-src-another-folder-not-fat-ts-runner-use-typescript-run-like-dev-ts-run-src-watch-or-ts-run-src-w-src-another-folder",
"package-json-could-have-a-module-field-pointing-to-an-esm-mjs-like-module-dist-index-mjs",
Expand Down Expand Up @@ -474,7 +474,7 @@ exports[`package E vue project 1`] = `
"package-json-could-have-a-check-script-like-check-pnpm-build-pnpm-lint",
"package-json-one-unit-testing-dependency-from-vitest-mocha-uvu",
"package-json-one-coverage-dependency-from-nyc-c8-v8",
"package-json-could-have-latest-version-of-repo-checker-like-repo-check-1-39-43m30m-fixable-39m49m",
"package-json-could-have-latest-version-of-repo-checker-like-repo-check-x-yy-43m30m-fixable-39m49m",
],
}
`;
Expand Down
29 changes: 23 additions & 6 deletions src/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,21 @@ import path from 'node:path'
import { clone, sleep } from 'shuutils'
import type { FileBase } from './file'
import { join } from './utils'
import type { check } from './check'

const baseKeys: (keyof FileBase)[] = ['fileExists', 'checkFileExists', 'folderPath', 'inspectFile', 'updateFile', 'initFile']
const fileBaseKeysToDelete: (keyof FileBase)[] = ['fileExists', 'checkFileExists', 'folderPath', 'inspectFile', 'updateFile', 'initFile']
const indicatorsKeys: ReadonlyArray<keyof Awaited<ReturnType<typeof check>>> = ['failed', 'passed', 'warnings'] as const

function cleanStringForSnap(input: string) {
return input.replace(/repo-check-\d-\d+/gu, 'repo-check-x-yy') // replace repo-check-1-40 by repo-check-x-yy
}

function cleanUnknownValueForSnap<Type>(input: Type): Type {

Check failure on line 16 in src/mock.ts

View workflow job for this annotation

GitHub Actions / ci

Don't specify return type on function
if (typeof input === 'string') return cleanStringForSnap(input) as Type

Check failure on line 17 in src/mock.ts

View workflow job for this annotation

GitHub Actions / ci

Do not use any type assertions
if (Array.isArray(input)) return input.map(cleanUnknownValueForSnap) as Type

Check failure on line 18 in src/mock.ts

View workflow job for this annotation

GitHub Actions / ci

Do not use any type assertions

Check failure on line 18 in src/mock.ts

View workflow job for this annotation

GitHub Actions / ci

Do not pass function `cleanUnknownValueForSnap` directly to `.map(…)`
if (typeof input === 'object') return JSON.parse(cleanStringForSnap(JSON.stringify(input))) as Type

Check failure on line 19 in src/mock.ts

View workflow job for this annotation

GitHub Actions / ci

Do not use any type assertions
throw new Error(`cleanInstanceValueForSnap: unknown type ${typeof input}`)
}

export async function promiseValue<Type>(value: Type) {
await sleep(1)
Expand Down Expand Up @@ -37,10 +50,14 @@ export const mocksProjectsFolder = join(sourceFolder, 'mocks')

// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
export function cleanInstanceForSnap(instance: Readonly<FileBase>, ...bonusKeys: readonly string[]) {
const clean = clone<Partial<FileBase>>(instance)
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions, @typescript-eslint/no-dynamic-delete
for (const key of baseKeys) delete (clean as Record<string, unknown>)[key]
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete, @typescript-eslint/consistent-type-assertions
for (const key of bonusKeys) if (key in clean) delete (clean as Record<string, unknown>)[key]
const clean = clone<Partial<FileBase>>(instance) as Record<string, unknown>

Check failure on line 53 in src/mock.ts

View workflow job for this annotation

GitHub Actions / ci

Do not use any type assertions
const keys = [...fileBaseKeysToDelete, ...bonusKeys] // eslint-disable-next-line @typescript-eslint/no-dynamic-delete, @typescript-eslint/consistent-type-assertions
for (const key of keys) if (key in clean) delete (clean as Record<string, unknown>)[key]

Check failure on line 55 in src/mock.ts

View workflow job for this annotation

GitHub Actions / ci

This assertion is unnecessary since it does not change the type of the expression
return cleanIndicatorsForSnap(clean)

Check failure on line 56 in src/mock.ts

View workflow job for this annotation

GitHub Actions / ci

'cleanIndicatorsForSnap' was used before it was defined
}

export function cleanIndicatorsForSnap(record: Record<string, unknown>) {

Check failure on line 59 in src/mock.ts

View workflow job for this annotation

GitHub Actions / ci

Parameter should be a read only type
const clean = clone(record)
for (const key of indicatorsKeys) if (key in clean) clean[key] = cleanUnknownValueForSnap(clean[key])
return clean
}

0 comments on commit 00229c5

Please sign in to comment.