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

Commit 0821e14

Browse files
test: refactor (#298)
1 parent 360e69c commit 0821e14

24 files changed

+132
-302
lines changed

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/Linter.test.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
import Linter from '../src/Linter';
22

3-
const loaderContext = { resourcePath: 'test' };
4-
const options = {
5-
eslintPath: 'eslint',
6-
ignore: false,
7-
formatter: jest.fn(),
8-
};
9-
const res = { results: [{ filePath: '' }] };
10-
113
describe('Linter', () => {
124
let linter;
5+
136
beforeAll(() => {
7+
const loaderContext = {
8+
resourcePath: 'test',
9+
};
10+
11+
const options = {
12+
eslintPath: 'eslint',
13+
ignore: false,
14+
formatter: jest.fn(),
15+
};
16+
1417
linter = new Linter(loaderContext, options);
1518
});
1619

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

2124
it('should parse results correctly', () => {
25+
const res = {
26+
results: [{ filePath: '' }],
27+
};
28+
2229
expect(linter.parseResults(res)).toEqual([{ filePath: 'test' }]);
2330
});
2431
});

test/autofix-stop.test.js

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1+
import { join } from 'path';
2+
13
import { copySync, removeSync } from 'fs-extra';
2-
import webpack from 'webpack';
34
import chokidar from 'chokidar';
45

5-
import conf from './utils/conf';
6+
import pack from './utils/pack';
67

78
describe('autofix stop', () => {
8-
const entry = './test/fixtures/nonfixable-clone.js';
9+
const entry = join(__dirname, 'fixtures/nonfixable-clone.js');
10+
911
let changed = false;
1012
let watcher;
1113

1214
beforeAll(() => {
13-
copySync('./test/fixtures/nonfixable.js', entry);
15+
copySync(join(__dirname, 'fixtures/nonfixable.js'), entry);
1416

1517
watcher = chokidar.watch(entry);
1618
watcher.on('change', () => {
@@ -24,16 +26,7 @@ describe('autofix stop', () => {
2426
});
2527

2628
it('should not change file if there are no fixable errors/warnings', (done) => {
27-
const compiler = webpack(
28-
conf(
29-
{
30-
entry,
31-
},
32-
{
33-
fix: true,
34-
}
35-
)
36-
);
29+
const compiler = pack('nonfixable-clone', { fix: true });
3730

3831
compiler.run(() => {
3932
expect(changed).toBe(false);

test/autofix.test.js

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,22 @@
1+
import { join } from 'path';
2+
13
import { copySync, removeSync } from 'fs-extra';
2-
import webpack from 'webpack';
34

4-
import conf from './utils/conf';
5+
import pack from './utils/pack';
56

67
describe('autofix stop', () => {
7-
const entry = './test/fixtures/fixable-clone.js';
8+
const entry = join(__dirname, 'fixtures/fixable-clone.js');
89

910
beforeAll(() => {
10-
copySync('./test/fixtures/fixable.js', entry);
11+
copySync(join(__dirname, 'fixtures/fixable.js'), entry);
1112
});
1213

1314
afterAll(() => {
1415
removeSync(entry);
1516
});
1617

1718
it('should not throw error if file ok after auto-fixing', (done) => {
18-
const compiler = webpack(
19-
conf(
20-
{
21-
entry,
22-
},
23-
{
24-
fix: true,
25-
}
26-
)
27-
);
19+
const compiler = pack('fixable-clone', { fix: true });
2820

2921
compiler.run((err, stats) => {
3022
expect(stats.hasWarnings()).toBe(false);

test/error.test.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
1-
import webpack from 'webpack';
2-
3-
import conf from './utils/conf';
1+
import pack from './utils/pack';
42

53
describe('error', () => {
64
it('should return error if file is bad', (done) => {
7-
const compiler = webpack(
8-
conf({
9-
entry: './test/fixtures/error.js',
10-
})
11-
);
5+
const compiler = pack('error');
126

137
compiler.run((err, stats) => {
148
expect(stats.hasErrors()).toBe(true);

test/eslint-path.test.js

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,11 @@
11
import { join } from 'path';
22

3-
import webpack from 'webpack';
4-
5-
import conf from './utils/conf';
3+
import pack from './utils/pack';
64

75
describe('eslint path', () => {
86
it('should use another instance of eslint via eslintPath config', (done) => {
9-
const compiler = webpack(
10-
conf(
11-
{
12-
entry: './test/fixtures/good.js',
13-
},
14-
{
15-
eslintPath: join(__dirname, 'mock/eslint'),
16-
}
17-
)
18-
);
7+
const eslintPath = join(__dirname, 'mock/eslint');
8+
const compiler = pack('good', { eslintPath });
199

2010
compiler.run((err, stats) => {
2111
expect(stats.hasErrors()).toBe(true);

test/eslintignore.test.js

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,8 @@
1-
import webpack from 'webpack';
2-
3-
import conf from './utils/conf';
1+
import pack from './utils/pack';
42

53
describe('eslintignore', () => {
64
it('should ignores files present in .eslintignore', (done) => {
7-
const compiler = webpack(
8-
conf(
9-
{
10-
entry: './test/fixtures/ignore.js',
11-
},
12-
{
13-
ignore: true,
14-
}
15-
)
16-
);
5+
const compiler = pack('ignore', { ignore: true });
176

187
compiler.run((err, stats) => {
198
expect(stats.hasWarnings()).toBe(false);

test/fail-on-error.test.js

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,8 @@
1-
import webpack from 'webpack';
2-
3-
import conf from './utils/conf';
1+
import pack from './utils/pack';
42

53
describe('fail on error', () => {
64
it('should emits errors', (done) => {
7-
const compiler = webpack(
8-
conf(
9-
{
10-
cache: true,
11-
entry: './test/fixtures/error.js',
12-
},
13-
{
14-
failOnError: true,
15-
cache: true,
16-
}
17-
)
18-
);
5+
const compiler = pack('error', { failOnError: true });
196

207
compiler.run((err, stats) => {
218
expect(stats.hasErrors()).toBe(true);
@@ -24,21 +11,20 @@ describe('fail on error', () => {
2411
});
2512

2613
it('should correctly indentifies a success', (done) => {
27-
const compiler = webpack(
28-
conf(
29-
{
30-
cache: true,
31-
entry: './test/fixtures/good.js',
32-
},
33-
{
34-
failOnError: true,
35-
cache: true,
36-
}
37-
)
38-
);
14+
const compiler = pack('good', { failOnError: true });
15+
3916
compiler.run((err, stats) => {
4017
expect(stats.hasErrors()).toBe(false);
4118
done();
4219
});
4320
});
21+
22+
it('should emits errors when cache enabled', (done) => {
23+
const compiler = pack('error', { failOnError: true, cache: true });
24+
25+
compiler.run((err, stats) => {
26+
expect(stats.hasErrors()).toBe(true);
27+
done();
28+
});
29+
});
4430
});

test/fail-on-warning.test.js

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,8 @@
1-
import webpack from 'webpack';
2-
3-
import conf from './utils/conf';
1+
import pack from './utils/pack';
42

53
describe('fail on warning', () => {
64
it('should emits errors', (done) => {
7-
const compiler = webpack(
8-
conf(
9-
{
10-
cache: true,
11-
entry: './test/fixtures/warn.js',
12-
},
13-
{
14-
failOnWarning: true,
15-
cache: true,
16-
}
17-
)
18-
);
5+
const compiler = pack('warn', { failOnWarning: true });
196

207
compiler.run((err, stats) => {
218
expect(stats.hasErrors()).toBe(true);
@@ -24,21 +11,20 @@ describe('fail on warning', () => {
2411
});
2512

2613
it('should correctly indentifies a success', (done) => {
27-
const compiler = webpack(
28-
conf(
29-
{
30-
cache: true,
31-
entry: './test/fixtures/good.js',
32-
},
33-
{
34-
failOnWarning: true,
35-
cache: true,
36-
}
37-
)
38-
);
14+
const compiler = pack('good', { failOnWarning: true });
15+
3916
compiler.run((err, stats) => {
4017
expect(stats.hasErrors()).toBe(false);
4118
done();
4219
});
4320
});
21+
22+
it('should emits errors when cache enabled', (done) => {
23+
const compiler = pack('error', { failOnWarning: true, cache: true });
24+
25+
compiler.run((err, stats) => {
26+
expect(stats.hasErrors()).toBe(true);
27+
done();
28+
});
29+
});
4430
});

test/force-emit-error.test.js

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,8 @@
1-
import webpack from 'webpack';
2-
3-
import conf from './utils/conf';
1+
import pack from './utils/pack';
42

53
describe('force emit error', () => {
64
it('should force to emit error', (done) => {
7-
const compiler = webpack(
8-
conf(
9-
{
10-
entry: './test/fixtures/warn.js',
11-
},
12-
{
13-
emitError: true,
14-
}
15-
)
16-
);
5+
const compiler = pack('warn', { emitError: true });
176

187
compiler.run((err, stats) => {
198
expect(stats.hasWarnings()).toBe(false);

0 commit comments

Comments
 (0)