Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/nuxi/src/commands/module/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ async function resolveModule(moduleName: string, cwd: string): Promise<ModuleRes
const matchedModule = modulesDB.find(
module =>
module.name === moduleName
|| (pkgVersion && module.name === pkgName)
|| module.npm === pkgName
|| module.aliases?.includes(pkgName),
)
Expand Down
101 changes: 101 additions & 0 deletions packages/nuxi/test/unit/commands/module/add.spec.ts
Original file line number Diff line number Diff line change
@@ -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: <explanation>
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,
})
})
})
Loading