create-node-lib
Version:
Scaffolding out a Node.js library module
207 lines (177 loc) • 7.42 kB
JavaScript
const path = require('path')
const sao = require('sao')
const template = path.join(__dirname, '..')
describe('all the template files are accountable for', () => {
test('generator contains github templates', async () => {
jest.setTimeout(15000)
const stream = await sao.mock({ generator: template })
expect(stream.fileList).toContain('.github/ISSUE_TEMPLATE.md')
expect(stream.fileList).toContain('.github/ISSUE_TEMPLATE/1-bug-report.yml')
expect(stream.fileList).toContain('.github/ISSUE_TEMPLATE/2-feature-request.yml')
expect(stream.fileList).toContain('.github/ISSUE_TEMPLATE/3-help.md')
expect(stream.fileList).toContain('.github/PULL_REQUEST_TEMPLATE.md')
expect(stream.fileList).toContain('.github/CODE_OF_CONDUCT.md')
expect(stream.fileList).toContain('CONTRIBUTING.md')
})
test('generator contains project files', async () => {
const stream = await sao.mock(
{ generator: template },
{
features: ['linter'],
linterConfig: 'xo'
}
)
expect(stream.fileList).toContain('.gitignore')
expect(stream.fileList).toContain('.npmrc')
expect(stream.fileList).toContain('.prettierignore')
expect(stream.fileList).toContain('.prettierrc.json')
expect(stream.fileList).toContain('.github/workflows/ci.yml')
expect(stream.fileList).toContain('LICENSE')
expect(stream.fileList).toContain('README.md')
expect(stream.fileList).toContain('__tests__/app.test.ts')
expect(stream.fileList).toContain('package.json')
expect(stream.fileList).toContain('apm.yml')
})
test('Generator input creates a valid package.json file', async () => {
const mockProjectName = 'acme'
const mockProjectAuthor = 'Alice in Chains'
const mockProjectDescription = 'hello'
const mockProjectKeywords = ['kw1, kw2']
const mockUsername = 'alice'
const mockUserEmail = 'alice@inchains.com'
const mockProjectRepository = 'https://www.github.com/alice/inchains.git'
const stream = await sao.mock(
{ generator: template },
{
keywords: [mockProjectKeywords],
projectName: mockProjectName,
description: mockProjectDescription,
author: mockProjectAuthor,
username: mockUsername,
email: mockUserEmail,
projectRepository: mockProjectRepository
}
)
const pkg = JSON.parse(await stream.readFile('package.json'))
const apmYaml = await stream.readFile('apm.yml')
expect(apmYaml).toContain(`name: "${mockProjectName}"`)
expect(apmYaml).toContain(`description: "${mockProjectDescription}"`)
expect(apmYaml).toContain(`author: "${mockProjectAuthor}"`)
expect(pkg.name).toBe(mockProjectName)
expect(pkg.description).toBe(mockProjectDescription)
expect(pkg.author.name).toBe(mockProjectAuthor)
expect(pkg.author.email).toBe(mockUserEmail)
expect(pkg.homepage).toBe(mockProjectRepository)
expect(pkg.keywords).toEqual(mockProjectKeywords)
expect(pkg.engines.pnpm).toBe('>=10.26.0')
expect(pkg.engines.npm).toBeUndefined()
expect(pkg.packageManager).toBeUndefined()
expect(pkg.scripts.lint).toBe('eslint . && pnpm run lint:markdown')
expect(pkg.scripts['lint:lockfile']).toBeUndefined()
})
test('Generator includes pnpm-workspace.yaml when pnpm is selected', async () => {
const stream = await sao.mock(
{ generator: template },
{
npmClient: 'pnpm'
}
)
expect(stream.fileList).toContain('pnpm-workspace.yaml')
})
test('Generator excludes pnpm-workspace.yaml when npm is selected', async () => {
const stream = await sao.mock(
{ generator: template },
{
npmClient: 'npm'
}
)
expect(stream.fileList).not.toContain('pnpm-workspace.yaml')
})
test('Generator input creates correct package.json scripts with npm as client', async () => {
const stream = await sao.mock(
{ generator: template },
{
npmClient: 'npm'
}
)
const pkg = JSON.parse(await stream.readFile('package.json'))
expect(pkg.scripts.lint).toBe('eslint . && npm run lint:markdown')
expect(pkg.scripts['lint:lockfile']).toBeUndefined()
expect(pkg.engines.npm).toBe('>=11.10.0')
expect(pkg.engines.pnpm).toBeUndefined()
expect(pkg.packageManager).toBeUndefined()
})
test('Generator creates package.json with prepare script for husky', async () => {
const stream = await sao.mock({ generator: template })
const pkg = JSON.parse(await stream.readFile('package.json'))
expect(pkg.scripts.prepare).toBe('husky')
})
test('Generator creates package-manager aware husky hooks', async () => {
const pnpmStream = await sao.mock(
{ generator: template },
{
npmClient: 'pnpm'
}
)
const npmStream = await sao.mock(
{ generator: template },
{
npmClient: 'npm'
}
)
expect(await pnpmStream.readFile('.husky/commit-msg')).toBe('pnpm exec validate-conventional-commit < "$1"\n')
expect(await pnpmStream.readFile('.husky/pre-commit')).toBe('pnpm exec lint-staged\n')
expect(await npmStream.readFile('.husky/commit-msg')).toBe('npm exec validate-conventional-commit < "$1"\n')
expect(await npmStream.readFile('.husky/pre-commit')).toBe('npm exec lint-staged\n')
})
test('Generator creates changeset config.json file', async () => {
const stream = await sao.mock({ generator: template })
expect(stream.fileList).toContain('.changeset/config.json')
})
test('Generator creates changeset config with correct repo from projectRepository', async () => {
const mockProjectRepository = 'https://github.com/alice/wonderland'
const stream = await sao.mock(
{ generator: template },
{
projectRepository: mockProjectRepository
}
)
const changesetConfig = JSON.parse(await stream.readFile('.changeset/config.json'))
expect(changesetConfig.changelog[1].repo).toBe('alice/wonderland')
expect(changesetConfig.$schema).toBe('https://unpkg.com/@changesets/config/schema.json')
expect(changesetConfig.access).toBe('public')
expect(changesetConfig.baseBranch).toBe('main')
})
test('Generator creates changeset config with repo extracted from .git URL', async () => {
const mockProjectRepository = 'https://github.com/bob/project.git'
const stream = await sao.mock(
{ generator: template },
{
projectRepository: mockProjectRepository
}
)
const changesetConfig = JSON.parse(await stream.readFile('.changeset/config.json'))
expect(changesetConfig.changelog[1].repo).toBe('bob/project')
})
test('Generator handles URL with trailing slash', async () => {
const mockProjectRepository = 'https://github.com/user/repo/'
const stream = await sao.mock(
{ generator: template },
{
projectRepository: mockProjectRepository
}
)
const changesetConfig = JSON.parse(await stream.readFile('.changeset/config.json'))
expect(changesetConfig.changelog[1].repo).toBe('user/repo')
})
test('Generator handles empty projectRepository gracefully', async () => {
const stream = await sao.mock(
{ generator: template },
{
projectRepository: ''
}
)
const changesetConfig = JSON.parse(await stream.readFile('.changeset/config.json'))
expect(changesetConfig.changelog[1].repo).toBe('')
})
})