Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
epidemian
GitHub Repository: epidemian/eslint-plugin-import
Path: blob/main/resolvers/webpack/test/config.js
829 views
1
'use strict';
2
3
const chai = require('chai');
4
const expect = chai.expect;
5
const path = require('path');
6
7
const resolve = require('../index').resolve;
8
9
const file = path.join(__dirname, 'files', 'src', 'jsx', 'dummy.js');
10
const extensionFile = path.join(__dirname, 'config-extensions', 'src', 'dummy.js');
11
12
const absoluteSettings = {
13
config: path.join(__dirname, 'files', 'some', 'absolute.path.webpack.config.js'),
14
};
15
16
describe('config', function () {
17
it('finds webpack.config.js in parent directories', function () {
18
expect(resolve('main-module', file)).to.have.property('path')
19
.and.equal(path.join(__dirname, 'files', 'src', 'main-module.js'));
20
});
21
22
it('finds absolute webpack.config.js files', function () {
23
expect(resolve('foo', file, absoluteSettings)).to.have.property('path')
24
.and.equal(path.join(__dirname, 'files', 'some', 'absolutely', 'goofy', 'path', 'foo.js'));
25
});
26
27
it('finds compile-to-js configs', function () {
28
const settings = {
29
config: path.join(__dirname, './files/webpack.config.babel.js'),
30
};
31
32
expect(resolve('main-module', file, settings))
33
.to.have.property('path')
34
.and.equal(path.join(__dirname, 'files', 'src', 'main-module.js'));
35
});
36
37
it('finds compile-to-js config in parent directories', function () {
38
expect(resolve('main-module', extensionFile))
39
.to.have.property('path')
40
.and.equal(path.join(__dirname, 'config-extensions', 'src', 'main-module.js'));
41
});
42
43
it('finds the first config with a resolve section', function () {
44
const settings = {
45
config: path.join(__dirname, './files/webpack.config.multiple.js'),
46
};
47
48
expect(resolve('main-module', file, settings)).to.have.property('path')
49
.and.equal(path.join(__dirname, 'files', 'src', 'main-module.js'));
50
});
51
52
it('finds the config at option config-index', function () {
53
const settings = {
54
config: path.join(__dirname, './files/webpack.config.multiple.js'),
55
'config-index': 2,
56
};
57
58
expect(resolve('foo', file, settings)).to.have.property('path')
59
.and.equal(path.join(__dirname, 'files', 'some', 'goofy', 'path', 'foo.js'));
60
});
61
62
it("doesn't swallow config load errors (#435)", function () {
63
const settings = {
64
config: path.join(__dirname, './files/webpack.config.garbage.js'),
65
};
66
expect(function () { resolve('foo', file, settings); }).to.throw(Error);
67
});
68
69
it('finds config object when config is an object', function () {
70
const settings = {
71
config: require(path.join(__dirname, 'files', 'some', 'absolute.path.webpack.config.js')),
72
};
73
expect(resolve('foo', file, settings)).to.have.property('path')
74
.and.equal(path.join(__dirname, 'files', 'some', 'absolutely', 'goofy', 'path', 'foo.js'));
75
});
76
77
it('finds config object when config uses a path relative to working dir', function () {
78
const settings = {
79
config: './test/files/some/absolute.path.webpack.config.js',
80
};
81
expect(resolve('foo', file, settings)).to.have.property('path')
82
.and.equal(path.join(__dirname, 'files', 'some', 'absolutely', 'goofy', 'path', 'foo.js'));
83
});
84
85
it('finds the first config with a resolve section when config is an array of config objects', function () {
86
const settings = {
87
config: require(path.join(__dirname, './files/webpack.config.multiple.js')),
88
};
89
90
expect(resolve('main-module', file, settings)).to.have.property('path')
91
.and.equal(path.join(__dirname, 'files', 'src', 'main-module.js'));
92
});
93
94
it('finds the config at option config-index when config is an array of config objects', function () {
95
const settings = {
96
config: require(path.join(__dirname, './files/webpack.config.multiple.js')),
97
'config-index': 2,
98
};
99
100
expect(resolve('foo', file, settings)).to.have.property('path')
101
.and.equal(path.join(__dirname, 'files', 'some', 'goofy', 'path', 'foo.js'));
102
});
103
104
it('finds the config at option env when config is a function', function () {
105
const settings = {
106
config: require(path.join(__dirname, './files/webpack.function.config.js')),
107
env: {
108
dummy: true,
109
},
110
};
111
112
expect(resolve('bar', file, settings)).to.have.property('path')
113
.and.equal(path.join(__dirname, 'files', 'some', 'goofy', 'path', 'bar.js'));
114
});
115
116
it('finds the config at option env when config is an array of functions', function () {
117
const settings = {
118
config: require(path.join(__dirname, './files/webpack.function.config.multiple.js')),
119
env: {
120
dummy: true,
121
},
122
};
123
124
expect(resolve('bar', file, settings)).to.have.property('path')
125
.and.equal(path.join(__dirname, 'files', 'some', 'goofy', 'path', 'bar.js'));
126
});
127
128
it('passes argv to config when it is a function', function () {
129
const settings = {
130
config: require(path.join(__dirname, './files/webpack.function.config.js')),
131
argv: {
132
mode: 'test',
133
},
134
};
135
136
expect(resolve('baz', file, settings)).to.have.property('path')
137
.and.equal(path.join(__dirname, 'files', 'some', 'bar', 'bar.js'));
138
});
139
140
it('passes a default empty argv object to config when it is a function', function () {
141
const settings = {
142
config: require(path.join(__dirname, './files/webpack.function.config.js')),
143
argv: undefined,
144
};
145
146
expect(function () { resolve('baz', file, settings); }).to.not.throw(Error);
147
});
148
149
it('prevents async config using', function () {
150
const settings = {
151
config: require(path.join(__dirname, './files/webpack.config.async.js')),
152
};
153
const result = resolve('foo', file, settings);
154
155
expect(result).not.to.have.property('path');
156
expect(result).to.have.property('found').to.be.false;
157
});
158
});
159
160