diff --git a/packages/nuxi/src/commands/module/add.ts b/packages/nuxi/src/commands/module/add.ts index 7766790c8..2c9b03d5c 100644 --- a/packages/nuxi/src/commands/module/add.ts +++ b/packages/nuxi/src/commands/module/add.ts @@ -223,6 +223,7 @@ async function resolveModule(moduleName: string, cwd: string): Promise module.name === moduleName + || (pkgVersion && module.name === pkgName) || module.npm === pkgName || module.aliases?.includes(pkgName), ) diff --git a/packages/nuxi/test/unit/commands/module/add.spec.ts b/packages/nuxi/test/unit/commands/module/add.spec.ts new file mode 100644 index 000000000..ccc4d9189 --- /dev/null +++ b/packages/nuxi/test/unit/commands/module/add.spec.ts @@ -0,0 +1,101 @@ +import { describe, expect, it, vi } from 'vitest' +import commands from '../../../../src/commands/module' +import * as utils from '../../../../src/commands/module/_utils' +import * as runCommands from '../../../../src/run' + +const updateConfig = vi.fn(() => Promise.resolve()) +const addDependency = vi.fn(() => Promise.resolve()) +interface CommandsType { + subCommands: { + // biome-ignore lint/correctness/noEmptyPattern: + add: () => Promise<{ setup: (args: any) => void }> + } +} +function applyMocks() { + vi.mock('c12/update', async () => { + return { + updateConfig, + } + }) + vi.mock('nypm', async () => { + return { + addDependency, + } + }) + vi.mock('pkg-types', async () => { + return { + readPackageJSON: () => { + return new Promise((resolve) => { + resolve({ + devDependencies: { + nuxt: '3.0.0', + }, + }) + }) + }, + } + }) +} +describe('module add', () => { + applyMocks() + vi.spyOn(runCommands, 'runCommand').mockImplementation(vi.fn()) + vi.spyOn(utils, 'getNuxtVersion').mockResolvedValue('3.0.0') + vi.spyOn(utils, 'fetchModules').mockResolvedValue([ + { + name: 'content', + npm: '@nuxt/content', + compatibility: { + nuxt: '3.0.0', + requires: {}, + versionMap: {}, + }, + description: '', + repo: '', + github: '', + website: '', + learn_more: '', + category: '', + type: 'community', + maintainers: [], + stats: { + downloads: 0, + stars: 0, + maintainers: 0, + contributors: 0, + modules: 0, + }, + }, + ]) + + it('should install Nuxt module', async () => { + const addCommand = await (commands as CommandsType).subCommands.add() + await addCommand.setup({ + args: { + cwd: '/fake-dir', + _: ['content'], + }, + }) + + expect(addDependency).toHaveBeenCalledWith(['@nuxt/content@3.0.0'], { + cwd: '/fake-dir', + dev: true, + installPeerDependencies: true, + }) + }) + + it('should convert versioned module to Nuxt module', async () => { + const addCommand = await (commands as CommandsType).subCommands.add() + await addCommand.setup({ + args: { + cwd: '/fake-dir', + _: ['content@2.9.0'], + }, + }) + + expect(addDependency).toHaveBeenCalledWith(['@nuxt/content@2.9.0'], { + cwd: '/fake-dir', + dev: true, + installPeerDependencies: true, + }) + }) +})