Skip to content

test: add basic tests for dependency-graph & linters #82

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/rules/unused-export.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const {entries, imported} = require('../dependency-graph')
const depGraph = require('../dependency-graph')

module.exports = {
meta: {
Expand All @@ -7,9 +7,9 @@ module.exports = {

create(context) {
const filename = context.getFilename()
const {identifiers} = imported()
const {identifiers} = depGraph.imported()

if (entries.has(filename)) {
if (depGraph.entries.has(filename)) {
return {}
}

Expand Down
6 changes: 3 additions & 3 deletions lib/rules/unused-module.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const {entries, imported} = require('../dependency-graph')
const depGraph = require('../dependency-graph')

module.exports = {
meta: {
Expand All @@ -8,13 +8,13 @@ module.exports = {
create(context) {
const filename = context.getFilename()

if (entries.has(filename)) {
if (depGraph.entries.has(filename)) {
return {}
}

return {
Program(node) {
const {filenames} = imported()
const {filenames} = depGraph.imported()
if (!filenames.has(filename)) {
context.report(node, 'Module was not imported by any files.')
}
Expand Down
17 changes: 14 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"eslint": ">=6.5.1",
"flow-bin": ">=0.110.1",
"graphql": ">=14.5.8",
"mocha": ">=6.2.2"
"mocha": ">=6.2.2",
"rimraf": "^3.0.0"
}
}
48 changes: 48 additions & 0 deletions tests/dependency-graph.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const dependencyGraph = require('../lib/dependency-graph')
const assert = require('assert')
const {describe, it, before, after} = require('mocha')
const rimraf = require('rimraf')
const {resolve} = require('path')
const {mkdirSync, writeFileSync, symlinkSync} = require('fs')
describe('dependency-graph', () => {
it('has entries set', () => {
assert(dependencyGraph.entries instanceof Set)
})
it('has dependencyGraph map', () => {
assert(dependencyGraph.dependencyGraph instanceof Map)
})
it('has imported function', () => {
assert.equal(typeof dependencyGraph.imported, 'function')
})
describe('imported', () => {
before(() => {
rimraf.sync('./fixtures')
mkdirSync('./fixtures')
writeFileSync('./fixtures/index.js', ``)
writeFileSync('./fixtures/one.js', ``)
symlinkSync('./fixtures/one.js', './fixtures/link.js')
const graph = {imports: new Map(), exports: new Set()}
graph.imports.set(resolve('./fixtures/one.js'), new Set(['a', 'b']))
dependencyGraph.dependencyGraph.set('./fixtures/index.js', graph)
})
after(() => {
rimraf.sync('./fixtures')
dependencyGraph.dependencyGraph.clear()
})
it('gathers all imported files from dependencyGraph', () => {
const {filenames} = dependencyGraph.imported()
assert.deepEqual([...filenames], [resolve('./fixtures/one.js')])
})
it('gathers all imported identifiers from dependencyGraph', () => {
const {identifiers} = dependencyGraph.imported()
assert.deepEqual([...identifiers], [resolve('./fixtures/one.js#a'), resolve('./fixtures/one.js#b')])
})
it('follows symlinks', () => {
const graph = dependencyGraph.dependencyGraph.get('./fixtures/index.js')
graph.imports.delete('./fixtures/one.js')
graph.imports.set('./fixtures/link.js', new Set(['a', 'b']))
const {filenames} = dependencyGraph.imported()
assert.deepEqual([...filenames], [resolve('./fixtures/one.js')])
})
})
})
95 changes: 95 additions & 0 deletions tests/unused-export.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
const {describe, beforeEach, afterEach} = require('mocha')
const depGraph = require('../lib/dependency-graph')
var rule = require('../lib/rules/unused-export')
var RuleTester = require('eslint').RuleTester
var ruleTester = new RuleTester()

describe('dependency-graph', () => {
const originalDepGraphImport = depGraph.imported
beforeEach(() => {
depGraph.imported = () => ({
identifiers: new Set(['a.js#*', 'b.js#foo', 'c.js#default'])
})
})

afterEach(() => {
depGraph.imported = originalDepGraphImport
})

ruleTester.run('unused-export', rule, {
valid: [
{
code: 'export const foo = 1',
parserOptions: {ecmaVersion: 2015, sourceType: 'module'},
filename: 'a.js'
},
{
code: 'export const foo = 1',
parserOptions: {ecmaVersion: 2015, sourceType: 'module'},
filename: 'b.js'
},
{
code: 'export default 1',
parserOptions: {ecmaVersion: 2015, sourceType: 'module'},
filename: 'c.js'
}
],
invalid: [
{
code: 'export default 1',
parserOptions: {ecmaVersion: 2015, sourceType: 'module'},
filename: 'b.js',
errors: [
{
message: 'Export was not imported by any modules.',
type: 'ExportDefaultDeclaration'
}
]
},
{
code: 'export const bar = 1',
parserOptions: {ecmaVersion: 2015, sourceType: 'module'},
filename: 'b.js',
errors: [
{
message: 'Export was not imported by any modules.',
type: 'ExportNamedDeclaration'
}
]
},
{
code: 'export const foo = 1',
parserOptions: {ecmaVersion: 2015, sourceType: 'module'},
filename: 'c.js',
errors: [
{
message: 'Export was not imported by any modules.',
type: 'ExportNamedDeclaration'
}
]
},
{
code: 'export default 1',
parserOptions: {ecmaVersion: 2015, sourceType: 'module'},
filename: 'd.js',
errors: [
{
message: 'Export was not imported by any modules.',
type: 'ExportDefaultDeclaration'
}
]
},
{
code: 'export const foo = 1',
parserOptions: {ecmaVersion: 2015, sourceType: 'module'},
filename: 'd.js',
errors: [
{
message: 'Export was not imported by any modules.',
type: 'ExportNamedDeclaration'
}
]
}
]
})
})
45 changes: 45 additions & 0 deletions tests/unused-module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const {describe, beforeEach, afterEach} = require('mocha')
const depGraph = require('../lib/dependency-graph')
var rule = require('../lib/rules/unused-module')
var RuleTester = require('eslint').RuleTester
var ruleTester = new RuleTester()

describe('dependency-graph', () => {
const originalDepGraphImport = depGraph.imported
beforeEach(() => {
depGraph.entries.add('a.js')
depGraph.imported = () => ({
filenames: new Set(['b.js'])
})
})

afterEach(() => {
depGraph.entries.clear()
depGraph.imported = originalDepGraphImport
})

ruleTester.run('unused-modules', rule, {
valid: [
{
code: 'foo',
filename: 'a.js'
},
{
code: 'foo',
filename: 'b.js'
}
],
invalid: [
{
code: 'foo',
filename: 'c.js',
errors: [
{
message: 'Module was not imported by any files.',
type: 'Program'
}
]
}
]
})
})