Skip to content
This repository has been archived by the owner on Sep 28, 2020. It is now read-only.

Commit

Permalink
test: refactor (#298)
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardogobbosouza authored Sep 25, 2019
1 parent 360e69c commit 0821e14
Show file tree
Hide file tree
Showing 24 changed files with 132 additions and 302 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 15 additions & 8 deletions test/Linter.test.js
Original file line number Diff line number Diff line change
@@ -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);
});

Expand All @@ -19,6 +22,10 @@ describe('Linter', () => {
});

it('should parse results correctly', () => {
const res = {
results: [{ filePath: '' }],
};

expect(linter.parseResults(res)).toEqual([{ filePath: 'test' }]);
});
});
21 changes: 7 additions & 14 deletions test/autofix-stop.test.js
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand All @@ -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);
Expand Down
20 changes: 6 additions & 14 deletions test/autofix.test.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,22 @@
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(() => {
removeSync(entry);
});

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);
Expand Down
10 changes: 2 additions & 8 deletions test/error.test.js
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
16 changes: 3 additions & 13 deletions test/eslint-path.test.js
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
15 changes: 2 additions & 13 deletions test/eslintignore.test.js
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
40 changes: 13 additions & 27 deletions test/fail-on-error.test.js
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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();
});
});
});
40 changes: 13 additions & 27 deletions test/fail-on-warning.test.js
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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();
});
});
});
15 changes: 2 additions & 13 deletions test/force-emit-error.test.js
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
15 changes: 2 additions & 13 deletions test/force-emit-warning.test.js
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
28 changes: 5 additions & 23 deletions test/formatter-custom.test.js
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -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();
Expand Down
Loading

0 comments on commit 0821e14

Please sign in to comment.