Skip to content

Commit

Permalink
fix: add two tests
Browse files Browse the repository at this point in the history
  • Loading branch information
xiongmao86 committed Jan 8, 2024
1 parent 909cfab commit 1d965ae
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/commands/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,10 @@ class Install extends ArboristWorkspaceCmd {
const isJsonEmptyObject = Object.keys(jsonContent).length === 0
&& jsonContent.constructor === Object

const emptyJson = new Error()
const emptyJson = new Error('EEMPTYPKGJSON: package.json is empty object')
emptyJson.code = 'EEMPTYPKGJSON'
emptyJson.path = pkgJson.filename
if(isJsonEmptyObject) {
if (isJsonEmptyObject) {
throw emptyJson
}
}
Expand Down
42 changes: 42 additions & 0 deletions test/lib/commands/install.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const t = require('tap')
const { load: loadMockNpm } = require('../../fixtures/mock-npm')
const fs = require('node:fs')
const path = require('node:path')

t.test('exec commands', async t => {
await t.test('with args, dev=true', async t => {
Expand All @@ -9,6 +11,9 @@ t.test('exec commands', async t => {
let ARB_OBJ = null

const { npm } = await loadMockNpm(t, {
prefixDir: {
'package.json': '{ "name": "does not matter" }',
},
mocks: {
'@npmcli/run-script': ({ event }) => {
SCRIPTS.push(event)
Expand Down Expand Up @@ -53,6 +58,9 @@ t.test('exec commands', async t => {
let ARB_OBJ = null

const { npm } = await loadMockNpm(t, {
prefixDir: {
'package.json': '{ "name": "does not matter" }',
},
mocks: {
'@npmcli/run-script': ({ event }) => {
SCRIPTS.push(event)
Expand Down Expand Up @@ -93,6 +101,9 @@ t.test('exec commands', async t => {
const SCRIPTS = []
let REIFY_CALLED = false
const { npm } = await loadMockNpm(t, {
prefixDir: {
'package.json': '{ "name": "does not matter" }',
},
mocks: {
'{LIB}/utils/reify-finish.js': async () => {},
'@npmcli/run-script': ({ event }) => {
Expand Down Expand Up @@ -165,6 +176,37 @@ t.test('exec commands', async t => {
)
})

await t.test('should throw when package.json does not exist', async t => {
const { npm, prefix } = await loadMockNpm(t, {})
await t.rejects(
npm.exec('install'),
/ENOENT: no such file or directory/,
'should throw before reify'
)
t.notOk(
fs.existsSync(path.join(prefix, './package-lock.json')),
'should not generate package-lock.json'
)
})

await t.test('should throw when package.json is an empty object', async t => {
const { npm, prefix } = await loadMockNpm(t, {
prefixDir: {
'package.json': '{}',
},
})
await t.rejects(
npm.exec('install'),
/EEMPTYPKGJSON: package.json is empty object/,
'should throw before reify'
)

t.notOk(
fs.existsSync(path.join(prefix, './package-lock.json')),
'should not generate package-lock.json'
)
})

await t.test('npm i -g npm engines check success', async t => {
const { npm } = await loadMockNpm(t, {
mocks: {
Expand Down

0 comments on commit 1d965ae

Please sign in to comment.