Skip to content

Commit 8c8f491

Browse files
committed
chore(test): node-repo replace sinon with nock
1 parent ed7986d commit 8c8f491

1 file changed

Lines changed: 76 additions & 59 deletions

File tree

test/unit/node-repo.test.js

Lines changed: 76 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,118 +1,135 @@
11
'use strict'
22

3-
const proxyquire = require('proxyquire')
4-
const sinon = require('sinon')
53
const tap = require('tap')
64
const lolex = require('lolex')
5+
const nock = require('nock')
6+
7+
const nodeRepo = require('../../lib/node-repo')
78

89
const logger = require('../../lib/logger')
9-
const githubClient = require('../../lib/github-client')
1010
const readFixture = require('../read-fixture')
11+
const { ignoreQueryParams } = require('../common')
1112

1213
tap.test('fetchExistingLabels(): caches existing repository labels', async (t) => {
13-
sinon.stub(githubClient.issues, 'listLabelsForRepo', () => Promise.resolve([]))
14-
sinon.stub(githubClient, 'hasNextPage', () => false)
15-
const nodeRepo = proxyquire('../../lib/node-repo', {
16-
'./github-client': githubClient
17-
})
18-
19-
t.plan(1)
20-
t.tearDown(() => {
21-
githubClient.issues.listLabelsForRepo.restore()
22-
githubClient.hasNextPage.restore()
23-
})
24-
25-
await nodeRepo._fetchExistingLabels({ owner: 'nodejs', repo: 'node', logger })
26-
await nodeRepo._fetchExistingLabels({ owner: 'nodejs', repo: 'node', logger })
27-
t.ok(githubClient.issues.listLabelsForRepo.calledOnce)
14+
const owner = 'nodejs'
15+
const repo = 'node1'
16+
// Test passes if nock is only called once, no other checks to run
17+
const scope = nock('https://api.github.com')
18+
.filteringPath(ignoreQueryParams)
19+
.get(`/repos/${owner}/${repo}/labels`)
20+
.once() // should only be called once
21+
.reply(200, [])
22+
23+
await nodeRepo._fetchExistingLabels({ owner, repo, logger })
24+
await nodeRepo._fetchExistingLabels({ owner, repo, logger })
25+
scope.done()
2826
})
2927

3028
tap.test('fetchExistingLabels(): cache expires after one hour', async (t) => {
29+
const owner = 'nodejs'
30+
const repo = 'node2'
3131
const clock = lolex.install()
32-
sinon.stub(githubClient.issues, 'listLabelsForRepo', () => Promise.resolve([]))
33-
sinon.stub(githubClient, 'hasNextPage', () => false)
34-
const nodeRepo = proxyquire('../../lib/node-repo', {
35-
'./github-client': githubClient
36-
})
3732

38-
t.plan(1)
33+
// Test passes if nock is only called once, no other checks to run
34+
const scope = nock('https://api.github.com')
35+
.filteringPath(ignoreQueryParams)
36+
.get(`/repos/${owner}/${repo}/labels`)
37+
.twice() // should be called twice
38+
.reply(200, [])
39+
3940
t.tearDown(() => {
40-
githubClient.issues.listLabelsForRepo.restore()
41-
githubClient.hasNextPage.restore()
4241
clock.uninstall()
4342
})
4443

45-
await nodeRepo._fetchExistingLabels({ owner: 'nodejs', repo: 'node', logger })
44+
await nodeRepo._fetchExistingLabels({ owner, repo, logger })
4645

4746
// fetch labels again after 1 hour and 1 minute
4847
clock.tick(1000 * 60 * 61)
4948

50-
await nodeRepo._fetchExistingLabels({ owner: 'nodejs', repo: 'node', logger })
51-
t.equal(githubClient.issues.listLabelsForRepo.callCount, 2)
49+
await nodeRepo._fetchExistingLabels({ owner, repo, logger })
50+
scope.done()
5251
})
5352

5453
tap.test('fetchExistingLabels(): yields an array of existing label names', async (t) => {
5554
const labelsFixture = readFixture('repo-labels.json')
56-
sinon.stub(githubClient.issues, 'listLabelsForRepo', () => Promise.resolve(labelsFixture))
57-
sinon.stub(githubClient, 'hasNextPage', () => false)
58-
const nodeRepo = proxyquire('../../lib/node-repo', {
59-
'./github-client': githubClient
60-
})
55+
const owner = 'nodejs'
56+
const repo = 'node3'
57+
58+
const scope = nock('https://api.github.com')
59+
.filteringPath(ignoreQueryParams)
60+
.get(`/repos/${owner}/${repo}/labels`)
61+
.reply(200, labelsFixture.data)
6162

6263
t.plan(1)
63-
t.tearDown(() => {
64-
githubClient.issues.listLabelsForRepo.restore()
65-
githubClient.hasNextPage.restore()
66-
})
6764

68-
const existingLabels = await nodeRepo._fetchExistingLabels({ owner: 'nodejs', repo: 'node', logger })
65+
const existingLabels = await nodeRepo._fetchExistingLabels({ owner, repo, logger })
6966
t.ok(existingLabels.includes('cluster'))
67+
scope.done()
7068
})
7169

7270
tap.test('fetchExistingLabels(): can retrieve more than 100 labels', async (t) => {
7371
const labelsFixturePage1 = readFixture('repo-labels.json')
7472
const labelsFixturePage2 = readFixture('repo-labels-page-2.json')
75-
sinon.stub(githubClient.issues, 'listLabelsForRepo', (options) => Promise.resolve(options.page === 1 ? labelsFixturePage1 : labelsFixturePage2))
76-
sinon.stub(githubClient, 'hasNextPage', (listing) => listing === labelsFixturePage1)
77-
const nodeRepo = proxyquire('../../lib/node-repo', { './github-client': githubClient })
73+
const owner = 'nodejs'
74+
const repo = 'node4'
75+
const headers = {
76+
'Link': `<https://api.github.com/repos/${owner}/${repo}/labels?page=2>; rel="next"`
77+
}
78+
79+
const firstPageScope = nock('https://api.github.com')
80+
.filteringPath(ignoreQueryParams)
81+
.get(`/repos/${owner}/${repo}/labels`)
82+
.reply(200, labelsFixturePage1.data, headers)
83+
84+
const secondPageScope = nock('https://api.github.com')
85+
.get(`/repos/${owner}/${repo}/labels`)
86+
.query({ page: 2, per_page: 100, access_token: 'invalid-placeholder-token' })
87+
.reply(200, labelsFixturePage2.data)
7888

7989
t.plan(2)
80-
t.tearDown(() => {
81-
githubClient.issues.listLabelsForRepo.restore()
82-
githubClient.hasNextPage.restore()
83-
})
8490

85-
const existingLabels = await nodeRepo._fetchExistingLabels({ owner: 'nodejs', repo: 'node', logger })
91+
const existingLabels = await nodeRepo._fetchExistingLabels({ owner, repo, logger })
8692
t.ok(existingLabels.includes('cluster'))
8793
t.ok(existingLabels.includes('windows'))
94+
firstPageScope.done()
95+
secondPageScope.done()
8896
})
8997

9098
tap.test('getBotPrLabels(): returns labels added by nodejs-github-bot', (t) => {
9199
const events = readFixture('pull-request-events.json')
92-
sinon.stub(githubClient.issues, 'getEvents', (options, cb) => { cb(null, events) })
93-
const nodeRepo = proxyquire('../../lib/node-repo', { './github-client': githubClient })
100+
101+
const owner = 'nodejs'
102+
const repo = 'node5'
103+
const prId = '1'
104+
105+
const scope = nock('https://api.github.com')
106+
.filteringPath(ignoreQueryParams)
107+
.get(`/repos/${owner}/${repo}/issues/${prId}/events`)
108+
.reply(200, events.data)
94109

95110
t.plan(1)
96-
t.tearDown(() => {
97-
githubClient.issues.getEvents.restore()
98-
})
99111

100-
nodeRepo.getBotPrLabels({ owner: 'nodejs', repo: 'node', prId: '1' }, (_, labels) => {
112+
nodeRepo.getBotPrLabels({ owner, repo, prId }, (_, labels) => {
101113
t.same(labels, ['testlabel'])
114+
scope.done()
102115
})
103116
})
104117

105118
tap.test('getBotPrLabels(): returns net labels added/removed by nodejs-github-bot', (t) => {
106119
const events = readFixture('pull-request-events-2.json')
107-
sinon.stub(githubClient.issues, 'getEvents', (options, cb) => { cb(null, events) })
108-
const nodeRepo = proxyquire('../../lib/node-repo', { './github-client': githubClient })
109120

121+
const owner = 'nodejs'
122+
const repo = 'node6'
123+
const prId = '2'
124+
125+
const scope = nock('https://api.github.com')
126+
.filteringPath(ignoreQueryParams)
127+
.get(`/repos/${owner}/${repo}/issues/${prId}/events`)
128+
.reply(200, events.data)
110129
t.plan(1)
111-
t.tearDown(() => {
112-
githubClient.issues.getEvents.restore()
113-
})
114130

115-
nodeRepo.getBotPrLabels({ owner: 'nodejs', repo: 'node', prId: '1' }, (_, labels) => {
131+
nodeRepo.getBotPrLabels({ owner, repo, prId }, (_, labels) => {
116132
t.same(labels, [])
133+
scope.done()
117134
})
118135
})

0 commit comments

Comments
 (0)