/
hooks.ts
124 lines (104 loc) · 3.98 KB
/
hooks.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import { LOCKFILE_VERSION } from '@pnpm/constants'
import { Lockfile } from '@pnpm/lockfile-file'
import { prepareEmpty } from '@pnpm/prepare'
import { addDistTag } from '@pnpm/registry-mock'
import {
addDependenciesToPackage,
PackageManifest,
} from '@pnpm/core'
import { testDefaults } from '../utils'
test('readPackage, afterAllResolved hooks', async () => {
const project = prepareEmpty()
// w/o the hook, 100.1.0 would be installed
await addDistTag({ package: '@pnpm.e2e/dep-of-pkg-with-1-dep', version: '100.1.0', distTag: 'latest' })
function readPackageHook (manifest: PackageManifest) {
switch (manifest.name) {
case '@pnpm.e2e/pkg-with-1-dep':
if (manifest.dependencies == null) {
throw new Error('@pnpm.e2e/pkg-with-1-dep expected to have a dependencies field')
}
manifest.dependencies['@pnpm.e2e/dep-of-pkg-with-1-dep'] = '100.0.0'
break
}
return manifest
}
const afterAllResolved = jest.fn((lockfile: Lockfile) => {
lockfile['foo'] = 'foo'
return lockfile
})
await addDependenciesToPackage({}, ['@pnpm.e2e/pkg-with-1-dep'], await testDefaults({
hooks: {
afterAllResolved: [afterAllResolved],
readPackage: [readPackageHook],
},
}))
await project.storeHas('@pnpm.e2e/dep-of-pkg-with-1-dep', '100.0.0')
expect(afterAllResolved).toHaveBeenCalledTimes(1)
expect(afterAllResolved.mock.calls[0][0].lockfileVersion).toEqual(LOCKFILE_VERSION)
const wantedLockfile = await project.readLockfile()
expect(wantedLockfile['foo']).toEqual('foo')
})
test('readPackage, afterAllResolved async hooks', async () => {
const project = prepareEmpty()
// w/o the hook, 100.1.0 would be installed
await addDistTag({ package: '@pnpm.e2e/dep-of-pkg-with-1-dep', version: '100.1.0', distTag: 'latest' })
async function readPackageHook (manifest: PackageManifest) {
switch (manifest.name) {
case '@pnpm.e2e/pkg-with-1-dep':
if (manifest.dependencies == null) {
throw new Error('@pnpm.e2e/pkg-with-1-dep expected to have a dependencies field')
}
manifest.dependencies['@pnpm.e2e/dep-of-pkg-with-1-dep'] = '100.0.0'
break
}
return manifest
}
const afterAllResolved = jest.fn(async (lockfile: Lockfile) => {
lockfile['foo'] = 'foo'
return lockfile
})
await addDependenciesToPackage({}, ['@pnpm.e2e/pkg-with-1-dep'], await testDefaults({
hooks: {
afterAllResolved: [afterAllResolved],
readPackage: [readPackageHook],
},
}))
await project.storeHas('@pnpm.e2e/dep-of-pkg-with-1-dep', '100.0.0')
expect(afterAllResolved).toHaveBeenCalledTimes(1)
expect(afterAllResolved.mock.calls[0][0].lockfileVersion).toEqual(LOCKFILE_VERSION)
const wantedLockfile = await project.readLockfile()
expect(wantedLockfile['foo']).toEqual('foo')
})
test('readPackage hooks array', async () => {
const project = prepareEmpty()
// w/o the hook, 100.1.0 would be installed
await addDistTag({ package: '@pnpm.e2e/dep-of-pkg-with-1-dep', version: '100.1.0', distTag: 'latest' })
function readPackageHook1 (manifest: PackageManifest) {
switch (manifest.name) {
case '@pnpm.e2e/pkg-with-1-dep':
if (manifest.dependencies == null) {
throw new Error('@pnpm.e2e/pkg-with-1-dep expected to have a dependencies field')
}
manifest.dependencies['@pnpm.e2e/dep-of-pkg-with-1-dep'] = '50.0.0'
break
}
return manifest
}
function readPackageHook2 (manifest: PackageManifest) {
switch (manifest.name) {
case '@pnpm.e2e/pkg-with-1-dep':
if (manifest.dependencies == null) {
throw new Error('@pnpm.e2e/pkg-with-1-dep expected to have a dependencies field')
}
manifest.dependencies['@pnpm.e2e/dep-of-pkg-with-1-dep'] = '100.0.0'
break
}
return manifest
}
await addDependenciesToPackage({}, ['@pnpm.e2e/pkg-with-1-dep'], await testDefaults({
hooks: {
readPackage: [readPackageHook1, readPackageHook2],
},
}))
await project.storeHas('@pnpm.e2e/dep-of-pkg-with-1-dep', '100.0.0')
})