-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
1 parent
2b2389f
commit 8ad6549
Showing
4 changed files
with
115 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters