diff --git a/package-lock.json b/package-lock.json index 25612c7..466c16a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6222,9 +6222,9 @@ "dev": true }, "handlebars": { - "version": "4.1.2", - "resolved": "https://registry.npmjs.org/handlebars/-/handlebars-4.1.2.tgz", - "integrity": "sha512-nvfrjqvt9xQ8Z/w0ijewdD/vvWDTOweBUm96NTr66Wfvo1mJenBLwcYmPs3TIBP5ruzYGD7Hx/DaM9RmhroGPw==", + "version": "4.3.1", + "resolved": "https://registry.npmjs.org/handlebars/-/handlebars-4.3.1.tgz", + "integrity": "sha512-c0HoNHzDiHpBt4Kqe99N8tdLPKAnGCQ73gYMPWtAYM4PwGnf7xl8PBUHJqh9ijlzt2uQKaSRxbXRt+rZ7M2/kA==", "dev": true, "requires": { "neo-async": "^2.6.0", diff --git a/test/Linter.test.js b/test/Linter.test.js index e80dfc1..3efa11b 100644 --- a/test/Linter.test.js +++ b/test/Linter.test.js @@ -1,16 +1,19 @@ import Linter from '../src/Linter'; -const loaderContext = { resourcePath: 'test' }; -const options = { - eslintPath: 'eslint', - ignore: false, - formatter: jest.fn(), -}; -const res = { results: [{ filePath: '' }] }; - describe('Linter', () => { let linter; + beforeAll(() => { + const loaderContext = { + resourcePath: 'test', + }; + + const options = { + eslintPath: 'eslint', + ignore: false, + formatter: jest.fn(), + }; + linter = new Linter(loaderContext, options); }); @@ -19,6 +22,10 @@ describe('Linter', () => { }); it('should parse results correctly', () => { + const res = { + results: [{ filePath: '' }], + }; + expect(linter.parseResults(res)).toEqual([{ filePath: 'test' }]); }); }); diff --git a/test/autofix-stop.test.js b/test/autofix-stop.test.js index a8606f9..c58ada7 100644 --- a/test/autofix-stop.test.js +++ b/test/autofix-stop.test.js @@ -1,16 +1,18 @@ +import { join } from 'path'; + import { copySync, removeSync } from 'fs-extra'; -import webpack from 'webpack'; import chokidar from 'chokidar'; -import conf from './utils/conf'; +import pack from './utils/pack'; describe('autofix stop', () => { - const entry = './test/fixtures/nonfixable-clone.js'; + const entry = join(__dirname, 'fixtures/nonfixable-clone.js'); + let changed = false; let watcher; beforeAll(() => { - copySync('./test/fixtures/nonfixable.js', entry); + copySync(join(__dirname, 'fixtures/nonfixable.js'), entry); watcher = chokidar.watch(entry); watcher.on('change', () => { @@ -24,16 +26,7 @@ describe('autofix stop', () => { }); it('should not change file if there are no fixable errors/warnings', (done) => { - const compiler = webpack( - conf( - { - entry, - }, - { - fix: true, - } - ) - ); + const compiler = pack('nonfixable-clone', { fix: true }); compiler.run(() => { expect(changed).toBe(false); diff --git a/test/autofix.test.js b/test/autofix.test.js index a141b97..1908393 100644 --- a/test/autofix.test.js +++ b/test/autofix.test.js @@ -1,13 +1,14 @@ +import { join } from 'path'; + import { copySync, removeSync } from 'fs-extra'; -import webpack from 'webpack'; -import conf from './utils/conf'; +import pack from './utils/pack'; describe('autofix stop', () => { - const entry = './test/fixtures/fixable-clone.js'; + const entry = join(__dirname, 'fixtures/fixable-clone.js'); beforeAll(() => { - copySync('./test/fixtures/fixable.js', entry); + copySync(join(__dirname, 'fixtures/fixable.js'), entry); }); afterAll(() => { @@ -15,16 +16,7 @@ describe('autofix stop', () => { }); it('should not throw error if file ok after auto-fixing', (done) => { - const compiler = webpack( - conf( - { - entry, - }, - { - fix: true, - } - ) - ); + const compiler = pack('fixable-clone', { fix: true }); compiler.run((err, stats) => { expect(stats.hasWarnings()).toBe(false); diff --git a/test/error.test.js b/test/error.test.js index e48fbb0..f54412b 100644 --- a/test/error.test.js +++ b/test/error.test.js @@ -1,14 +1,8 @@ -import webpack from 'webpack'; - -import conf from './utils/conf'; +import pack from './utils/pack'; describe('error', () => { it('should return error if file is bad', (done) => { - const compiler = webpack( - conf({ - entry: './test/fixtures/error.js', - }) - ); + const compiler = pack('error'); compiler.run((err, stats) => { expect(stats.hasErrors()).toBe(true); diff --git a/test/eslint-path.test.js b/test/eslint-path.test.js index 97e36b5..f3f14cc 100644 --- a/test/eslint-path.test.js +++ b/test/eslint-path.test.js @@ -1,21 +1,11 @@ import { join } from 'path'; -import webpack from 'webpack'; - -import conf from './utils/conf'; +import pack from './utils/pack'; describe('eslint path', () => { it('should use another instance of eslint via eslintPath config', (done) => { - const compiler = webpack( - conf( - { - entry: './test/fixtures/good.js', - }, - { - eslintPath: join(__dirname, 'mock/eslint'), - } - ) - ); + const eslintPath = join(__dirname, 'mock/eslint'); + const compiler = pack('good', { eslintPath }); compiler.run((err, stats) => { expect(stats.hasErrors()).toBe(true); diff --git a/test/eslintignore.test.js b/test/eslintignore.test.js index 99545a0..13c1de7 100644 --- a/test/eslintignore.test.js +++ b/test/eslintignore.test.js @@ -1,19 +1,8 @@ -import webpack from 'webpack'; - -import conf from './utils/conf'; +import pack from './utils/pack'; describe('eslintignore', () => { it('should ignores files present in .eslintignore', (done) => { - const compiler = webpack( - conf( - { - entry: './test/fixtures/ignore.js', - }, - { - ignore: true, - } - ) - ); + const compiler = pack('ignore', { ignore: true }); compiler.run((err, stats) => { expect(stats.hasWarnings()).toBe(false); diff --git a/test/fail-on-error.test.js b/test/fail-on-error.test.js index 934bf3c..cf28348 100644 --- a/test/fail-on-error.test.js +++ b/test/fail-on-error.test.js @@ -1,21 +1,8 @@ -import webpack from 'webpack'; - -import conf from './utils/conf'; +import pack from './utils/pack'; describe('fail on error', () => { it('should emits errors', (done) => { - const compiler = webpack( - conf( - { - cache: true, - entry: './test/fixtures/error.js', - }, - { - failOnError: true, - cache: true, - } - ) - ); + const compiler = pack('error', { failOnError: true }); compiler.run((err, stats) => { expect(stats.hasErrors()).toBe(true); @@ -24,21 +11,20 @@ describe('fail on error', () => { }); it('should correctly indentifies a success', (done) => { - const compiler = webpack( - conf( - { - cache: true, - entry: './test/fixtures/good.js', - }, - { - failOnError: true, - cache: true, - } - ) - ); + const compiler = pack('good', { failOnError: true }); + compiler.run((err, stats) => { expect(stats.hasErrors()).toBe(false); done(); }); }); + + it('should emits errors when cache enabled', (done) => { + const compiler = pack('error', { failOnError: true, cache: true }); + + compiler.run((err, stats) => { + expect(stats.hasErrors()).toBe(true); + done(); + }); + }); }); diff --git a/test/fail-on-warning.test.js b/test/fail-on-warning.test.js index ba54bed..b54aac8 100644 --- a/test/fail-on-warning.test.js +++ b/test/fail-on-warning.test.js @@ -1,21 +1,8 @@ -import webpack from 'webpack'; - -import conf from './utils/conf'; +import pack from './utils/pack'; describe('fail on warning', () => { it('should emits errors', (done) => { - const compiler = webpack( - conf( - { - cache: true, - entry: './test/fixtures/warn.js', - }, - { - failOnWarning: true, - cache: true, - } - ) - ); + const compiler = pack('warn', { failOnWarning: true }); compiler.run((err, stats) => { expect(stats.hasErrors()).toBe(true); @@ -24,21 +11,20 @@ describe('fail on warning', () => { }); it('should correctly indentifies a success', (done) => { - const compiler = webpack( - conf( - { - cache: true, - entry: './test/fixtures/good.js', - }, - { - failOnWarning: true, - cache: true, - } - ) - ); + const compiler = pack('good', { failOnWarning: true }); + compiler.run((err, stats) => { expect(stats.hasErrors()).toBe(false); done(); }); }); + + it('should emits errors when cache enabled', (done) => { + const compiler = pack('error', { failOnWarning: true, cache: true }); + + compiler.run((err, stats) => { + expect(stats.hasErrors()).toBe(true); + done(); + }); + }); }); diff --git a/test/force-emit-error.test.js b/test/force-emit-error.test.js index 03a8038..ed9d3a5 100644 --- a/test/force-emit-error.test.js +++ b/test/force-emit-error.test.js @@ -1,19 +1,8 @@ -import webpack from 'webpack'; - -import conf from './utils/conf'; +import pack from './utils/pack'; describe('force emit error', () => { it('should force to emit error', (done) => { - const compiler = webpack( - conf( - { - entry: './test/fixtures/warn.js', - }, - { - emitError: true, - } - ) - ); + const compiler = pack('warn', { emitError: true }); compiler.run((err, stats) => { expect(stats.hasWarnings()).toBe(false); diff --git a/test/force-emit-warning.test.js b/test/force-emit-warning.test.js index 836c78d..54d97a5 100644 --- a/test/force-emit-warning.test.js +++ b/test/force-emit-warning.test.js @@ -1,19 +1,8 @@ -import webpack from 'webpack'; - -import conf from './utils/conf'; +import pack from './utils/pack'; describe('force emit warning', () => { it('should force to emit warning', (done) => { - const compiler = webpack( - conf( - { - entry: './test/fixtures/error.js', - }, - { - emitWarning: true, - } - ) - ); + const compiler = pack('error', { emitWarning: true }); compiler.run((err, stats) => { expect(stats.hasWarnings()).toBe(true); diff --git a/test/formatter-custom.test.js b/test/formatter-custom.test.js index c583ba4..c41420c 100644 --- a/test/formatter-custom.test.js +++ b/test/formatter-custom.test.js @@ -1,19 +1,9 @@ -import webpack from 'webpack'; - -import conf from './utils/conf'; +import pack from './utils/pack'; describe('formatter eslint', () => { it('should use custom formatter as function', (done) => { - const compiler = webpack( - conf( - { - entry: './test/fixtures/error.js', - }, - { - formatter: require('eslint-friendly-formatter'), - } - ) - ); + const formatter = require('eslint-friendly-formatter'); + const compiler = pack('error', { formatter }); compiler.run((err, stats) => { expect(stats.compilation.errors[0].message).toBeTruthy(); @@ -22,16 +12,8 @@ describe('formatter eslint', () => { }); it('should use custom formatter as string', (done) => { - const compiler = webpack( - conf( - { - entry: './test/fixtures/error.js', - }, - { - formatter: 'eslint-friendly-formatter', - } - ) - ); + const formatter = 'eslint-friendly-formatter'; + const compiler = pack('error', { formatter }); compiler.run((err, stats) => { expect(stats.compilation.errors[0].message).toBeTruthy(); diff --git a/test/formatter-eslint.test.js b/test/formatter-eslint.test.js index fc4d532..2ee22dc 100644 --- a/test/formatter-eslint.test.js +++ b/test/formatter-eslint.test.js @@ -1,14 +1,8 @@ -import webpack from 'webpack'; - -import conf from './utils/conf'; +import pack from './utils/pack'; describe('formatter eslint', () => { it('should use eslint formatter', (done) => { - const compiler = webpack( - conf({ - entry: './test/fixtures/error.js', - }) - ); + const compiler = pack('error'); compiler.run((err, stats) => { expect(stats.compilation.errors[0].message).toBeTruthy(); diff --git a/test/formatter-multiple-entries.test.js b/test/formatter-multiple-entries.test.js index 69d3a41..1567f41 100644 --- a/test/formatter-multiple-entries.test.js +++ b/test/formatter-multiple-entries.test.js @@ -1,4 +1,4 @@ -import { basename } from 'path'; +import { basename, join } from 'path'; import { readFileSync } from 'fs-extra'; import { CLIEngine } from 'eslint'; @@ -11,13 +11,11 @@ describe('formatter multiple entries', () => { const formatter = CLIEngine.getFormatter('checkstyle'); const outputFilename = 'outputReport-[name].txt'; const config = conf( - { - entry: [ - './test/fixtures/error-multi-two.js', - './test/fixtures/error-multi-one.js', - './test/fixtures/error-multi.js', - ], - }, + [ + join(__dirname, 'fixtures/error-multi-two.js'), + join(__dirname, 'fixtures/error-multi-one.js'), + join(__dirname, 'fixtures/error-multi.js'), + ], { formatter, outputReport: { @@ -32,7 +30,7 @@ describe('formatter multiple entries', () => { compiler.run((err, stats) => { stats.compilation.errors.forEach((e) => { const name = basename(e.module.resource, '.js'); - const filename = `${config.output.path}outputReport-${name}.txt`; + const filename = join(config.output.path, `outputReport-${name}.txt`); const contents = readFileSync(filename, 'utf8'); expect(e.error.message).toBe(contents); }); diff --git a/test/formatter-official.test.js b/test/formatter-official.test.js index 1deb50f..9245b02 100644 --- a/test/formatter-official.test.js +++ b/test/formatter-official.test.js @@ -1,19 +1,8 @@ -import webpack from 'webpack'; - -import conf from './utils/conf'; +import pack from './utils/pack'; describe('formatter official', () => { it('should use official formatter', (done) => { - const compiler = webpack( - conf( - { - entry: './test/fixtures/error.js', - }, - { - formatter: 'table', - } - ) - ); + const compiler = pack('error', { formatter: 'table' }); compiler.run((err, stats) => { expect(stats.compilation.errors[0].message).toBeTruthy(); diff --git a/test/formatter-write.test.js b/test/formatter-write.test.js index 74dd206..fb15ec3 100644 --- a/test/formatter-write.test.js +++ b/test/formatter-write.test.js @@ -9,22 +9,18 @@ import conf from './utils/conf'; describe('formatter write', () => { it('should configured to write eslint results to a file (relative path)', (done) => { const outputFilename = 'outputReport-relative.txt'; - const config = conf( - { - entry: './test/fixtures/error.js', + const loaderOptions = { + formatter: CLIEngine.getFormatter('checkstyle'), + outputReport: { + filePath: outputFilename, }, - { - formatter: CLIEngine.getFormatter('checkstyle'), - outputReport: { - filePath: outputFilename, - }, - } - ); + }; + const config = conf('error', loaderOptions); const compiler = webpack(config); compiler.run((err, stats) => { - const filePath = `${config.output.path}${outputFilename}`; + const filePath = join(config.output.path, outputFilename); const contents = readFileSync(filePath, 'utf8'); expect(stats.compilation.errors[0].error.message).toBe(contents); @@ -35,18 +31,14 @@ describe('formatter write', () => { it('should configured to write eslint results to a file (absolute path)', (done) => { const outputFilename = 'outputReport-absolute.txt'; const outputFilepath = join(__dirname, 'output', outputFilename); - const config = conf( - { - entry: './test/fixtures/error.js', + const loaderOptions = { + formatter: CLIEngine.getFormatter('checkstyle'), + outputReport: { + filePath: outputFilepath, }, - { - formatter: CLIEngine.getFormatter('checkstyle'), - outputReport: { - filePath: outputFilepath, - }, - } - ); + }; + const config = conf('error', loaderOptions); const compiler = webpack(config); compiler.run((err, stats) => { diff --git a/test/multiple-engines.test.js b/test/multiple-engines.test.js index 5504017..ea463d6 100644 --- a/test/multiple-engines.test.js +++ b/test/multiple-engines.test.js @@ -1,18 +1,20 @@ -import webpack from 'webpack'; +import { join } from 'path'; -import conf from './utils/conf'; +import pack from './utils/pack'; describe('multiple engines', () => { it('should will create an engine for each unique config', (done) => { - const compiler = webpack( - conf({ - entry: './test/fixtures/good.js', + const loader = join(__dirname, '../src/index'); + const compiler = pack( + 'good', + {}, + { module: { rules: [ { test: /\.js$/, exclude: /node_modules/, - loader: './src/index', + loader, options: { ignore: false, rules: { @@ -23,7 +25,7 @@ describe('multiple engines', () => { { test: /\.js$/, exclude: /node_modules/, - loader: './src/index', + loader, options: { ignore: false, rules: { @@ -33,7 +35,7 @@ describe('multiple engines', () => { }, ], }, - }) + } ); compiler.run((err, stats) => { diff --git a/test/no-eslint-configuration.test.js b/test/no-eslint-configuration.test.js index ce2c4f7..fc982b8 100644 --- a/test/no-eslint-configuration.test.js +++ b/test/no-eslint-configuration.test.js @@ -1,19 +1,8 @@ -import webpack from 'webpack'; - -import conf from './utils/conf'; +import pack from './utils/pack'; describe('no eslint configuration', () => { it('should emit warning when there is no eslint configuration', (done) => { - const compiler = webpack( - conf( - { - entry: './test/fixtures/good.js', - }, - { - cwd: '/', - } - ) - ); + const compiler = pack('good', { cwd: '/' }); compiler.run((err, stats) => { expect(stats.hasWarnings()).toBe(true); diff --git a/test/ok.test.js b/test/ok.test.js index e226096..10713ea 100644 --- a/test/ok.test.js +++ b/test/ok.test.js @@ -1,14 +1,8 @@ -import webpack from 'webpack'; - -import conf from './utils/conf'; +import pack from './utils/pack'; describe('ok', () => { it("should don't throw error if file is ok", (done) => { - const compiler = webpack( - conf({ - entry: './test/fixtures/good.js', - }) - ); + const compiler = pack('good'); compiler.run((err, stats) => { expect(stats.hasWarnings()).toBe(false); diff --git a/test/parameters.test.js b/test/parameters.test.js index 61bd86c..13554d3 100644 --- a/test/parameters.test.js +++ b/test/parameters.test.js @@ -1,21 +1,8 @@ -import webpack from 'webpack'; - -import conf from './utils/conf'; +import pack from './utils/pack'; describe('parameters', () => { it('should supports query strings parameters', (done) => { - const compiler = webpack( - conf( - { - entry: './test/fixtures/good.js', - }, - { - rules: { - semi: 0, - }, - } - ) - ); + const compiler = pack('good', { rules: { semi: 0 } }); compiler.run((err, stats) => { expect(stats.hasWarnings()).toBe(false); diff --git a/test/quiet.test.js b/test/quiet.test.js index a56f712..8d17762 100644 --- a/test/quiet.test.js +++ b/test/quiet.test.js @@ -1,19 +1,8 @@ -import webpack from 'webpack'; - -import conf from './utils/conf'; +import pack from './utils/pack'; describe('quiet', () => { it('should not emit warnings if quiet is set', (done) => { - const compiler = webpack( - conf( - { - entry: './test/fixtures/warn.js', - }, - { - quiet: true, - } - ) - ); + const compiler = pack('warn', { quiet: true }); compiler.run((err, stats) => { expect(stats.hasWarnings()).toBe(false); diff --git a/test/utils/conf.js b/test/utils/conf.js index 681f917..1c688a7 100644 --- a/test/utils/conf.js +++ b/test/utils/conf.js @@ -1,16 +1,14 @@ -import { join, sep } from 'path'; +import { join } from 'path'; + +export default (entry, loaderConf = {}, webpackConf = {}) => { + const testDir = join(__dirname, '..'); + const fixturesDir = join(testDir, 'fixtures'); -/** - * Returns a valid config for webpack. - * @param webpackConf Additional webpack config to apply/override to the default - * @param loaderConf Additional eslint config to apply/override to the default - * @returns {Object} - */ -module.exports = function conf(webpackConf, loaderConf) { return { + entry: typeof entry === 'string' ? join(fixturesDir, `${entry}.js`) : entry, mode: 'development', output: { - path: join(__dirname, '..', 'output') + sep, + path: join(testDir, 'output'), filename: 'bundle.js', }, module: { @@ -20,7 +18,7 @@ module.exports = function conf(webpackConf, loaderConf) { exclude: /node_modules/, use: [ { - loader: './src/index', + loader: join(testDir, '../src/index'), options: { // this disables the use of .eslintignore, since it contains the fixtures // folder to skip it on the global linting, but here we want the opposite diff --git a/test/utils/pack.js b/test/utils/pack.js new file mode 100644 index 0000000..859f7ea --- /dev/null +++ b/test/utils/pack.js @@ -0,0 +1,7 @@ +import webpack from 'webpack'; + +import conf from './conf'; + +export default (entry, loaderConf = {}, webpackConf = {}) => { + return webpack(conf(entry, loaderConf, webpackConf)); +}; diff --git a/test/warning.test.js b/test/warning.test.js index b0715bf..15b5fdb 100644 --- a/test/warning.test.js +++ b/test/warning.test.js @@ -1,14 +1,8 @@ -import webpack from 'webpack'; - -import conf from './utils/conf'; +import pack from './utils/pack'; describe('warning', () => { it('should emit warnings', (done) => { - const compiler = webpack( - conf({ - entry: './test/fixtures/warn.js', - }) - ); + const compiler = pack('warn'); compiler.run((err, stats) => { expect(stats.hasWarnings()).toBe(true);