Skip to content

Commit

Permalink
Merge pull request probot#89 from probot/jest
Browse files Browse the repository at this point in the history
Migrate to jest
  • Loading branch information
bkeepers authored Feb 11, 2018
2 parents d5d140b + d560599 commit 0cf2578
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 31 deletions.
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"repository": "https://github.com/probot/stale",
"scripts": {
"start": "probot run ./index.js",
"test": "mocha && standard"
"test": "jest && standard"
},
"dependencies": {
"probot": "^3.0.0",
Expand All @@ -19,9 +19,8 @@
"npm": "^4.0"
},
"devDependencies": {
"expect": "^1.20.2",
"localtunnel": "^1.8.3",
"mocha": "^3.5.0"
"jest": "^22.2.2",
"localtunnel": "^1.8.3"
},
"standard": {
"env": [
Expand Down
64 changes: 37 additions & 27 deletions test/stale.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const expect = require('expect')
process.env.LOG_LEVEL = 'fatal'

const {createRobot} = require('probot')
const Stale = require('../lib/stale')
const notFoundError = {
Expand All @@ -14,17 +15,17 @@ describe('stale', () => {
beforeEach(() => {
robot = createRobot()

const issueAction = expect.createSpy().andReturn(Promise.resolve(notFoundError))
const issueAction = jest.fn().mockImplementation(() => Promise.resolve(notFoundError))

// Mock out the GitHub API
github = {
integrations: {
getInstallations: expect.createSpy()
getInstallations: jest.fn()
},
paginate: expect.createSpy(),
paginate: jest.fn(),
issues: {
removeLabel: issueAction,
getLabel: expect.createSpy().andReturn(Promise.reject(notFoundError)),
getLabel: jest.fn().mockImplementation(() => Promise.reject(notFoundError)),
createLabel: issueAction,
addLabels: issueAction,
createComment: issueAction,
Expand All @@ -39,19 +40,22 @@ describe('stale', () => {
robot.auth = () => Promise.resolve(github)
})

it('removes the stale label and ignores if it has already been removed', async () => {
let stale = new Stale(github, {perform: true, owner: 'probot', repo: 'stale'})
test(
'removes the stale label and ignores if it has already been removed',
async () => {
let stale = new Stale(github, {perform: true, owner: 'probot', repo: 'stale', logger: robot.log})

for (const type of ['pulls', 'issues']) {
try {
await stale.unmark(type, {number: 123})
} catch (_) {
throw new Error('Should not have thrown an error')
for (const type of ['pulls', 'issues']) {
try {
await stale.unmark(type, {number: 123})
} catch (_) {
throw new Error('Should not have thrown an error')
}
}
}
})
)

it('should limit the number of actions it takes each run', async () => {
test('should limit the number of actions it takes each run', async () => {
const staleLabel = 'stale'
const limitPerRun = 30

Expand Down Expand Up @@ -86,7 +90,7 @@ describe('stale', () => {
items = items.filter(item => item.labels.map(label => label.name).includes(staleLabel))
}

expect(items.length).toBeLessThanOrEqualTo(per_page)
expect(items.length).toBeLessThanOrEqual(per_page)

return Promise.resolve({
data: {
Expand All @@ -99,7 +103,10 @@ describe('stale', () => {
let comments = 0
let closed = 0
let labeledStale = 0
github.issues.createComment = expect.createSpy().andCall(() => comments++).andReturn(Promise.resolve(notFoundError))
github.issues.createComment = jest.fn().mockImplementation(() => {
comments++
return Promise.resolve(notFoundError)
})
github.issues.edit = ({owner, repo, number, state}) => {
if (state === 'closed') {
closed++
Expand All @@ -114,7 +121,7 @@ describe('stale', () => {
// Mock out GitHub client
robot.auth = () => Promise.resolve(github)

const stale = new Stale(github, {perform: true, owner: 'probot', repo: 'stale'})
const stale = new Stale(github, {perform: true, owner: 'probot', repo: 'stale', logger: robot.log})
stale.config.limitPerRun = limitPerRun
stale.config.staleLabel = staleLabel
stale.config.closeComment = 'closed'
Expand All @@ -127,16 +134,19 @@ describe('stale', () => {
}
})

it('should not close issues if daysUntilClose is configured as false', async () => {
let stale = new Stale(github, {perform: true, owner: 'probot', repo: 'stale'})
stale.config.daysUntilClose = false
stale.getStale = expect.createSpy().andReturn(Promise.resolve({data: {items: []}}))
stale.getClosable = expect.createSpy()
test(
'should not close issues if daysUntilClose is configured as false',
async () => {
let stale = new Stale(github, {perform: true, owner: 'probot', repo: 'stale', logger: robot.log})
stale.config.daysUntilClose = false
stale.getStale = jest.fn().mockImplementation(() => Promise.resolve({data: {items: []}}))
stale.getClosable = jest.fn()

await stale.markAndSweep('issues')
expect(stale.getClosable).toNotHaveBeenCalled()
await stale.markAndSweep('issues')
expect(stale.getClosable).not.toHaveBeenCalled()

await stale.markAndSweep('pulls')
expect(stale.getClosable).toNotHaveBeenCalled()
})
await stale.markAndSweep('pulls')
expect(stale.getClosable).not.toHaveBeenCalled()
}
)
})

0 comments on commit 0cf2578

Please sign in to comment.