basic.test.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import { $ } from 'execa'
  2. import { beforeAll, describe, expect, it } from 'vitest'
  3. import registries from '../registries.json'
  4. import { addCustomRegistry, getAllRegistries, getCurrentRegistry, getRegistriesList, removeCustomRegistry, setCurrentRegistry, store } from '../src/utils'
  5. async function nnrmCommand(...args: string[]) {
  6. return await $`node dist/index.js ${args.join(' ')}`
  7. }
  8. const customRegistry = {
  9. name: 'yyj',
  10. url: 'https://www.yunyoujun.cn',
  11. urlWithSlash: 'https://www.yunyoujun.cn/',
  12. }
  13. describe('hnrm ls', () => {
  14. beforeAll(async () => {
  15. // init
  16. store.registries = await getAllRegistries()
  17. store.pkgManager = 'npm'
  18. })
  19. const name = 'npm'
  20. it('command', async () => {
  21. const { stdout } = await nnrmCommand('ls')
  22. expect(stdout.includes(name) && stdout.includes(registries[name].registry)).toBe(true)
  23. })
  24. it('list', async () => {
  25. const { list } = await getRegistriesList()
  26. expect(list.includes(name) && list.includes(registries[name].registry)).toBe(true)
  27. })
  28. })
  29. describe('hnrm use', () => {
  30. async function useRegistry(registry: string) {
  31. await setCurrentRegistry(registry, 'npm')
  32. }
  33. // use setTimeout to wait `npm config set` `npm config get`
  34. it('use toggle', async () => {
  35. await useRegistry('taobao')
  36. setTimeout(async () => {
  37. expect(await getCurrentRegistry()).toBe('taobao')
  38. }, 50)
  39. setTimeout(async () => {
  40. await useRegistry('npm')
  41. setTimeout(async () => {
  42. expect(await getCurrentRegistry()).toBe('npm')
  43. }, 50)
  44. }, 50)
  45. })
  46. })
  47. describe('hnrm add', () => {
  48. it('add', async () => {
  49. const { name, url, urlWithSlash } = customRegistry
  50. await addCustomRegistry(name, url)
  51. store.registries = await getAllRegistries()
  52. expect(store.registries[name]).toStrictEqual({
  53. home: urlWithSlash,
  54. registry: urlWithSlash,
  55. })
  56. })
  57. it('list after add', async () => {
  58. const { name, url } = customRegistry
  59. const { inList, list } = await getRegistriesList()
  60. expect(inList).toBe(true)
  61. expect(list.includes(name) && list.includes(url)).toBe(true)
  62. })
  63. })
  64. describe('hnrm remove', () => {
  65. it('utils', async () => {
  66. expect(store.registries[customRegistry.name]).not.toBeUndefined()
  67. await removeCustomRegistry(customRegistry.name)
  68. store.registries = await getAllRegistries()
  69. expect(store.registries[customRegistry.name]).toBeUndefined()
  70. })
  71. })