npm-dependency-prune.test.mjs 2.02 KB
import assert from 'node:assert/strict'
import { readFile } from 'node:fs/promises'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import { describe, it } from 'node:test'

const projectRoot = path.resolve(
  path.dirname(fileURLToPath(import.meta.url)),
  '../..'
)

describe('mini-program npm dependency pruning', () => {
  it('removes the unused glsx component plugin from every dependency manifest', async () => {
    const packageJson = JSON.parse(
      await readFile(path.join(projectRoot, 'package.json'), 'utf8')
    )
    const packageLock = JSON.parse(
      await readFile(path.join(projectRoot, 'package-lock.json'), 'utf8')
    )
    const yarnLock = await readFile(path.join(projectRoot, 'yarn.lock'), 'utf8')

    assert.equal(packageJson.dependencies['glsx-applet-component'], undefined)
    assert.equal(
      packageLock.packages[''].dependencies['glsx-applet-component'],
      undefined
    )
    assert.equal(
      packageLock.packages['node_modules/glsx-applet-component'],
      undefined
    )
    assert.doesNotMatch(yarnLock, /^glsx-applet-component@/m)
  })

  it('keeps the privacy component dependency used by app.json', async () => {
    const packageJson = JSON.parse(
      await readFile(path.join(projectRoot, 'package.json'), 'utf8')
    )
    const packageLock = JSON.parse(
      await readFile(path.join(projectRoot, 'package-lock.json'), 'utf8')
    )
    const yarnLock = await readFile(path.join(projectRoot, 'yarn.lock'), 'utf8')
    const appConfig = JSON.parse(
      await readFile(path.join(projectRoot, 'app.json'), 'utf8')
    )

    assert.equal(packageJson.dependencies['glsk-applet-component'], '^1.0.0')
    assert.equal(
      packageLock.packages[''].dependencies['glsk-applet-component'],
      '^1.0.0'
    )
    assert.ok(packageLock.packages['node_modules/glsk-applet-component'])
    assert.match(yarnLock, /^glsk-applet-component@\^1\.0\.0:\r?$/m)
    assert.equal(
      appConfig.usingComponents['glsx-privacy'],
      'glsk-applet-component/GlsxPrivacy/index'
    )
  })
})