From 8ad6549e478bd46c214a6d5e5bf2eba6ce402214 Mon Sep 17 00:00:00 2001 From: Kenneth Williams Date: Thu, 14 Dec 2017 20:53:24 -0800 Subject: [PATCH] test(pkg): Add tests to verify config 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. --- .eslintrc.js | 9 ++++- __mocks__/requireindex.js | 79 ++++++++++++++++++++++++++++++++++++++ __tests__/eslintrc.test.js | 23 +++++++++++ package.json | 7 +++- 4 files changed, 115 insertions(+), 3 deletions(-) create mode 100644 __mocks__/requireindex.js create mode 100644 __tests__/eslintrc.test.js diff --git a/.eslintrc.js b/.eslintrc.js index 17daeef..e9747c7 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -1,7 +1,14 @@ module.exports = { + plugins: [ + 'jest' + ], extends: [ 'eslint:recommended', + 'plugin:jest/recommended', 'plugin:vue/recommended', 'standard' - ] + ], + env: { + 'jest/globals': true + } } diff --git a/__mocks__/requireindex.js b/__mocks__/requireindex.js new file mode 100644 index 0000000..da2b6d2 --- /dev/null +++ b/__mocks__/requireindex.js @@ -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; +}; diff --git a/__tests__/eslintrc.test.js b/__tests__/eslintrc.test.js new file mode 100644 index 0000000..def0b06 --- /dev/null +++ b/__tests__/eslintrc.test.js @@ -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') +}) diff --git a/package.json b/package.json index dce2f35..8a7dcb7 100644 --- a/package.json +++ b/package.json @@ -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"