forked from vrn-deco/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
155 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters