Skip to content

Commit

Permalink
test(pkg): Add tests to verify config
Browse files Browse the repository at this point in the history
Use Jest as a testing framework to check that the required config
options are being set correctly. This also adds the Jest ESLint plugin
and includes its recommended rules.
  • Loading branch information
scriptdaemon committed Dec 15, 2017
1 parent 2b2389f commit 8ad6549
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 3 deletions.
9 changes: 8 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
module.exports = {
plugins: [
'jest'
],
extends: [
'eslint:recommended',
'plugin:jest/recommended',
'plugin:vue/recommended',
'standard'
]
],
env: {
'jest/globals': true
}
}
79 changes: 79 additions & 0 deletions __mocks__/requireindex.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/* eslint-disable */

/*
* This file mocks an old, outdated package on which `eslint-plugin-vue`
* depends. The `requireindex` dependency uses `require.extensions` to determine
* which files to include (or omit), but Jest injects its own `require` global
* that does not populate the `extensions` property. This bug causes
* `requireindex` to ignore all files when in the Jest testing environment.
*
* Related GitHub issues:
* - https://github.com/facebook/jest/issues/2017
* - https://github.com/vuejs/eslint-plugin-vue/issues/290
*/

var FS = require('fs');
var Path = require('path');

module.exports = function (dir, basenames) {
var requires = {};

if (arguments.length === 2) {
// if basenames argument is passed, explicitly include those files
basenames.forEach(function (basename) {
var filepath = Path.resolve(Path.join(dir, basename));
requires[basename] = require(filepath);
});

} else if (arguments.length === 1) {
// if basenames arguments isn't passed, require all javascript
// files (except for those prefixed with _) and all directories

var files = FS.readdirSync(dir);

// sort files in lowercase alpha for linux
files.sort(function (a,b) {
a = a.toLowerCase();
b = b.toLowerCase();

if (a < b) {
return -1;
} else if (b < a) {
return 1;
} else {
return 0;
}
});

files.forEach(function (filename) {
// ignore index.js and files prefixed with underscore and
if ((filename === 'index.js') || (filename[0] === '_') || (filename[0] === '.')) {
return;
}

var filepath = Path.resolve(Path.join(dir, filename));
var ext = Path.extname(filename);
var stats = FS.statSync(filepath);

/*
* The next line of code has been patched to check that `ext` is any
* non-JavaScript extension directly, as `require.extensions` is empty
* when in the Jest testing environment.
*/

// don't require non-javascript files (.txt .md etc.)
if (stats.isFile() && !['.js', '.json', '.node'].includes(ext)) {
return;
}

var basename = Path.basename(filename, ext);

requires[basename] = require(filepath);
});

} else {
throw new Error("Must pass directory as first argument");
}

return requires;
};
23 changes: 23 additions & 0 deletions __tests__/eslintrc.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const { CLIEngine } = require('eslint')

const configFile = require.resolve('../.eslintrc')
const engine = new CLIEngine({ configFile })

test('includes required parser options', () => {
const config = engine.getConfigForFile(configFile)
expect(config.parserOptions.ecmaVersion).toBeGreaterThanOrEqual(6)
expect(config.parserOptions).toHaveProperty('sourceType', 'module')
})

test('includes required environments', () => {
const config = engine.getConfigForFile(configFile)
expect(config.env).toHaveProperty('browser', true)
expect(config.env).toHaveProperty('es6', true)
expect(config.env).toHaveProperty('node', true)
})

test('includes required plugins', () => {
const config = engine.getConfigForFile(configFile)
expect(config.plugins).toContain('jest')
expect(config.plugins).toContain('vue')
})
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,21 @@
"main": ".eslintrc.js",
"repository": "scriptdaemon/eslint-config",
"scripts": {
"test": "eslint --ignore-pattern !.eslintrc.js ."
"lint": "eslint --ignore-pattern !.eslintrc.js .",
"test": "npm run lint && jest"
},
"dependencies": {
"eslint-config-standard": "^10.2.1",
"eslint-plugin-import": "^2.8.0",
"eslint-plugin-jest": "^21.4.3",
"eslint-plugin-node": "^5.2.1",
"eslint-plugin-promise": "^3.6.0",
"eslint-plugin-standard": "^3.0.1",
"eslint-plugin-vue": "^4.0.0-beta.2"
},
"devDependencies": {
"eslint": "^4.12.1"
"eslint": "^4.12.1",
"jest": "^21.2.1"
},
"peerDependencies": {
"eslint": "^4.12.1"
Expand Down

0 comments on commit 8ad6549

Please sign in to comment.