Skip to content

Commit

Permalink
test(config-helper): some test case
Browse files Browse the repository at this point in the history
  • Loading branch information
Cphayim committed Feb 25, 2022
1 parent d4731ce commit cb7d841
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"@changesets/cli": "^2.19.0",
"@commitlint/cli": "^12.1.4",
"@commitlint/config-conventional": "^12.1.4",
"@jest/globals": "^27.5.1",
"@ombro/logger": "^1.5.2",
"@ombro/ts-config": "^1.3.0",
"@swc/core": "^1.2.145",
Expand Down
56 changes: 56 additions & 0 deletions packages/config-helper/src/__tests__/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import os from 'node:os'
import path from 'node:path'
import fs from 'fs-extra'
import { jest } from '@jest/globals'

import { PackageManager } from '@vrn-deco/cli-shared'
import { readConfig, updateConfig, writeConfig } from '../index.js'

const TEST_HOME_PATH = path.join(os.homedir(), '.vrn-deco.test')

afterAll(() => {
fs.pathExistsSync(TEST_HOME_PATH) && fs.removeSync(TEST_HOME_PATH)
})

describe('@vrn-deco/cli-config-helper -> index.ts', () => {
beforeAll(() => {
//
})

afterEach(() => {
jest.resetAllMocks()
})

afterAll(() => {
jest.restoreAllMocks()
})

test('can only be used after initalizing environment variables', () => {
expect(() => readConfig()).toThrow('be after environment variables initialization')
})

test('can read config', () => {
process.env.VRN_CLI_HOME_PATH = TEST_HOME_PATH
const config = readConfig()
expect(config).toBeDefined()
expect(config.npmRegistry).toMatch(/^http/)
expect(config.packageManager).toBe(PackageManager.NPM)
expect(config.checkUpdateEnabled).toBe(true)
})

test('can write config', () => {
process.env.VRN_CLI_HOME_PATH = TEST_HOME_PATH
const config = readConfig()
config.npmRegistry = 'https://registry.npm.cphayim.me'
config.packageManager = PackageManager.Yarn
config.checkUpdateEnabled = false
expect(() => writeConfig(config)).not.toThrow()
expect(readConfig()).toEqual(config)
})

test('can update config', () => {
process.env.VRN_CLI_HOME_PATH = TEST_HOME_PATH
expect(() => updateConfig({ packageManager: PackageManager.PNPM })).not.toThrow()
expect(readConfig().packageManager).toBe(PackageManager.PNPM)
})
})
2 changes: 2 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit cb7d841

Please sign in to comment.