-
Notifications
You must be signed in to change notification settings - Fork 190
Expand file tree
/
Copy pathpublish-npm.spec.ts
More file actions
91 lines (77 loc) · 2.89 KB
/
Copy pathpublish-npm.spec.ts
File metadata and controls
91 lines (77 loc) · 2.89 KB
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
import assert from 'node:assert/strict'
import path from 'node:path'
import { before, beforeEach, describe, it, mock } from 'node:test'
import { mockCommandImplementation, mockModule } from './lib/testHelpers.ts'
interface CommandChain {
withEnvironment: (env: Record<string, string>) => CommandChain
withLogs: () => CommandChain
run: () => string | undefined
}
describe('publish-npm', () => {
const commandMock = mock.fn<(template: TemplateStringsArray, ...values: any[]) => CommandChain>()
const getNpmTokenMock = mock.fn<() => string>()
let main: (args?: string[]) => void
let publishError: Error
before(async () => {
await mockModule(path.resolve(import.meta.dirname, '../lib/command.ts'), { command: commandMock })
await mockModule(path.resolve(import.meta.dirname, '../lib/secrets.ts'), { getNpmToken: getNpmTokenMock })
const publishNpmModule: { main: (args?: string[]) => void } = await import('./publish-npm.ts')
;({ main } = publishNpmModule)
})
beforeEach(() => {
publishError = new Error('publish failed')
getNpmTokenMock.mock.mockImplementation(() => 'fake-token')
const baseCommandMock = mock.fn<(template: TemplateStringsArray, ...values: any[]) => CommandChain>()
mockCommandImplementation(baseCommandMock)
commandMock.mock.mockImplementation((template: TemplateStringsArray, ...values: any[]): CommandChain => {
const chain = baseCommandMock(template, ...values)
if (isNpmPublishCommand(template, ...values)) {
const throwingChain: CommandChain = {
withEnvironment: () => throwingChain,
withLogs: () => throwingChain,
run: () => {
throw publishError
},
}
return {
...chain,
withEnvironment: () => throwingChain,
withLogs: () => throwingChain,
run: throwingChain.run,
}
}
return chain
})
})
it('should suggest renewing npm token when npm publish fails', () => {
assert.throws(
() => main([]),
(error: Error) => {
assert.match(error.message, /scripts\/release\/renew-token\.ts/)
assert.equal(error.cause, publishError)
return true
}
)
})
it('should not mask npm token lookup failures as publish failures', () => {
const tokenError = new Error('failed to fetch npm token')
getNpmTokenMock.mock.mockImplementation(() => {
throw tokenError
})
assert.throws(
() => main([]),
(error: Error) => {
assert.equal(error, tokenError)
assert.doesNotMatch(error.message, /scripts\/release\/renew-token\.ts/)
return true
}
)
})
})
function isNpmPublishCommand(template: TemplateStringsArray, ...values: any[]): boolean {
const command = template
.reduce((acc, part, index) => `${acc}${part}${values[index] || ''}`, '')
.replace(/\s+/g, ' ')
.trim()
return command.includes('npm publish')
}