Files
pnpm/exec/plugin-commands-script-runners/test/create.ts
Zoltan Kochan 1c8c4e49f5 style: add eslint-plugin-simple-import-sort (#10947)
Add eslint-plugin-simple-import-sort to enforce consistent import ordering:
- Node.js builtins first
- External packages second
- Relative imports last
- Named imports sorted alphabetically within each statement
2026-03-13 02:02:38 +01:00

143 lines
4.2 KiB
TypeScript

import { jest } from '@jest/globals'
import { PnpmError } from '@pnpm/error'
import { DLX_DEFAULT_OPTS as DEFAULT_OPTS } from './utils/index.js'
jest.unstable_mockModule('../src/dlx.js', () => ({ handler: jest.fn() }))
const { create, dlx } = await import('../src/index.js')
beforeEach(() => jest.mocked(dlx.handler).mockClear())
it('throws an error if called without arguments', async () => {
await expect(create.handler({
...DEFAULT_OPTS,
dir: process.cwd(),
}, [])).rejects.toThrow(PnpmError)
expect(dlx.handler).not.toHaveBeenCalled()
})
it(
'appends `create-` to an unscoped package that doesn\'t start with `create-`',
async () => {
await create.handler({
...DEFAULT_OPTS,
dir: process.cwd(),
}, ['some-app'])
expect(dlx.handler).toHaveBeenCalledWith(expect.anything(), ['create-some-app'])
await create.handler({
...DEFAULT_OPTS,
dir: process.cwd(),
}, ['create_no_dash'])
expect(dlx.handler).toHaveBeenCalledWith(expect.anything(), ['create-create_no_dash'])
}
)
it(
'does not append `create-` to an unscoped package that starts with `create-`',
async () => {
await create.handler({
...DEFAULT_OPTS,
dir: process.cwd(),
}, ['create-some-app'])
expect(dlx.handler).toHaveBeenCalledWith(expect.anything(), ['create-some-app'])
await create.handler({
...DEFAULT_OPTS,
dir: process.cwd(),
}, ['create-'])
expect(dlx.handler).toHaveBeenCalledWith(expect.anything(), ['create-'])
}
)
it(
'appends `create-` to a scoped package that doesn\'t start with `create-`',
async () => {
await create.handler({
...DEFAULT_OPTS,
dir: process.cwd(),
}, ['@scope/some-app'])
expect(dlx.handler).toHaveBeenCalledWith(expect.anything(), ['@scope/create-some-app'])
await create.handler({
...DEFAULT_OPTS,
dir: process.cwd(),
}, ['@scope/create_no_dash'])
expect(dlx.handler).toHaveBeenCalledWith(expect.anything(), ['@scope/create-create_no_dash'])
}
)
it(
'does not append `create-` to a scoped package that starts with `create-`',
async () => {
await create.handler({
...DEFAULT_OPTS,
dir: process.cwd(),
}, ['@scope/create-some-app'])
expect(dlx.handler).toHaveBeenCalledWith(expect.anything(), ['@scope/create-some-app'])
await create.handler({
...DEFAULT_OPTS,
dir: process.cwd(),
}, ['@scope/create-'])
expect(dlx.handler).toHaveBeenCalledWith(expect.anything(), ['@scope/create-'])
}
)
it('infers a package name from a plain scope', async () => {
await create.handler({
...DEFAULT_OPTS,
dir: process.cwd(),
}, ['@scope'])
expect(dlx.handler).toHaveBeenCalledWith(expect.anything(), ['@scope/create'])
})
it('passes the remaining arguments to `dlx`', async () => {
await create.handler({
...DEFAULT_OPTS,
dir: process.cwd(),
}, ['some-app', 'directory/', '--silent'])
expect(dlx.handler).toHaveBeenCalledWith(expect.anything(), ['create-some-app', 'directory/', '--silent'])
})
it(
'appends `create` to package with preferred version`',
async () => {
await create.handler({
...DEFAULT_OPTS,
dir: process.cwd(),
}, ['foo@2.0.0'])
expect(dlx.handler).toHaveBeenCalledWith(expect.anything(), ['create-foo@2.0.0'])
await create.handler({
...DEFAULT_OPTS,
dir: process.cwd(),
}, ['foo@latest'])
expect(dlx.handler).toHaveBeenCalledWith(expect.anything(), ['create-foo@latest'])
await create.handler({
...DEFAULT_OPTS,
dir: process.cwd(),
}, ['@scope@2.0.0'])
expect(dlx.handler).toHaveBeenCalledWith(expect.anything(), ['@scope/create@2.0.0'])
await create.handler({
...DEFAULT_OPTS,
dir: process.cwd(),
}, ['@scope@next'])
expect(dlx.handler).toHaveBeenCalledWith(expect.anything(), ['@scope/create@next'])
await create.handler({
...DEFAULT_OPTS,
dir: process.cwd(),
}, ['@scope/foo@2.0.0'])
expect(dlx.handler).toHaveBeenCalledWith(expect.anything(), ['@scope/create-foo@2.0.0'])
await create.handler({
...DEFAULT_OPTS,
dir: process.cwd(),
}, ['@scope/create-a@2.0.0'])
expect(dlx.handler).toHaveBeenCalledWith(expect.anything(), ['@scope/create-a@2.0.0'])
}
)