Skip to content

Commit

Permalink
test: some test case
Browse files Browse the repository at this point in the history
  • Loading branch information
Cphayim committed Feb 25, 2022
1 parent cdfb6a0 commit a28cac6
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 24 deletions.
68 changes: 68 additions & 0 deletions packages/npm-helper/src/__tests__/querier.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { jest } from '@jest/globals'
import { DistTag } from '../common.js'

const mockResponseJson = jest.fn()
jest.unstable_mockModule('node-fetch', () => ({
default: jest.fn(() =>
Promise.resolve({
json: mockResponseJson,
}),
),
}))

const {
queryPackageInfo,
queryPackageVersion,
queryPackageLatestVersion,
queryPackageNextVersion,
queryPackageLegacyVersion,
} = await import('../querier.js')

const packageInfo = {
name: '@vrn-deco/cli-exists',
'dist-tags': {
latest: '1.0.0',
next: '1.0.2',
legacy: '0.3.4',
},
}

describe('@vrn-deco/cli-npm-helper -> querier.ts', () => {
test('Query for a package that dose not exists, will throw error', async () => {
const errorInfo = { error: 'not found' }
mockResponseJson.mockReturnValueOnce(errorInfo)
expect(async () => {
await queryPackageInfo('@vrn-deco/cli-not-exists')
}).rejects.toThrow('NPMQuery failed: package @vrn-deco/cli-not-exists not found')
})

test('Query for a package, will return the correct information', async () => {
mockResponseJson.mockReturnValueOnce(packageInfo)
const info = await queryPackageInfo('@vrn-deco/cli-exists')
expect(info).toEqual(packageInfo)
})

test('Can query the package specified dist-tag version', async () => {
mockResponseJson.mockReturnValueOnce(packageInfo)
const version = await queryPackageVersion('@vrn-deco/cli-exists', DistTag.Latest)
expect(version).toEqual(packageInfo['dist-tags'].latest)
})

test('Can query the package latest tag version', async () => {
mockResponseJson.mockReturnValueOnce(packageInfo)
const version = await queryPackageLatestVersion('@vrn-deco/cli-exists')
expect(version).toEqual(packageInfo['dist-tags'].latest)
})

test('Can query the package next tag version', async () => {
mockResponseJson.mockReturnValueOnce(packageInfo)
const version = await queryPackageNextVersion('@vrn-deco/cli-exists')
expect(version).toEqual(packageInfo['dist-tags'].next)
})

test('Can query the package legacy tag version', async () => {
mockResponseJson.mockReturnValueOnce(packageInfo)
const version = await queryPackageLegacyVersion('@vrn-deco/cli-exists')
expect(version).toEqual(packageInfo['dist-tags'].legacy)
})
})
54 changes: 54 additions & 0 deletions packages/npm-helper/src/__tests__/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import { jest } from '@jest/globals'

import { logger } from '@vrn-deco/cli-log'

import { isDistTagVersion, isPackage, parseModuleMap } from '../utils.js'

const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)

describe('@vrn-deco/cli-npm-helper -> utils.ts -> isPackage', () => {
test('can verify a directory is a package', () => {
expect(isPackage(__dirname)).toBe(false)
expect(isPackage(path.join(__dirname, '..', '..'))).toBe(true)
})
})

describe('@vrn-deco/cli-npm-helper -> utils.ts -> parseModuleMap', () => {
beforeAll(() => {
jest.spyOn(logger, 'warn').mockImplementation(() => void 0)
})
afterAll(() => {
jest.restoreAllMocks()
})

beforeEach(() => {
process.env.VRN_CLI_MODULE_MAP = ''
})

test('When VRN_CLI_MODULE is not define, an empty object should be returned', () => {
expect(parseModuleMap()).toEqual({})
})

test('When VRN_CLI_MODULE is not a valid JSON, an empty object should be returned', () => {
process.env.VRN_CLI_MODULE_MAP = '{ @vrn-deco/boilerplate-manifest: "/data/vrn-deco/boilerplate/manifest" }'
expect(parseModuleMap()).toEqual({})
expect(logger.warn).toHaveBeenCalledTimes(1)
})

test('When VRN_CLI_MODULE is a valid JSON, an object should be returned', () => {
process.env.VRN_CLI_MODULE_MAP = '{ "@vrn-deco/boilerplate-manifest": "/data/vrn-deco/boilerplate/manifest" }'
expect(parseModuleMap()).toEqual({ '@vrn-deco/boilerplate-manifest': '/data/vrn-deco/boilerplate/manifest' })
})
})

describe('@vrn-deco/cli-npm-helper -> utils.ts -> isDistTagVersion', () => {
test('can verify a version is a dist tag', () => {
expect(isDistTagVersion('latest')).toBe(true)
expect(isDistTagVersion('next')).toBe(true)
expect(isDistTagVersion('beteee')).toBe(false)
expect(isDistTagVersion('v1.0.0')).toBe(false)
})
})
4 changes: 1 addition & 3 deletions packages/npm-helper/src/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
* @Description: package install
*/
import { execa } from 'execa'
import { NPMRegistry, PackageManager } from '@vrn-deco/cli-shared'

import { cmdExists } from './utils.js'
import { cmdExists, NPMRegistry, PackageManager } from '@vrn-deco/cli-shared'

export type InstallOptions = {
name: string
Expand Down
18 changes: 0 additions & 18 deletions packages/npm-helper/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
* @Description: utils
*/
import path from 'node:path'
import os from 'node:os'
import { execSync } from 'node:child_process'
import fs from 'fs-extra'

import { isObject } from '@vrn-deco/cli-shared'
Expand Down Expand Up @@ -38,22 +36,6 @@ export function parseModuleMap(): Record<string, string> {
return result
}

/**
* check if command is exists
*/
export function cmdExists(cmd: string) {
try {
execSync(
os.platform() === 'win32'
? `cmd /c "(help ${cmd} > nul || exit 0) && where ${cmd} > nul 2> nul"`
: `command -v ${cmd}`,
)
return true
} catch {
return false
}
}

/**
* check if a version is dist tag
*/
Expand Down
13 changes: 12 additions & 1 deletion packages/shared/src/__tests__/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { isFunction, noop } from '../index.js'
import os from 'node:os'
import { jest } from '@jest/globals'

import { isFunction, noop, cmdExists } from '../index.js'

describe('@vrn-deco/cli-shared -> index.ts', () => {
it('noop is a function', () => {
Expand All @@ -8,4 +11,12 @@ describe('@vrn-deco/cli-shared -> index.ts', () => {
it('noop has no return value', () => {
expect(noop()).toBeUndefined()
})

it('can verify a command exists', () => {
expect(cmdExists('npm')).toBe(true)
expect(cmdExists('nnnnpm')).toBe(false)
const spy = jest.spyOn(os, 'platform').mockImplementation(() => 'win32')
expect(cmdExists('nnnnpm')).toBe(false)
spy.mockRestore()
})
})
22 changes: 20 additions & 2 deletions packages/shared/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,27 @@
* @Date: 2021-06-26 01:20:21
* @Description: shared utils
*/
import os from 'node:os'
import { execSync } from 'node:child_process'

export * from './type-helper.js'
export * from './enum.js'

// eslint-disable-next-line @typescript-eslint/no-empty-function
export const noop = (): void => {}
export const noop = (): void => void 0

/**
* check if command is exists
*/
export function cmdExists(cmd: string) {
try {
execSync(
os.platform() === 'win32'
? `cmd /c "(help ${cmd} > nul || exit 0) && where ${cmd} > nul 2> nul"`
: `command -v ${cmd}`,
{ stdio: 'ignore' },
)
return true
} catch {
return false
}
}

0 comments on commit a28cac6

Please sign in to comment.