Skip to content

Commit 5eec7cb

Browse files
authored
Merge pull request #82 from github/test-add-basic-tests-for-dependency-graph-linters
test: add basic tests for dependency-graph & linters
2 parents 420cd0f + 3a25763 commit 5eec7cb

File tree

7 files changed

+210
-10
lines changed

7 files changed

+210
-10
lines changed

lib/rules/unused-export.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const {entries, imported} = require('../dependency-graph')
1+
const depGraph = require('../dependency-graph')
22

33
module.exports = {
44
meta: {
@@ -7,9 +7,9 @@ module.exports = {
77

88
create(context) {
99
const filename = context.getFilename()
10-
const {identifiers} = imported()
10+
const {identifiers} = depGraph.imported()
1111

12-
if (entries.has(filename)) {
12+
if (depGraph.entries.has(filename)) {
1313
return {}
1414
}
1515

lib/rules/unused-module.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const {entries, imported} = require('../dependency-graph')
1+
const depGraph = require('../dependency-graph')
22

33
module.exports = {
44
meta: {
@@ -8,13 +8,13 @@ module.exports = {
88
create(context) {
99
const filename = context.getFilename()
1010

11-
if (entries.has(filename)) {
11+
if (depGraph.entries.has(filename)) {
1212
return {}
1313
}
1414

1515
return {
1616
Program(node) {
17-
const {filenames} = imported()
17+
const {filenames} = depGraph.imported()
1818
if (!filenames.has(filename)) {
1919
context.report(node, 'Module was not imported by any files.')
2020
}

package-lock.json

Lines changed: 14 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
"eslint": ">=6.5.1",
6767
"flow-bin": ">=0.110.1",
6868
"graphql": ">=14.5.8",
69-
"mocha": ">=6.2.2"
69+
"mocha": ">=6.2.2",
70+
"rimraf": "^3.0.0"
7071
}
7172
}

tests/dependency-graph.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
const dependencyGraph = require('../lib/dependency-graph')
2+
const assert = require('assert')
3+
const {describe, it, before, after} = require('mocha')
4+
const rimraf = require('rimraf')
5+
const {resolve} = require('path')
6+
const {mkdirSync, writeFileSync, symlinkSync} = require('fs')
7+
describe('dependency-graph', () => {
8+
it('has entries set', () => {
9+
assert(dependencyGraph.entries instanceof Set)
10+
})
11+
it('has dependencyGraph map', () => {
12+
assert(dependencyGraph.dependencyGraph instanceof Map)
13+
})
14+
it('has imported function', () => {
15+
assert.equal(typeof dependencyGraph.imported, 'function')
16+
})
17+
describe('imported', () => {
18+
before(() => {
19+
rimraf.sync('./fixtures')
20+
mkdirSync('./fixtures')
21+
writeFileSync('./fixtures/index.js', ``)
22+
writeFileSync('./fixtures/one.js', ``)
23+
symlinkSync('./fixtures/one.js', './fixtures/link.js')
24+
const graph = {imports: new Map(), exports: new Set()}
25+
graph.imports.set(resolve('./fixtures/one.js'), new Set(['a', 'b']))
26+
dependencyGraph.dependencyGraph.set('./fixtures/index.js', graph)
27+
})
28+
after(() => {
29+
rimraf.sync('./fixtures')
30+
dependencyGraph.dependencyGraph.clear()
31+
})
32+
it('gathers all imported files from dependencyGraph', () => {
33+
const {filenames} = dependencyGraph.imported()
34+
assert.deepEqual([...filenames], [resolve('./fixtures/one.js')])
35+
})
36+
it('gathers all imported identifiers from dependencyGraph', () => {
37+
const {identifiers} = dependencyGraph.imported()
38+
assert.deepEqual([...identifiers], [resolve('./fixtures/one.js#a'), resolve('./fixtures/one.js#b')])
39+
})
40+
it('follows symlinks', () => {
41+
const graph = dependencyGraph.dependencyGraph.get('./fixtures/index.js')
42+
graph.imports.delete('./fixtures/one.js')
43+
graph.imports.set('./fixtures/link.js', new Set(['a', 'b']))
44+
const {filenames} = dependencyGraph.imported()
45+
assert.deepEqual([...filenames], [resolve('./fixtures/one.js')])
46+
})
47+
})
48+
})

tests/unused-export.js

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
const {describe, beforeEach, afterEach} = require('mocha')
2+
const depGraph = require('../lib/dependency-graph')
3+
var rule = require('../lib/rules/unused-export')
4+
var RuleTester = require('eslint').RuleTester
5+
var ruleTester = new RuleTester()
6+
7+
describe('dependency-graph', () => {
8+
const originalDepGraphImport = depGraph.imported
9+
beforeEach(() => {
10+
depGraph.imported = () => ({
11+
identifiers: new Set(['a.js#*', 'b.js#foo', 'c.js#default'])
12+
})
13+
})
14+
15+
afterEach(() => {
16+
depGraph.imported = originalDepGraphImport
17+
})
18+
19+
ruleTester.run('unused-export', rule, {
20+
valid: [
21+
{
22+
code: 'export const foo = 1',
23+
parserOptions: {ecmaVersion: 2015, sourceType: 'module'},
24+
filename: 'a.js'
25+
},
26+
{
27+
code: 'export const foo = 1',
28+
parserOptions: {ecmaVersion: 2015, sourceType: 'module'},
29+
filename: 'b.js'
30+
},
31+
{
32+
code: 'export default 1',
33+
parserOptions: {ecmaVersion: 2015, sourceType: 'module'},
34+
filename: 'c.js'
35+
}
36+
],
37+
invalid: [
38+
{
39+
code: 'export default 1',
40+
parserOptions: {ecmaVersion: 2015, sourceType: 'module'},
41+
filename: 'b.js',
42+
errors: [
43+
{
44+
message: 'Export was not imported by any modules.',
45+
type: 'ExportDefaultDeclaration'
46+
}
47+
]
48+
},
49+
{
50+
code: 'export const bar = 1',
51+
parserOptions: {ecmaVersion: 2015, sourceType: 'module'},
52+
filename: 'b.js',
53+
errors: [
54+
{
55+
message: 'Export was not imported by any modules.',
56+
type: 'ExportNamedDeclaration'
57+
}
58+
]
59+
},
60+
{
61+
code: 'export const foo = 1',
62+
parserOptions: {ecmaVersion: 2015, sourceType: 'module'},
63+
filename: 'c.js',
64+
errors: [
65+
{
66+
message: 'Export was not imported by any modules.',
67+
type: 'ExportNamedDeclaration'
68+
}
69+
]
70+
},
71+
{
72+
code: 'export default 1',
73+
parserOptions: {ecmaVersion: 2015, sourceType: 'module'},
74+
filename: 'd.js',
75+
errors: [
76+
{
77+
message: 'Export was not imported by any modules.',
78+
type: 'ExportDefaultDeclaration'
79+
}
80+
]
81+
},
82+
{
83+
code: 'export const foo = 1',
84+
parserOptions: {ecmaVersion: 2015, sourceType: 'module'},
85+
filename: 'd.js',
86+
errors: [
87+
{
88+
message: 'Export was not imported by any modules.',
89+
type: 'ExportNamedDeclaration'
90+
}
91+
]
92+
}
93+
]
94+
})
95+
})

tests/unused-module.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
const {describe, beforeEach, afterEach} = require('mocha')
2+
const depGraph = require('../lib/dependency-graph')
3+
var rule = require('../lib/rules/unused-module')
4+
var RuleTester = require('eslint').RuleTester
5+
var ruleTester = new RuleTester()
6+
7+
describe('dependency-graph', () => {
8+
const originalDepGraphImport = depGraph.imported
9+
beforeEach(() => {
10+
depGraph.entries.add('a.js')
11+
depGraph.imported = () => ({
12+
filenames: new Set(['b.js'])
13+
})
14+
})
15+
16+
afterEach(() => {
17+
depGraph.entries.clear()
18+
depGraph.imported = originalDepGraphImport
19+
})
20+
21+
ruleTester.run('unused-modules', rule, {
22+
valid: [
23+
{
24+
code: 'foo',
25+
filename: 'a.js'
26+
},
27+
{
28+
code: 'foo',
29+
filename: 'b.js'
30+
}
31+
],
32+
invalid: [
33+
{
34+
code: 'foo',
35+
filename: 'c.js',
36+
errors: [
37+
{
38+
message: 'Module was not imported by any files.',
39+
type: 'Program'
40+
}
41+
]
42+
}
43+
]
44+
})
45+
})

0 commit comments

Comments
 (0)