Repository navigation
fix(testtools): declare vitest as a peer dependency #2909
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
Open
thelastbackspace
wants to merge
1
commit into
zenstackhq:dev
Choose a base branch
from
thelastbackspace:fix/2659-testtools-vitest-peer
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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,33 @@ | ||
| import fs from 'node:fs'; | ||
| import { builtinModules } from 'node:module'; | ||
| import path from 'node:path'; | ||
| import { describe, expect, it } from 'vitest'; | ||
|
|
||
| // https://github.com/zenstackhq/zenstack/issues/2659 | ||
| describe('Regression for issue 2659', () => { | ||
| it('testtools declares every package it imports at runtime', () => { | ||
| const pkgDir = path.resolve(__dirname, '../../../packages/testtools'); | ||
| const pkg = JSON.parse(fs.readFileSync(path.join(pkgDir, 'package.json'), 'utf-8')); | ||
| const declared = new Set([...Object.keys(pkg.dependencies ?? {}), ...Object.keys(pkg.peerDependencies ?? {})]); | ||
|
|
||
| const srcDir = path.join(pkgDir, 'src'); | ||
| const imported = new Set<string>(); | ||
| for (const file of fs.readdirSync(srcDir)) { | ||
| if (!file.endsWith('.ts') || file.endsWith('.d.ts')) { | ||
| continue; | ||
| } | ||
| const content = fs.readFileSync(path.join(srcDir, file), 'utf-8'); | ||
| for (const m of content.matchAll(/^import\s+(?!type\s)[^'"]*?from\s+['"]([^'"]+)['"]/gm)) { | ||
| const spec = m[1]!; | ||
| if (spec.startsWith('.') || spec.startsWith('node:') || builtinModules.includes(spec)) { | ||
| continue; | ||
| } | ||
| const parts = spec.split('/'); | ||
| imported.add(spec.startsWith('@') ? parts.slice(0, 2).join('/') : parts[0]!); | ||
| } | ||
| } | ||
|
|
||
| const missing = [...imported].filter((name) => !declared.has(name)); | ||
| expect(missing).toEqual([]); | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
Script executed:
Repository: zenstackhq/zenstack
Length of output: 1931
Declare
vitestindependenciesso consumers do not need to install it.The root entry re-exports
schemaandvitest-ext. Both modules importexpectfromvitestat top level. A consumer whose package manager does not auto-install peers receives novitest. ImportingloadSchemafrom the package root then fails withCannot find module 'vitest'. Issue#2659asks that consumers not need to addvitestthemselves, and this declaration does not provide that for these consumers.Move
vitestfrompeerDependenciestodependencies, then updatepnpm-lock.yamlsopnpm install --frozen-lockfilepasses. The regression test still passes because it checksdependenciesas well. ThedevDependenciesentry can be removed becausedependenciesnow covers it.Moving the Vitest-dependent exports to a separate entry point is the other option. It changes the public import surface, so it is a larger change.
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.
I went with a peer on purpose.
vitest-extcallsexpect.extend, which only works if testtools and the consumer's tests share the same vitest instance. As a regular dependency, a project on a different vitest version would get a second copy, and the matchers would register on the wrongexpect. pnpm and npm 7+ install peers automatically, and yarn warns. That said, happy to switch it todependenciesif the maintainers prefer.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.
Analysis chain
Script executed:
Length of output: 10138
web_search executed:
For Vitest 4, what are the documented requirements for importing expect from vitest and registering custom matchers with expect.extend? Can vitest be imported outside the Vitest runner?Length of output: 15257
@thelastbackspaceThanks for explaining the peer dependency. I withdraw my recommendation to move Vitest todependencies: a separate copy could cause matcher registration to affect a differentexpectinstance.The original
#2659concern remains: the root entry imports Vitest, so consumers whose package manager does not install peers can still encounter a missing-module error. The regression test checks declarations, not package loading.To verify both requirements, I recommend testing the packed package in Jest without Vitest and testing matcher registration in a Vitest consumer. I inspected the source but did not run those tests.
You are interacting with an AI system.