Skip to content

Commit

Permalink
New: no-unused-modules rule - added more tests
Browse files Browse the repository at this point in the history
Signed-off-by: René Fermann <rene.fermann@gmx.de>
  • Loading branch information
rfermann committed Jul 31, 2018
1 parent a402023 commit 230122b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
Empty file.
Empty file.
32 changes: 30 additions & 2 deletions tests/src/rules/no-unused-modules.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { test, testFilePath } from '../utils'

import { RuleTester } from 'eslint'
import { expect } from 'chai';

const doPreparation = require( '../../../src/rules/no-unused-modules').doPreparation

const ruleTester = new RuleTester()
, rule = require('rules/no-unused-modules')
Expand All @@ -17,6 +20,26 @@ const unusedExportsOptions = [{
ignore: [testFilePath('./no-unused-modules/*ignored*.js')],
}]

describe('doPreparation throws correct errors', () => {
// const fn = doPreparation()
const context = {id: 'no-unused-modules' }
it('should throw an error, if src is not an array', () => {
expect(doPreparation.bind(doPreparation, null, null, context)).to.throw(`Rule ${context.id}: src option must be an array`)
})
it('should throw an error, if ignore is not an array', () => {
expect(doPreparation.bind(doPreparation, [], null, context)).to.throw(`Rule ${context.id}: ignore option must be an array`)
})
it('should throw an error, if src is empty', () => {
expect(doPreparation.bind(doPreparation, [], [], context)).to.throw(`Rule ${context.id}: src option must be defined`)
})
it('should throw an error, if src is empty', () => {
expect(doPreparation.bind(doPreparation, [""], [], context)).to.throw(`Rule ${context.id}: src option must not contain empty strings`)
})
it('should throw an error, if src is empty', () => {
expect(doPreparation.bind(doPreparation, ["src"], [""], context)).to.throw(`Rule ${context.id}: ignore option must not contain empty strings`)
})
})

// tests for missing exports
ruleTester.run('no-unused-modules', rule, {
valid: [
Expand All @@ -39,6 +62,11 @@ ruleTester.run('no-unused-modules', rule, {
code: 'const a = 1',
errors: [error(`No exports found`)],
}),
test({
options: missingExportsOptions,
code: '/* const a = 1 */',
errors: [error(`No exports found`)],
}),
],
})

Expand Down Expand Up @@ -152,7 +180,7 @@ ruleTester.run('no-unused-modules', rule, {
ruleTester.run('no-unused-modules', rule, {
valid: [
test({ options: unusedExportsOptions,
code: `import g from '${testFilePath('./no-unused-modules/file-g.js')}'`,
code: `import g from '${testFilePath('./no-unused-modules/file-g.js')}';import {h} from '${testFilePath('./no-unused-modules/file-gg.js')}'`,
filename: testFilePath('./no-unused-modules/file-0.js')}),
],
invalid: [
Expand All @@ -166,7 +194,7 @@ ruleTester.run('no-unused-modules', rule, {
ruleTester.run('no-unused-modules', rule, {
valid: [
test({ options: unusedExportsOptions,
code: `import { g } from '${testFilePath('./no-unused-modules/file-g.js')}'`,
code: `import { g } from '${testFilePath('./no-unused-modules/file-g.js')}'; import eslint from 'eslint'`,
filename: testFilePath('./no-unused-modules/file-0.js')}),
test({ options: unusedExportsOptions,
code: 'export const g = 2',
Expand Down

0 comments on commit 230122b

Please sign in to comment.