Skip to content

Commit

Permalink
fix(test): added npm init parity test
Browse files Browse the repository at this point in the history
  • Loading branch information
wesleytodd committed Oct 2, 2023
1 parent 6298644 commit edfe6df
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ async function readPackageJson (options, { log } = {}) {
author,
description: pkg.description,
repository: repo,
keywords: pkg.keywords,
keywords: pkg.keywords || [],
scripts: pkg.scripts,
license: pkg.license
})
Expand All @@ -298,7 +298,7 @@ async function format (opts, pkg = {}) {
pkg.main = opts.main
pkg.type = opts.type || 'commonjs'

if (opts.keywords && opts.keywords.length) {
if (opts.keywords) {
// TODO: extra parsing going on here due to wesleytodd/opta#1
pkg.keywords = uniquify([...(pkg.keywords || []), ...parseList(opts.keywords)])
}
Expand Down
40 changes: 37 additions & 3 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
'use strict'
const path = require('path')
const assert = require('assert')
const path = require('node:path')
const assert = require('node:assert')
const fs = require('node:fs/promises')
const { promisify } = require('node:util')
const execFile = promisify(require('node:child_process').execFile)
const { suite, test, before } = require('mocha')
const fixtures = require('fs-test-fixtures')
const createPkgJson = require('..')
Expand Down Expand Up @@ -79,7 +82,7 @@ suite('create-package-json', () => {
assert.strictEqual(pkg.description, '')
assert.strictEqual(pkg.author, 'Test User <[email protected]>')
assert.strictEqual(pkg.repository, undefined)
assert.strictEqual(pkg.keywords, undefined)
assert.deepStrictEqual(pkg.keywords, [])
assert.strictEqual(pkg.license, 'ISC')
assert.strictEqual(pkg.type, 'commonjs')
assert.strictEqual(pkg.main, 'index.js')
Expand Down Expand Up @@ -248,4 +251,35 @@ suite('create-package-json', () => {
assert.deepStrictEqual(pkg.man, './man/foo.1')
})
})

suite('npm init', () => {
test('parity', async () => {
await fix.setup()
try {
await execFile('npm', ['init', '-y'], {
cwd: fix.TMP
})
} catch (e) {
console.error(e)
throw e
}
const npmInitPkg = JSON.parse(await fs.readFile(path.join(fix.TMP, 'package.json'), 'utf8'))

await fix.setup()
const pkg = await createPackageJson()

// Should be the same
assert.strictEqual(pkg.name, npmInitPkg.name)
assert.strictEqual(pkg.version, npmInitPkg.version)
assert.strictEqual(pkg.description, npmInitPkg.description)
assert.strictEqual(pkg.main, npmInitPkg.main)
assert.deepStrictEqual(pkg.scripts, npmInitPkg.scripts)
assert.deepStrictEqual(pkg.keywords, npmInitPkg.keywords)
assert.strictEqual(pkg.license, npmInitPkg.license)

// Should be different
assert.notStrictEqual(pkg.author, npmInitPkg.author, [pkg.author, npmInitPkg.author].toString())
assert.notStrictEqual(pkg.type, npmInitPkg.type, [pkg.type, npmInitPkg.type].toString())
})
})
})

0 comments on commit edfe6df

Please sign in to comment.