Skip to content

Commit d03791a

Browse files
test: add tests and actions
1 parent 08ed0a2 commit d03791a

File tree

4 files changed

+100
-0
lines changed

4 files changed

+100
-0
lines changed

.github/workflows/test.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Test dependents
2+
3+
on:
4+
push:
5+
branches: [ master ]
6+
pull_request:
7+
branches: [ master ]
8+
9+
jobs:
10+
build:
11+
12+
runs-on: ubuntu-latest
13+
14+
strategy:
15+
matrix:
16+
node-version: [10.x, 12.x, 14.x]
17+
18+
steps:
19+
- uses: actions/checkout@v2
20+
- name: Use Node.js ${{ matrix.node-version }}
21+
uses: actions/setup-node@v1
22+
with:
23+
node-version: ${{ matrix.node-version }}
24+
- run: npm ci
25+
- run: npm test
26+
env:
27+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
node_modules
22
package-lock.json
33
.env
4+
5+
# Coverage directory used by tools like istanbul
6+
coverage
7+
*.lcov
8+
9+
# nyc test coverage
10+
.nyc_output

test/fixtures/config.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = {
2+
args: {
3+
package: 'express',
4+
number: 20,
5+
sort: 'forks',
6+
total: 20
7+
}
8+
}

test/index.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
require('dotenv').config()
2+
const assert = require('assert')
3+
const { suite, test } = require('mocha')
4+
const getDependents = require('../lib/getDependents')
5+
const pkg = require('../package.json')
6+
const config = require('./fixtures/config')
7+
8+
assert(process.env.GITHUB_TOKEN, `
9+
Tests require you to have a GitHub personal token called GITHUB_TOKEN
10+
For more information about GitHub tokens
11+
https://help.github.com/en/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line
12+
`)
13+
14+
suite(pkg.name, async function () {
15+
this.timeout(0)
16+
test('Dependents object created', async function () {
17+
const deps = await getDependents(config.args)
18+
for (const i in deps) {
19+
for (const key in deps[i]) {
20+
const subObj = deps[i][key]
21+
assert.strictEqual(Object.prototype.hasOwnProperty.call(subObj, 'downloads'), true)
22+
assert.strictEqual(Object.prototype.hasOwnProperty.call(subObj, 'forks'), true)
23+
assert.strictEqual(Object.prototype.hasOwnProperty.call(subObj, 'stars'), true)
24+
assert.strictEqual(Object.prototype.hasOwnProperty.call(subObj, 'watchers'), true)
25+
assert.strictEqual(Object.prototype.hasOwnProperty.call(subObj, 'url'), true)
26+
}
27+
}
28+
})
29+
test('GitHub attributes zero when URL is undefined', async function () {
30+
const deps = await getDependents(config.args)
31+
for (const i in deps) {
32+
for (const key in deps[i]) {
33+
const subObj = deps[i][key]
34+
if (subObj.url === 'undefined') {
35+
assert.strictEqual(subObj.forks, 0)
36+
assert.strictEqual(subObj.stars, 0)
37+
assert.strictEqual(subObj.watchers, 0)
38+
}
39+
}
40+
}
41+
})
42+
test('Dependents sorted correctly', async function () {
43+
const deps = await getDependents(config.args)
44+
let key, previousKey
45+
for (const i in deps) {
46+
for (const k in deps[i]) {
47+
previousKey = key
48+
key = k
49+
}
50+
if (i !== '0') {
51+
const j = String(parseInt(i) - 1)
52+
const previous = deps[j][previousKey][config.args.sort]
53+
const current = deps[i][key][config.args.sort]
54+
assert.strictEqual(previous >= current, true)
55+
}
56+
}
57+
})
58+
})

0 commit comments

Comments
 (0)