From 3bd19e9d5cb78d78dfaccc592b4dda941549ef5b Mon Sep 17 00:00:00 2001 From: Vladimir Sheremet Date: Thu, 3 Oct 2024 14:41:34 +0200 Subject: [PATCH] chore: remove vitest/runner patch --- package.json | 3 +- patches/@vitest__runner@2.0.5.patch | 34 ----------- pnpm-lock.yaml | 10 +--- samples/basic/test/bug.test.ts | 4 ++ src/api.ts | 1 + src/worker/collect.ts | 91 ++++++++++++++++++++++++++++- 6 files changed, 98 insertions(+), 45 deletions(-) delete mode 100644 patches/@vitest__runner@2.0.5.patch create mode 100644 samples/basic/test/bug.test.ts diff --git a/package.json b/package.json index 664d7eda..e51eb7eb 100644 --- a/package.json +++ b/package.json @@ -252,8 +252,7 @@ }, "pnpm": { "patchedDependencies": { - "birpc@0.2.17": "patches/birpc@0.2.17.patch", - "@vitest/runner@2.0.5": "patches/@vitest__runner@2.0.5.patch" + "birpc@0.2.17": "patches/birpc@0.2.17.patch" } }, "lint-staged": { diff --git a/patches/@vitest__runner@2.0.5.patch b/patches/@vitest__runner@2.0.5.patch deleted file mode 100644 index 2f601f90..00000000 --- a/patches/@vitest__runner@2.0.5.patch +++ /dev/null @@ -1,34 +0,0 @@ -diff --git a/dist/chunk-tasks.js b/dist/chunk-tasks.js -index 8d5d39d8ecfa64017593b50327215c95b600467a..8fb7f0add58b4aa6f173a8b1655498bf401ab0a8 100644 ---- a/dist/chunk-tasks.js -+++ b/dist/chunk-tasks.js -@@ -1,4 +1,3 @@ --import { processError } from '@vitest/utils/error'; - import { relative } from 'pathe'; - import { toArray } from '@vitest/utils'; - -@@ -103,14 +102,18 @@ function checkAllowOnly(task, allowOnly) { - if (allowOnly) { - return; - } -- const error = processError( -- new Error( -- "[Vitest] Unexpected .only modifier. Remove it or pass --allowOnly argument to bypass this error" -- ) -- ); -+ const error = new Error( -+ "[Vitest] Unexpected .only modifier. Remove it or pass --allowOnly argument to bypass this error" -+ ) - task.result = { - state: "fail", -- errors: [error] -+ errors: [ -+ { -+ name: error.name, -+ stack: error.stack, -+ message: error.message -+ } -+ ] - }; - } - function generateHash(str) { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 592e8697..e90d0cdd 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -5,9 +5,6 @@ settings: excludeLinksFromLockfile: false patchedDependencies: - '@vitest/runner@2.0.5': - hash: 3p7vko36hcqzkg3jwrwmmj7lvy - path: patches/@vitest__runner@2.0.5.patch birpc@0.2.17: hash: 76ascauag252sgpxpwh3k26eya path: patches/birpc@0.2.17.patch @@ -60,7 +57,7 @@ devDependencies: version: 8.5.10 '@vitest/runner': specifier: ^2.0.5 - version: 2.0.5(patch_hash=3p7vko36hcqzkg3jwrwmmj7lvy) + version: 2.0.5 '@vitest/utils': specifier: ^2.0.5 version: 2.0.5 @@ -1359,13 +1356,12 @@ packages: tinyrainbow: 1.2.0 dev: true - /@vitest/runner@2.0.5(patch_hash=3p7vko36hcqzkg3jwrwmmj7lvy): + /@vitest/runner@2.0.5: resolution: {integrity: sha512-TfRfZa6Bkk9ky4tW0z20WKXFEwwvWhRY+84CnSEtq4+3ZvDlJyY32oNTJtM7AW9ihW90tX/1Q78cb6FjoAs+ig==} dependencies: '@vitest/utils': 2.0.5 pathe: 1.1.2 dev: true - patched: true /@vitest/snapshot@2.0.5: resolution: {integrity: sha512-SgCPUeDFLaM0mIUHfaArq8fD2WbaXG/zVXjRupthYfYGzc8ztbFbu6dUNOblBG7XLMR1kEhS/DNnfCZ2IhdDew==} @@ -5691,7 +5687,7 @@ packages: '@types/node': 20.12.8 '@vitest/expect': 2.0.5 '@vitest/pretty-format': 2.0.5 - '@vitest/runner': 2.0.5(patch_hash=3p7vko36hcqzkg3jwrwmmj7lvy) + '@vitest/runner': 2.0.5 '@vitest/snapshot': 2.0.5 '@vitest/spy': 2.0.5 '@vitest/utils': 2.0.5 diff --git a/samples/basic/test/bug.test.ts b/samples/basic/test/bug.test.ts new file mode 100644 index 00000000..aba9a462 --- /dev/null +++ b/samples/basic/test/bug.test.ts @@ -0,0 +1,4 @@ +import { expect, test } from 'vitest'; +test('fail', () => { + expect(1).toEqual(2); +}) \ No newline at end of file diff --git a/src/api.ts b/src/api.ts index 1690641b..1f8196e9 100644 --- a/src/api.ts +++ b/src/api.ts @@ -168,6 +168,7 @@ export class VitestFolderAPI extends VitestReporter { async dispose() { this.handlers.clearListeners() delete require.cache[this.meta.pkg.vitestPackageJsonPath] + delete require.cache[this.meta.pkg.vitestNodePath] if (!this.meta.process.closed) { try { await this.meta.rpc.close() diff --git a/src/worker/collect.ts b/src/worker/collect.ts index 5110ac72..d1d312c4 100644 --- a/src/worker/collect.ts +++ b/src/worker/collect.ts @@ -8,10 +8,9 @@ import { ancestor as walkAst } from 'acorn-walk' import { calculateSuiteHash, generateHash, - interpretTaskModes, someTasksAreOnly, } from '@vitest/runner/utils' -import type { File, Suite, Test } from 'vitest' +import type { File, Suite, TaskBase, Test } from 'vitest' import type { WorkspaceProject } from 'vitest/node' interface ParsedFile extends File { @@ -359,3 +358,91 @@ function createIndexMap(source: string) { } return map } + +/** + * If any tasks been marked as `only`, mark all other tasks as `skip`. + */ +function interpretTaskModes( + suite: Suite, + namePattern?: string | RegExp, + onlyMode?: boolean, + parentIsOnly?: boolean, + allowOnly?: boolean, +): void { + const suiteIsOnly = parentIsOnly || suite.mode === 'only' + + suite.tasks.forEach((t) => { + // Check if either the parent suite or the task itself are marked as included + const includeTask = suiteIsOnly || t.mode === 'only' + if (onlyMode) { + if (t.type === 'suite' && (includeTask || someTasksAreOnly(t))) { + // Don't skip this suite + if (t.mode === 'only') { + checkAllowOnly(t, allowOnly) + t.mode = 'run' + } + } + else if (t.mode === 'run' && !includeTask) { + t.mode = 'skip' + } + else if (t.mode === 'only') { + checkAllowOnly(t, allowOnly) + t.mode = 'run' + } + } + if (t.type === 'test') { + if (namePattern && !getTaskFullName(t).match(namePattern)) { + t.mode = 'skip' + } + } + else if (t.type === 'suite') { + if (t.mode === 'skip') { + skipAllTasks(t) + } + else { + interpretTaskModes(t, namePattern, onlyMode, includeTask, allowOnly) + } + } + }) + + // if all subtasks are skipped, mark as skip + if (suite.mode === 'run') { + if (suite.tasks.length && suite.tasks.every(i => i.mode !== 'run')) { + suite.mode = 'skip' + } + } +} + +function checkAllowOnly(task: TaskBase, allowOnly?: boolean) { + if (allowOnly) { + return + } + const error = new Error( + '[Vitest] Unexpected .only modifier. Remove it or pass --allowOnly argument to bypass this error', + ) + task.result = { + state: 'fail', + errors: [ + { + name: error.name, + stack: error.stack, + message: error.message, + }, + ], + } +} + +function getTaskFullName(task: TaskBase): string { + return `${task.suite ? `${getTaskFullName(task.suite)} ` : ''}${task.name}` +} + +function skipAllTasks(suite: Suite) { + suite.tasks.forEach((t) => { + if (t.mode === 'run') { + t.mode = 'skip' + if (t.type === 'suite') { + skipAllTasks(t) + } + } + }) +}