Skip to content

Commit 7b8ab86

Browse files
committed
fix(test): added npm init parity test
1 parent 6298644 commit 7b8ab86

File tree

3 files changed

+43
-7
lines changed

3 files changed

+43
-7
lines changed

.github/workflows/test.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ jobs:
2727
node-version: ${{ fromJson(needs.setup.outputs.nodes) }}
2828
steps:
2929
- uses: actions/checkout@v4
30+
- name: Setup git user
31+
run: |
32+
git config --global user.name "$(git --no-pager log --format=format:'%an' -n 1)"
33+
git config --global user.email "$(git --no-pager log --format=format:'%ae' -n 1)"
3034
- name: Use Node.js ${{ matrix.node-version }}
3135
uses: actions/setup-node@v3
3236
with:

index.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -298,10 +298,8 @@ async function format (opts, pkg = {}) {
298298
pkg.main = opts.main
299299
pkg.type = opts.type || 'commonjs'
300300

301-
if (opts.keywords && opts.keywords.length) {
302-
// TODO: extra parsing going on here due to wesleytodd/opta#1
303-
pkg.keywords = uniquify([...(pkg.keywords || []), ...parseList(opts.keywords)])
304-
}
301+
// TODO: extra parsing going on here due to wesleytodd/opta#1
302+
pkg.keywords = uniquify([...(pkg.keywords || []), ...parseList(opts.keywords || [])])
305303

306304
// Scripts
307305
if (Object.keys(opts.scripts).length) {

test/index.js

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
'use strict'
2-
const path = require('path')
3-
const assert = require('assert')
2+
const path = require('node:path')
3+
const assert = require('node:assert')
4+
const fs = require('node:fs/promises')
5+
const { promisify } = require('node:util')
6+
const execFile = promisify(require('node:child_process').execFile)
47
const { suite, test, before } = require('mocha')
58
const fixtures = require('fs-test-fixtures')
69
const createPkgJson = require('..')
@@ -79,7 +82,7 @@ suite('create-package-json', () => {
7982
assert.strictEqual(pkg.description, '')
8083
assert.strictEqual(pkg.author, 'Test User <[email protected]>')
8184
assert.strictEqual(pkg.repository, undefined)
82-
assert.strictEqual(pkg.keywords, undefined)
85+
assert.deepStrictEqual(pkg.keywords, [])
8386
assert.strictEqual(pkg.license, 'ISC')
8487
assert.strictEqual(pkg.type, 'commonjs')
8588
assert.strictEqual(pkg.main, 'index.js')
@@ -248,4 +251,35 @@ suite('create-package-json', () => {
248251
assert.deepStrictEqual(pkg.man, './man/foo.1')
249252
})
250253
})
254+
255+
suite('npm init', () => {
256+
test('parity', async () => {
257+
await fix.setup()
258+
try {
259+
await execFile('npm', ['init', '-y'], {
260+
cwd: fix.TMP
261+
})
262+
} catch (e) {
263+
console.error(e)
264+
throw e
265+
}
266+
const npmInitPkg = JSON.parse(await fs.readFile(path.join(fix.TMP, 'package.json'), 'utf8'))
267+
268+
await fix.setup()
269+
const pkg = await createPackageJson()
270+
271+
// Should be the same
272+
assert.strictEqual(pkg.name, npmInitPkg.name)
273+
assert.strictEqual(pkg.version, npmInitPkg.version)
274+
assert.strictEqual(pkg.description, npmInitPkg.description)
275+
assert.strictEqual(pkg.main, npmInitPkg.main)
276+
assert.deepStrictEqual(pkg.scripts, npmInitPkg.scripts)
277+
assert.deepStrictEqual(pkg.keywords, npmInitPkg.keywords)
278+
assert.strictEqual(pkg.license, npmInitPkg.license)
279+
280+
// Should be different
281+
assert.notStrictEqual(pkg.author, npmInitPkg.author, JSON.stringify([pkg.author, npmInitPkg.author]))
282+
assert.notStrictEqual(pkg.type, npmInitPkg.type, JSON.stringify([pkg.type, npmInitPkg.type]))
283+
})
284+
})
251285
})

0 commit comments

Comments
 (0)