|
2 | 2 | // But this added complexity greatly speeds up the other npm-related tests by removing the |
3 | 3 | // dependency on actual npm CLI calls and a fake registry (which are very slow). |
4 | 4 |
|
5 | | -import { afterEach, beforeAll, describe, expect, it, jest } from '@jest/globals'; |
| 5 | +import { afterAll, afterEach, beforeAll, describe, expect, it, jest } from '@jest/globals'; |
6 | 6 | import fs from 'fs'; |
7 | | -// import fetch from 'npm-registry-fetch'; |
| 7 | +import fetch from 'npm-registry-fetch'; |
8 | 8 | import { type NpmResult, npm } from '../packageManager/npm'; |
9 | 9 | import type { PackageJson } from '../types/PackageInfo'; |
10 | | -import { |
11 | | - initNpmMock, |
12 | | - _makeRegistryData, |
13 | | - _mockNpmPack, |
14 | | - _mockNpmPublish, |
15 | | - _mockNpmShow, |
16 | | - type MockNpmResult, |
17 | | -} from './mockNpm'; |
| 10 | +import { initNpmMock, _makeRegistryData, _mockNpmPack, _mockNpmPublish, type MockNpmResult } from './mockNpm'; |
18 | 11 | import * as readJsonModule from '../object/readJson'; |
19 | 12 |
|
20 | 13 | jest.mock('fs'); |
21 | | -// jest.mock('npm-registry-fetch'); |
| 14 | +jest.mock('npm-registry-fetch'); |
22 | 15 | jest.mock('../object/readJson'); |
23 | 16 | jest.mock('../packageManager/npm'); |
24 | 17 |
|
@@ -93,94 +86,6 @@ describe('_makeRegistryData', () => { |
93 | 86 | }); |
94 | 87 | }); |
95 | 88 |
|
96 | | -describe('_mockNpmShow', () => { |
97 | | - function getErrorResult(errorMessage: string) { |
98 | | - return { |
99 | | - stdout: '', |
100 | | - stderr: errorMessage, |
101 | | - all: errorMessage, |
102 | | - success: false, |
103 | | - failed: true, |
104 | | - } as NpmResult; |
105 | | - } |
106 | | - |
107 | | - function getShowResult(params: { name: string; version: string }) { |
108 | | - const { name, version } = params; |
109 | | - const output = JSON.stringify({ |
110 | | - ...data[name].versions[version], |
111 | | - 'dist-tags': data[name]['dist-tags'], |
112 | | - versions: Object.keys(data[name].versions), |
113 | | - }); |
114 | | - |
115 | | - return { |
116 | | - stdout: output, |
117 | | - stderr: '', |
118 | | - all: output, |
119 | | - success: true, |
120 | | - failed: false, |
121 | | - } as NpmResult; |
122 | | - } |
123 | | - |
124 | | - const data = _makeRegistryData({ |
125 | | - foo: { |
126 | | - versions: ['1.0.0-beta', '1.0.0', '1.0.1'], |
127 | | - 'dist-tags': { latest: '1.0.1', beta: '1.0.0-beta' }, |
128 | | - }, |
129 | | - '@foo/bar': { |
130 | | - versions: ['2.0.0-beta', '2.0.0', '2.0.1'], |
131 | | - 'dist-tags': { latest: '2.0.1', beta: '2.0.0-beta' }, |
132 | | - }, |
133 | | - }); |
134 | | - |
135 | | - it("errors if package doesn't exist", async () => { |
136 | | - const emptyData = _makeRegistryData({}); |
137 | | - const result = await _mockNpmShow(emptyData, ['foo'], { cwd: undefined }); |
138 | | - expect(result).toEqual(getErrorResult('[fake] code E404 - foo - not found')); |
139 | | - }); |
140 | | - |
141 | | - it('returns requested version plus dist-tags and version list', async () => { |
142 | | - const result = await _mockNpmShow(data, ['foo@1.0.0'], { cwd: undefined }); |
143 | | - expect(result).toEqual(getShowResult({ name: 'foo', version: '1.0.0' })); |
144 | | - }); |
145 | | - |
146 | | - it('returns requested version of scoped package', async () => { |
147 | | - const result = await _mockNpmShow(data, ['@foo/bar@2.0.0'], { cwd: undefined }); |
148 | | - expect(result).toEqual(getShowResult({ name: '@foo/bar', version: '2.0.0' })); |
149 | | - }); |
150 | | - |
151 | | - it('returns requested tag', async () => { |
152 | | - const result = await _mockNpmShow(data, ['foo@beta'], { cwd: undefined }); |
153 | | - expect(result).toEqual(getShowResult({ name: 'foo', version: '1.0.0-beta' })); |
154 | | - }); |
155 | | - |
156 | | - it('returns requested tag of scoped package', async () => { |
157 | | - const result = await _mockNpmShow(data, ['@foo/bar@beta'], { cwd: undefined }); |
158 | | - expect(result).toEqual(getShowResult({ name: '@foo/bar', version: '2.0.0-beta' })); |
159 | | - }); |
160 | | - |
161 | | - it('returns latest version if no version requested', async () => { |
162 | | - const result = await _mockNpmShow(data, ['foo'], { cwd: undefined }); |
163 | | - expect(result).toEqual(getShowResult({ name: 'foo', version: '1.0.1' })); |
164 | | - }); |
165 | | - |
166 | | - it('returns latest version of scoped package if no version requested', async () => { |
167 | | - const result = await _mockNpmShow(data, ['@foo/bar'], { cwd: undefined }); |
168 | | - expect(result).toEqual(getShowResult({ name: '@foo/bar', version: '2.0.1' })); |
169 | | - }); |
170 | | - |
171 | | - it("errors if requested version doesn't exist", async () => { |
172 | | - const result = await _mockNpmShow(data, ['foo@2.0.0'], { cwd: undefined }); |
173 | | - expect(result).toEqual(getErrorResult('[fake] code E404 - foo@2.0.0 - not found')); |
174 | | - }); |
175 | | - |
176 | | - // support for this could be added later |
177 | | - it('currently throws if requested version is a range', async () => { |
178 | | - await expect(() => _mockNpmShow(data, ['foo@^1.0.0'], { cwd: undefined })).rejects.toThrow( |
179 | | - /not currently supported/ |
180 | | - ); |
181 | | - }); |
182 | | -}); |
183 | | - |
184 | 89 | describe('_mockNpmPublish', () => { |
185 | 90 | function getPublishResult(params: { error?: string; tag?: string }) { |
186 | 91 | const { error, tag } = params; |
@@ -362,25 +267,29 @@ describe('mockNpm', () => { |
362 | 267 | packageJson = undefined; |
363 | 268 | }); |
364 | 269 |
|
365 | | - // describe('mockFetchJson', () => { |
366 | | - // it('mocks registry fetch', async () => { |
367 | | - // npmMock.setRegistryData({ foo: { versions: ['1.0.0'] } }); |
368 | | - // expect(fetch.json).toHaveProperty('mock'); |
369 | | - // const result = await fetch.json('/foo'); |
370 | | - // expect(result).toEqual({ |
371 | | - // name: 'foo', |
372 | | - // modified: expect.any(String), |
373 | | - // versions: { '1.0.0': { name: 'foo', version: '1.0.0' } }, |
374 | | - // 'dist-tags': { latest: '1.0.0' }, |
375 | | - // }); |
376 | | - // }); |
377 | | - |
378 | | - // it('resets calls and registry after each test', () => { |
379 | | - // expect(npmMock.mockFetchJson).not.toHaveBeenCalled(); |
380 | | - // // registry data for foo was set in the previous test but should have been cleared |
381 | | - // expect(() => fetch.json('/foo')).toThrow('404 Not Found'); |
382 | | - // }); |
383 | | - // }); |
| 270 | + afterAll(() => { |
| 271 | + jest.restoreAllMocks(); |
| 272 | + }); |
| 273 | + |
| 274 | + describe('mockFetchJson', () => { |
| 275 | + it('mocks registry fetch', async () => { |
| 276 | + npmMock.setRegistryData({ foo: { versions: ['1.0.0'] } }); |
| 277 | + expect(fetch.json).toHaveProperty('mock'); |
| 278 | + const result = await fetch.json('/foo'); |
| 279 | + expect(result).toEqual({ |
| 280 | + name: 'foo', |
| 281 | + modified: expect.any(String), |
| 282 | + versions: { '1.0.0': { name: 'foo', version: '1.0.0' } }, |
| 283 | + 'dist-tags': { latest: '1.0.0' }, |
| 284 | + }); |
| 285 | + }); |
| 286 | + |
| 287 | + it('resets calls and registry after each test', () => { |
| 288 | + expect(npmMock.mockFetchJson).not.toHaveBeenCalled(); |
| 289 | + // registry data for foo was set in the previous test but should have been cleared |
| 290 | + expect(() => fetch.json('/foo')).toThrow('404 Not Found'); |
| 291 | + }); |
| 292 | + }); |
384 | 293 |
|
385 | 294 | describe('getPublishedVersions', () => { |
386 | 295 | it('gets data for a package', () => { |
@@ -433,23 +342,14 @@ describe('mockNpm', () => { |
433 | 342 | versions: ['1.0.0'], |
434 | 343 | 'dist-tags': { latest: '1.0.0' }, |
435 | 344 | }); |
436 | | - expect(await npm(['show', 'foo'], { cwd: undefined })).toMatchObject({ |
437 | | - success: true, |
438 | | - stdout: JSON.stringify({ |
439 | | - name: 'foo', |
440 | | - version: '1.0.0', |
441 | | - 'dist-tags': { latest: '1.0.0' }, |
442 | | - versions: ['1.0.0'], |
443 | | - }), |
| 345 | + expect(await fetch.json('/foo')).toEqual({ |
| 346 | + name: 'foo', |
| 347 | + modified: expect.any(String), |
| 348 | + versions: { |
| 349 | + '1.0.0': { name: 'foo', version: '1.0.0' }, |
| 350 | + }, |
| 351 | + 'dist-tags': { latest: '1.0.0' }, |
444 | 352 | }); |
445 | | - // expect(await fetch.json('/foo')).toEqual({ |
446 | | - // name: 'foo', |
447 | | - // modified: expect.any(String), |
448 | | - // versions: { |
449 | | - // '1.0.0': { name: 'foo', version: '1.0.0' }, |
450 | | - // }, |
451 | | - // 'dist-tags': { latest: '1.0.0' }, |
452 | | - // }); |
453 | 353 | }); |
454 | 354 | }); |
455 | 355 |
|
@@ -494,15 +394,6 @@ describe('mockNpm', () => { |
494 | 394 | expect(npmMock.mock).toHaveBeenCalledTimes(1); |
495 | 395 | expect(npmMock.mock).toHaveBeenCalledWith(['foo'], expect.objectContaining({ cwd: undefined })); |
496 | 396 | }); |
497 | | - |
498 | | - it('TEMP mocks npm show command', async () => { |
499 | | - npmMock.setRegistryData({ foo: { versions: ['1.0.0'] } }); |
500 | | - const result = await npm(['show', 'foo'], { cwd: undefined }); |
501 | | - expect(result).toMatchObject({ |
502 | | - success: true, |
503 | | - stdout: expect.stringContaining('"name":"foo"'), |
504 | | - }); |
505 | | - }); |
506 | 397 | }); |
507 | 398 |
|
508 | 399 | describe('setCommandOverride', () => { |
|
0 commit comments