|
1 | 1 | import fs from 'node:fs';
|
2 | 2 | import path from 'node:path';
|
3 | 3 | import { afterAll, beforeAll, describe, expect, it } from 'vitest';
|
4 |
| -import { execCommand, isDirectory, isFile, normalizePath } from '../src/utils'; |
| 4 | +import { checkPkgVersion, execCommand, isDirectory, isFile, normalizePath } from '../src/utils'; |
5 | 5 | import { testRoot } from './helpers';
|
6 | 6 |
|
7 | 7 | let tempDir: string;
|
@@ -100,3 +100,37 @@ describe('execCommand', () => {
|
100 | 100 | expect(result.stdout.trim()).toBe('test-value');
|
101 | 101 | });
|
102 | 102 | });
|
| 103 | + |
| 104 | +describe('checkPkgVersion', () => { |
| 105 | + it('should return package version from npm registry', async () => { |
| 106 | + const mockResponse = { version: '1.2.3' }; |
| 107 | + const mockFetch = vi.fn().mockResolvedValue({ |
| 108 | + json: () => Promise.resolve(mockResponse), |
| 109 | + }); |
| 110 | + vi.stubGlobal('fetch', mockFetch); |
| 111 | + |
| 112 | + const version = await checkPkgVersion({ |
| 113 | + name: 'test-package', |
| 114 | + distTag: 'xxx', |
| 115 | + registry: 'https://registry.npmjs.org', |
| 116 | + }); |
| 117 | + |
| 118 | + expect(version).toBe('1.2.3'); |
| 119 | + expect(mockFetch).toHaveBeenCalledWith('https://registry.npmjs.org/test-package/xxx?t=1234567890'); |
| 120 | + }); |
| 121 | + |
| 122 | + it('should use default distTag and registry when not provided', async () => { |
| 123 | + const mockResponse = { version: '1.0.0' }; |
| 124 | + const mockFetch = vi.fn().mockResolvedValue({ |
| 125 | + json: () => Promise.resolve(mockResponse), |
| 126 | + }); |
| 127 | + vi.stubGlobal('fetch', mockFetch); |
| 128 | + |
| 129 | + const version = await checkPkgVersion({ |
| 130 | + name: 'test-package', |
| 131 | + }); |
| 132 | + |
| 133 | + expect(version).toBe('1.0.0'); |
| 134 | + expect(mockFetch).toHaveBeenCalledWith('https://registry.npmjs.org/test-package/latest?t=1234567890'); |
| 135 | + }); |
| 136 | +}); |
0 commit comments