diff --git a/packages/nuxi/src/commands/module/add.ts b/packages/nuxi/src/commands/module/add.ts index 2086604df..16a52953c 100644 --- a/packages/nuxi/src/commands/module/add.ts +++ b/packages/nuxi/src/commands/module/add.ts @@ -289,7 +289,7 @@ async function resolveModule(moduleName: string, cwd: string): Promise satisfies(v, version)) || version + } - const pkg = pkgDetails.versions[pkgVersion] + const pkg = pkgDetails.versions[version!] const pkgDependencies = Object.assign( pkg.dependencies || {}, @@ -332,9 +335,9 @@ async function resolveModule(moduleName: string, cwd: string): Promise Promise.resolve()) const addDependency = vi.fn(() => Promise.resolve()) +let v3 = '3.0.0' interface CommandsType { subCommands: { // biome-ignore lint/correctness/noEmptyPattern: @@ -36,13 +37,46 @@ function applyMocks() { }, } }) + vi.mock('ofetch', async () => { + return { + $fetch: vi.fn(() => Promise.resolve({ + 'name': '@nuxt/content', + 'npm': '@nuxt/content', + 'devDependencies': { + nuxt: v3, + }, + 'dist-tags': { latest: v3 }, + 'versions': { + [v3]: { + devDependencies: { + nuxt: v3, + }, + }, + '3.1.1': { + devDependencies: { + nuxt: v3, + }, + }, + '2.9.0': { + devDependencies: { + nuxt: v3, + }, + }, + '2.13.1': { + devDependencies: { + nuxt: v3, + }, + }, + }, + })), + } + }) } describe('module add', () => { - let v3: string beforeAll(async () => { - v3 = await fetch('https://registry.npmjs.org/@nuxt/content') - .then(r => r.json()) - .then(r => r['dist-tags'].latest) + const response = await fetch('https://registry.npmjs.org/@nuxt/content') + const json = await response.json() + v3 = json['dist-tags'].latest }) applyMocks() vi.spyOn(runCommands, 'runCommand').mockImplementation(vi.fn()) @@ -105,4 +139,36 @@ describe('module add', () => { installPeerDependencies: true, }) }) + + it('should convert major only version to full semver', async () => { + const addCommand = await (commands as CommandsType).subCommands.add() + await addCommand.setup({ + args: { + cwd: '/fake-dir', + _: ['content@2'], + }, + }) + + expect(addDependency).toHaveBeenCalledWith(['@nuxt/content@2.13.1'], { + cwd: '/fake-dir', + dev: true, + installPeerDependencies: true, + }) + }) + + it('should convert not full version to full semver', async () => { + const addCommand = await (commands as CommandsType).subCommands.add() + await addCommand.setup({ + args: { + cwd: '/fake-dir', + _: ['content@3.1'], + }, + }) + + expect(addDependency).toHaveBeenCalledWith(['@nuxt/content@3.1.1'], { + cwd: '/fake-dir', + dev: true, + installPeerDependencies: true, + }) + }) })