diff options
author | 2025-06-05 14:25:23 +0000 | |
---|---|---|
committer | 2025-06-05 14:25:23 +0000 | |
commit | e586d7d704d475afe3373a1de6ae20d504f79d6d (patch) | |
tree | 7e3fa24807cebd48a86bd40f866d792181191ee9 /packages/create-astro/test | |
download | astro-e586d7d704d475afe3373a1de6ae20d504f79d6d.tar.gz astro-e586d7d704d475afe3373a1de6ae20d504f79d6d.tar.zst astro-e586d7d704d475afe3373a1de6ae20d504f79d6d.zip |
Sync from a8e1c0a7402940e0fc5beef669522b315052df1blatest
Diffstat (limited to 'packages/create-astro/test')
-rw-r--r-- | packages/create-astro/test/context.test.js | 73 | ||||
-rw-r--r-- | packages/create-astro/test/dependencies.test.js | 80 | ||||
-rw-r--r-- | packages/create-astro/test/fixtures/empty/.gitkeep | 0 | ||||
-rw-r--r-- | packages/create-astro/test/fixtures/not-empty/git.json | 1 | ||||
-rw-r--r-- | packages/create-astro/test/fixtures/not-empty/package.json | 9 | ||||
-rw-r--r-- | packages/create-astro/test/fixtures/not-empty/tsconfig.json | 1 | ||||
-rw-r--r-- | packages/create-astro/test/git.test.js | 57 | ||||
-rw-r--r-- | packages/create-astro/test/integrations.test.js | 64 | ||||
-rw-r--r-- | packages/create-astro/test/intro.test.js | 20 | ||||
-rw-r--r-- | packages/create-astro/test/next.test.js | 20 | ||||
-rw-r--r-- | packages/create-astro/test/project-name.test.js | 124 | ||||
-rw-r--r-- | packages/create-astro/test/template.test.js | 39 | ||||
-rw-r--r-- | packages/create-astro/test/utils.js | 32 | ||||
-rw-r--r-- | packages/create-astro/test/verify.test.js | 41 |
14 files changed, 561 insertions, 0 deletions
diff --git a/packages/create-astro/test/context.test.js b/packages/create-astro/test/context.test.js new file mode 100644 index 000000000..b4e67a8c6 --- /dev/null +++ b/packages/create-astro/test/context.test.js @@ -0,0 +1,73 @@ +import assert from 'node:assert/strict'; +import os from 'node:os'; +import { describe, it } from 'node:test'; +import { getContext } from '../dist/index.js'; +describe('context', () => { + it('no arguments', async () => { + const ctx = await getContext([]); + assert.ok(!ctx.projectName); + assert.ok(!ctx.template); + assert.deepEqual(ctx.skipHouston, os.platform() === 'win32'); + assert.ok(!ctx.dryRun); + }); + + it('project name', async () => { + const ctx = await getContext(['foobar']); + assert.deepEqual(ctx.projectName, 'foobar'); + }); + + it('template', async () => { + const ctx = await getContext(['--template', 'minimal']); + assert.deepEqual(ctx.template, 'minimal'); + }); + + it('skip houston (explicit)', async () => { + const ctx = await getContext(['--skip-houston']); + assert.deepEqual(ctx.skipHouston, true); + }); + + it('skip houston (yes)', async () => { + const ctx = await getContext(['-y']); + assert.deepEqual(ctx.skipHouston, true); + }); + + it('skip houston (no)', async () => { + const ctx = await getContext(['-n']); + assert.deepEqual(ctx.skipHouston, true); + }); + + it('skip houston (install)', async () => { + const ctx = await getContext(['--install']); + assert.deepEqual(ctx.skipHouston, true); + }); + + it('dry run', async () => { + const ctx = await getContext(['--dry-run']); + assert.deepEqual(ctx.dryRun, true); + }); + + it('install', async () => { + const ctx = await getContext(['--install']); + assert.deepEqual(ctx.install, true); + }); + + it('add', async () => { + const ctx = await getContext(['--add', 'node']); + assert.deepEqual(ctx.add, ['node']); + }); + + it('no install', async () => { + const ctx = await getContext(['--no-install']); + assert.deepEqual(ctx.install, false); + }); + + it('git', async () => { + const ctx = await getContext(['--git']); + assert.deepEqual(ctx.git, true); + }); + + it('no git', async () => { + const ctx = await getContext(['--no-git']); + assert.deepEqual(ctx.git, false); + }); +}); diff --git a/packages/create-astro/test/dependencies.test.js b/packages/create-astro/test/dependencies.test.js new file mode 100644 index 000000000..e4eb3d1f4 --- /dev/null +++ b/packages/create-astro/test/dependencies.test.js @@ -0,0 +1,80 @@ +import assert from 'node:assert/strict'; +import { describe, it } from 'node:test'; +import { dependencies } from '../dist/index.js'; +import { setup } from './utils.js'; +describe('dependencies', () => { + const fixture = setup(); + + it('--yes', async () => { + const context = { + cwd: '', + yes: true, + packageManager: 'npm', + dryRun: true, + prompt: () => ({ deps: true }), + }; + + await dependencies(context); + + assert.ok(fixture.hasMessage('Skipping dependency installation')); + }); + + it('prompt yes', async () => { + const context = { + cwd: '', + packageManager: 'npm', + dryRun: true, + prompt: () => ({ deps: true }), + install: undefined, + }; + + await dependencies(context); + + assert.ok(fixture.hasMessage('Skipping dependency installation')); + assert.equal(context.install, true); + }); + + it('prompt no', async () => { + const context = { + cwd: '', + install: true, + packageManager: 'npm', + dryRun: true, + prompt: () => ({ deps: false }), + install: undefined, + }; + + await dependencies(context); + + assert.ok(fixture.hasMessage('Skipping dependency installation')); + assert.equal(context.install, false); + }); + + it('--install', async () => { + const context = { + cwd: '', + install: true, + packageManager: 'npm', + dryRun: true, + prompt: () => ({ deps: false }), + }; + await dependencies(context); + assert.ok(fixture.hasMessage('Skipping dependency installation')); + assert.equal(context.install, true); + }); + + it('--no-install ', async () => { + const context = { + cwd: '', + install: false, + packageManager: 'npm', + dryRun: true, + prompt: () => ({ deps: false }), + }; + + await dependencies(context); + + assert.ok(fixture.hasMessage('Skipping dependency installation')); + assert.equal(context.install, false); + }); +}); diff --git a/packages/create-astro/test/fixtures/empty/.gitkeep b/packages/create-astro/test/fixtures/empty/.gitkeep new file mode 100644 index 000000000..e69de29bb --- /dev/null +++ b/packages/create-astro/test/fixtures/empty/.gitkeep diff --git a/packages/create-astro/test/fixtures/not-empty/git.json b/packages/create-astro/test/fixtures/not-empty/git.json new file mode 100644 index 000000000..9e26dfeeb --- /dev/null +++ b/packages/create-astro/test/fixtures/not-empty/git.json @@ -0,0 +1 @@ +{}
\ No newline at end of file diff --git a/packages/create-astro/test/fixtures/not-empty/package.json b/packages/create-astro/test/fixtures/not-empty/package.json new file mode 100644 index 000000000..d3f61d640 --- /dev/null +++ b/packages/create-astro/test/fixtures/not-empty/package.json @@ -0,0 +1,9 @@ +{ + "name": "@test/create-astro-not-empty", + "private": true, + "scripts": { + "dev": "astro dev", + "build": "astro build", + "preview": "astro preview" + } +}
\ No newline at end of file diff --git a/packages/create-astro/test/fixtures/not-empty/tsconfig.json b/packages/create-astro/test/fixtures/not-empty/tsconfig.json new file mode 100644 index 000000000..9e26dfeeb --- /dev/null +++ b/packages/create-astro/test/fixtures/not-empty/tsconfig.json @@ -0,0 +1 @@ +{}
\ No newline at end of file diff --git a/packages/create-astro/test/git.test.js b/packages/create-astro/test/git.test.js new file mode 100644 index 000000000..85854f0d5 --- /dev/null +++ b/packages/create-astro/test/git.test.js @@ -0,0 +1,57 @@ +import assert from 'node:assert/strict'; +import { rmSync } from 'node:fs'; +import { mkdir, writeFile } from 'node:fs/promises'; +import { after, before, describe, it } from 'node:test'; + +import { git } from '../dist/index.js'; +import { setup } from './utils.js'; + +describe('git', () => { + const fixture = setup(); + + it('none', async () => { + const context = { cwd: '', dryRun: true, prompt: () => ({ git: false }) }; + await git(context); + + assert.ok(fixture.hasMessage('Skipping Git initialization')); + }); + + it('yes (--dry-run)', async () => { + const context = { cwd: '', dryRun: true, prompt: () => ({ git: true }) }; + await git(context); + assert.ok(fixture.hasMessage('Skipping Git initialization')); + }); + + it('no (--dry-run)', async () => { + const context = { cwd: '', dryRun: true, prompt: () => ({ git: false }) }; + await git(context); + + assert.ok(fixture.hasMessage('Skipping Git initialization')); + }); +}); + +describe('git initialized', () => { + const fixture = setup(); + const dir = new URL(new URL('./fixtures/not-empty/.git', import.meta.url)); + + before(async () => { + await mkdir(dir, { recursive: true }); + await writeFile(new URL('./git.json', dir), '{}', { encoding: 'utf8' }); + }); + + it('already initialized', async () => { + const context = { + git: true, + cwd: './test/fixtures/not-empty', + dryRun: false, + prompt: () => ({ git: false }), + }; + await git(context); + + assert.ok(fixture.hasMessage('Git has already been initialized')); + }); + + after(() => { + rmSync(dir, { recursive: true, force: true }); + }); +}); diff --git a/packages/create-astro/test/integrations.test.js b/packages/create-astro/test/integrations.test.js new file mode 100644 index 000000000..412285223 --- /dev/null +++ b/packages/create-astro/test/integrations.test.js @@ -0,0 +1,64 @@ +import assert from 'node:assert/strict'; +import { describe, it } from 'node:test'; +import { dependencies } from '../dist/index.js'; +import { setup } from './utils.js'; +describe('integrations', () => { + const fixture = setup(); + + it('--add node', async () => { + const context = { + cwd: '', + yes: true, + packageManager: 'npm', + dryRun: true, + add: ['node'], + }; + + await dependencies(context); + + assert.ok(fixture.hasMessage('--dry-run Skipping dependency installation and adding node')); + }); + + it('--add node --add react', async () => { + const context = { + cwd: '', + yes: true, + packageManager: 'npm', + dryRun: true, + add: ['node', 'react'], + }; + + await dependencies(context); + + assert.ok( + fixture.hasMessage('--dry-run Skipping dependency installation and adding node, react'), + ); + }); + + it('--add node,react', async () => { + const context = { + cwd: '', + yes: true, + packageManager: 'npm', + dryRun: true, + add: ['node,react'], + }; + + await dependencies(context); + + assert.ok( + fixture.hasMessage('--dry-run Skipping dependency installation and adding node, react'), + ); + }); + + it('-y', async () => { + const context = { + cwd: '', + yes: true, + packageManager: 'npm', + dryRun: true, + }; + await dependencies(context); + assert.ok(fixture.hasMessage('--dry-run Skipping dependency installation')); + }); +}); diff --git a/packages/create-astro/test/intro.test.js b/packages/create-astro/test/intro.test.js new file mode 100644 index 000000000..d042dad7f --- /dev/null +++ b/packages/create-astro/test/intro.test.js @@ -0,0 +1,20 @@ +import assert from 'node:assert/strict'; +import { describe, it } from 'node:test'; +import { intro } from '../dist/index.js'; +import { setup } from './utils.js'; + +describe('intro', () => { + const fixture = setup(); + + it('no arguments', async () => { + await intro({ skipHouston: false, version: '0.0.0', username: 'user' }); + assert.ok(fixture.hasMessage('Houston:')); + assert.ok(fixture.hasMessage('Welcome to astro v0.0.0')); + }); + it('--skip-houston', async () => { + await intro({ skipHouston: true, version: '0.0.0', username: 'user' }); + assert.equal(fixture.length(), 1); + assert.ok(!fixture.hasMessage('Houston:')); + assert.ok(fixture.hasMessage('Launch sequence initiated')); + }); +}); diff --git a/packages/create-astro/test/next.test.js b/packages/create-astro/test/next.test.js new file mode 100644 index 000000000..5b9b22b30 --- /dev/null +++ b/packages/create-astro/test/next.test.js @@ -0,0 +1,20 @@ +import assert from 'node:assert/strict'; +import { describe, it } from 'node:test'; +import { next } from '../dist/index.js'; +import { setup } from './utils.js'; + +describe('next steps', () => { + const fixture = setup(); + + it('no arguments', async () => { + await next({ skipHouston: false, cwd: './it/fixtures/not-empty', packageManager: 'npm' }); + assert.ok(fixture.hasMessage('Liftoff confirmed.')); + assert.ok(fixture.hasMessage('npm run dev')); + assert.ok(fixture.hasMessage('Good luck out there, astronaut!')); + }); + + it('--skip-houston', async () => { + await next({ skipHouston: true, cwd: './it/fixtures/not-empty', packageManager: 'npm' }); + assert.ok(!fixture.hasMessage('Good luck out there, astronaut!')); + }); +}); diff --git a/packages/create-astro/test/project-name.test.js b/packages/create-astro/test/project-name.test.js new file mode 100644 index 000000000..0aebd1c79 --- /dev/null +++ b/packages/create-astro/test/project-name.test.js @@ -0,0 +1,124 @@ +import assert from 'node:assert/strict'; +import { describe, it } from 'node:test'; +import { projectName } from '../dist/index.js'; +import { setup } from './utils.js'; + +describe('project name', async () => { + const fixture = setup(); + + it('pass in name', async () => { + const context = { projectName: '', cwd: './foo/bar/baz', prompt: () => {} }; + await projectName(context); + assert.equal(context.cwd, './foo/bar/baz'); + assert.equal(context.projectName, 'baz'); + }); + + it('dot', async () => { + const context = { projectName: '', cwd: '.', prompt: () => ({ name: 'foobar' }) }; + await projectName(context); + assert.ok(fixture.hasMessage('"." is not empty!')); + assert.equal(context.projectName, 'foobar'); + }); + + it('dot slash', async () => { + const context = { projectName: '', cwd: './', prompt: () => ({ name: 'foobar' }) }; + await projectName(context); + assert.ok(fixture.hasMessage('"./" is not empty!')); + assert.equal(context.projectName, 'foobar'); + }); + + it('empty', async () => { + const context = { + projectName: '', + cwd: './test/fixtures/empty', + prompt: () => ({ name: 'foobar' }), + }; + await projectName(context); + assert.ok(!fixture.hasMessage('"./test/fixtures/empty" is not empty!')); + assert.equal(context.projectName, 'empty'); + }); + + it('not empty', async () => { + const context = { + projectName: '', + cwd: './test/fixtures/not-empty', + prompt: () => ({ name: 'foobar' }), + }; + await projectName(context); + assert.ok(fixture.hasMessage('"./test/fixtures/not-empty" is not empty!')); + assert.equal(context.projectName, 'foobar'); + }); + + it('basic', async () => { + const context = { projectName: '', cwd: '', prompt: () => ({ name: 'foobar' }) }; + await projectName(context); + assert.equal(context.cwd, 'foobar'); + assert.equal(context.projectName, 'foobar'); + }); + + it('head and tail blank spaces should be trimmed', async () => { + const context = { projectName: '', cwd: '', prompt: () => ({ name: ' foobar ' }) }; + await projectName(context); + assert.equal(context.cwd, 'foobar'); + assert.equal(context.projectName, 'foobar'); + }); + + it('normalize', async () => { + const context = { projectName: '', cwd: '', prompt: () => ({ name: 'Invalid Name' }) }; + await projectName(context); + assert.equal(context.cwd, 'Invalid Name'); + assert.equal(context.projectName, 'invalid-name'); + }); + + it('remove leading/trailing dashes', async () => { + const context = { projectName: '', cwd: '', prompt: () => ({ name: '(invalid)' }) }; + await projectName(context); + assert.equal(context.projectName, 'invalid'); + }); + + it('handles scoped packages', async () => { + const context = { projectName: '', cwd: '', prompt: () => ({ name: '@astro/site' }) }; + await projectName(context); + assert.equal(context.cwd, '@astro/site'); + assert.equal(context.projectName, '@astro/site'); + }); + + it('--yes', async () => { + const context = { projectName: '', cwd: './foo/bar/baz', yes: true, prompt: () => {} }; + await projectName(context); + assert.equal(context.projectName, 'baz'); + }); + + it('dry run with name', async () => { + const context = { + projectName: '', + cwd: './foo/bar/baz', + dryRun: true, + prompt: () => {}, + }; + await projectName(context); + assert.equal(context.projectName, 'baz'); + }); + + it('dry run with dot', async () => { + const context = { + projectName: '', + cwd: '.', + dryRun: true, + prompt: () => ({ name: 'foobar' }), + }; + await projectName(context); + assert.equal(context.projectName, 'foobar'); + }); + + it('dry run with empty', async () => { + const context = { + projectName: '', + cwd: './test/fixtures/empty', + dryRun: true, + prompt: () => ({ name: 'foobar' }), + }; + await projectName(context); + assert.equal(context.projectName, 'empty'); + }); +}); diff --git a/packages/create-astro/test/template.test.js b/packages/create-astro/test/template.test.js new file mode 100644 index 000000000..821ac9c2e --- /dev/null +++ b/packages/create-astro/test/template.test.js @@ -0,0 +1,39 @@ +import assert from 'node:assert/strict'; +import { describe, it } from 'node:test'; +import { template } from '../dist/index.js'; +import { setup } from './utils.js'; + +describe('template', async () => { + const fixture = setup(); + + it('none', async () => { + const context = { template: '', cwd: '', dryRun: true, prompt: () => ({ template: 'blog' }) }; + await template(context); + assert.ok(fixture.hasMessage('Skipping template copying')); + assert.equal(context.template, 'blog'); + }); + + it('minimal (--dry-run)', async () => { + const context = { template: 'minimal', cwd: '', dryRun: true, prompt: () => {} }; + await template(context); + assert.ok(fixture.hasMessage('Using minimal as project template')); + }); + + it('basics (--dry-run)', async () => { + const context = { template: 'basics', cwd: '', dryRun: true, prompt: () => {} }; + await template(context); + assert.ok(fixture.hasMessage('Using basics as project template')); + }); + + it('blog (--dry-run)', async () => { + const context = { template: 'blog', cwd: '', dryRun: true, prompt: () => {} }; + await template(context); + assert.ok(fixture.hasMessage('Using blog as project template')); + }); + + it('minimal (--yes)', async () => { + const context = { template: 'minimal', cwd: '', dryRun: true, yes: true, prompt: () => {} }; + await template(context); + assert.ok(fixture.hasMessage('Using minimal as project template')); + }); +}); diff --git a/packages/create-astro/test/utils.js b/packages/create-astro/test/utils.js new file mode 100644 index 000000000..20063ec53 --- /dev/null +++ b/packages/create-astro/test/utils.js @@ -0,0 +1,32 @@ +import { before, beforeEach } from 'node:test'; +import { stripVTControlCharacters } from 'node:util'; +import { setStdout } from '../dist/index.js'; + +export function setup() { + const ctx = { messages: [] }; + before(() => { + setStdout( + Object.assign({}, process.stdout, { + write(buf) { + ctx.messages.push(stripVTControlCharacters(String(buf)).trim()); + return true; + }, + }), + ); + }); + beforeEach(() => { + ctx.messages = []; + }); + + return { + messages() { + return ctx.messages; + }, + length() { + return ctx.messages.length; + }, + hasMessage(content) { + return !!ctx.messages.find((msg) => msg.includes(content)); + }, + }; +} diff --git a/packages/create-astro/test/verify.test.js b/packages/create-astro/test/verify.test.js new file mode 100644 index 000000000..ff3350145 --- /dev/null +++ b/packages/create-astro/test/verify.test.js @@ -0,0 +1,41 @@ +import assert from 'node:assert/strict'; +import { describe, it } from 'node:test'; +import { verify } from '../dist/index.js'; +import { setup } from './utils.js'; + +describe('verify', async () => { + const fixture = setup(); + const exit = (code) => { + throw code; + }; + + it('basics', async () => { + const context = { template: 'basics', exit }; + await verify(context); + assert.equal(fixture.messages().length, 0, 'Did not expect `verify` to log any messages'); + }); + + it('missing', async () => { + const context = { template: 'missing', exit }; + let err = null; + try { + await verify(context); + } catch (e) { + err = e; + } + assert.equal(err, 1); + assert.ok(!fixture.hasMessage('Template missing does not exist!')); + }); + + it('starlight', async () => { + const context = { template: 'starlight', exit }; + await verify(context); + assert.equal(fixture.messages().length, 0, 'Did not expect `verify` to log any messages'); + }); + + it('starlight/tailwind', async () => { + const context = { template: 'starlight/tailwind', exit }; + await verify(context); + assert.equal(fixture.messages().length, 0, 'Did not expect `verify` to log any messages'); + }); +}); |