-
-
Notifications
You must be signed in to change notification settings - Fork 936
/
publishedBy.test.ts
77 lines (65 loc) · 2.34 KB
/
publishedBy.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import fs from 'fs'
import path from 'path'
import { createFetchFromRegistry } from '@pnpm/fetch'
import { createNpmResolver } from '@pnpm/npm-resolver'
import { fixtures } from '@pnpm/test-fixtures'
import loadJsonFile from 'load-json-file'
import nock from 'nock'
import tempy from 'tempy'
const f = fixtures(__dirname)
const registry = 'https://registry.npmjs.org/'
/* eslint-disable @typescript-eslint/no-explicit-any */
const badDatesMeta = loadJsonFile.sync<any>(f.find('bad-dates.json'))
/* eslint-enable @typescript-eslint/no-explicit-any */
const fetch = createFetchFromRegistry({})
const getAuthHeader = () => undefined
const createResolveFromNpm = createNpmResolver.bind(null, fetch, getAuthHeader)
afterEach(() => {
nock.cleanAll()
nock.disableNetConnect()
})
beforeEach(() => {
nock.enableNetConnect()
})
test('fall back to a newer version if there is no version published by the given date', async () => {
nock(registry)
.get('/bad-dates')
.reply(200, badDatesMeta)
const cacheDir = tempy.directory()
const resolve = createResolveFromNpm({
cacheDir,
filterMetadata: true,
fullMetadata: true,
})
const resolveResult = await resolve({ alias: 'bad-dates', pref: '^1.0.0' }, {
registry,
publishedBy: new Date('2015-08-17T19:26:00.508Z'),
})
expect(resolveResult!.resolvedVia).toBe('npm-registry')
expect(resolveResult!.id).toBe('registry.npmjs.org/bad-dates/1.0.0')
})
test('request metadata when the one in cache does not have a version satisfiyng the range', async () => {
const cacheDir = tempy.directory()
const cachedMeta = {
'dist-tags': {},
versions: {},
time: {},
cachedAt: '2016-08-17T19:26:00.508Z',
}
fs.mkdirSync(path.join(cacheDir, 'metadata-v1.1/registry.npmjs.org'), { recursive: true })
fs.writeFileSync(path.join(cacheDir, 'metadata-v1.1/registry.npmjs.org/bad-dates.json'), JSON.stringify(cachedMeta), 'utf8')
nock(registry)
.get('/bad-dates')
.reply(200, badDatesMeta)
const resolve = createResolveFromNpm({
cacheDir,
filterMetadata: true,
fullMetadata: true,
})
const resolveResult = await resolve({ alias: 'bad-dates', pref: '^1.0.0' }, {
registry,
publishedBy: new Date('2015-08-17T19:26:00.508Z'),
})
expect(resolveResult!.resolvedVia).toBe('npm-registry')
expect(resolveResult!.id).toBe('registry.npmjs.org/bad-dates/1.0.0')
})