/
index.test.js
280 lines (255 loc) · 8.71 KB
/
index.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
const path = require('path');
const webpack = require('webpack');
const { merge } = require('webpack-merge');
const { getOutput } = require('../src/utils/processResources');
const pathToLoader = require.resolve('../lib/loader.js');
/**
* See: https://github.com/facebook/jest/issues/1909
* @jest-environment node
*/
function runWebpack(baseConfig, done) {
const webpackConfig = merge({
output: {
path: path.join(__dirname, 'output'),
libraryTarget: 'commonjs2',
},
mode: 'production',
}, baseConfig);
webpack(webpackConfig, (webpackErr, stats) => {
const err = webpackErr
|| (stats.hasErrors() && stats.compilation.errors[0])
|| (stats.hasWarnings() && stats.compilation.warnings[0]);
done(err || null);
});
}
function execTest(testId, options) {
return new Promise((resolve, reject) => {
const baseConfig = merge({
entry: path.resolve(__dirname, 'scss', `${testId}.scss`),
output: {
filename: `${testId}.js`,
},
module: {
rules: [{
test: /\.scss$/,
use: [
{ loader: 'raw-loader' },
{
loader: pathToLoader,
options,
},
],
}],
},
});
runWebpack(baseConfig, (err) => (err ? reject(err) : resolve()));
});
}
describe('sass-resources-loader', () => {
describe('resources', () => {
it('should parse resource', () => execTest('empty', {
resources: path.resolve(__dirname, './scss/shared/_variables.scss'),
}).then(() => {
// eslint-disable-next-line global-require
const output = require('./output/empty').default;
expect(output).toMatchSnapshot();
}));
it('should parse array resources', () => execTest('empty2', {
resources: [
path.resolve(__dirname, './scss/shared/*.scss'),
],
}).then(() => {
// eslint-disable-next-line global-require
const output = require('./output/empty2').default;
expect(output).toMatchSnapshot();
}));
it('should include resources', () => execTest('imports', {
resources: [
path.resolve(__dirname, './scss/shared/_imports.scss'),
],
}).then(() => {
// eslint-disable-next-line global-require
const output = require('./output/imports').default;
expect(output).toMatchSnapshot();
}));
it('should throw error when no resources provided', (done) => {
runWebpack({
entry: path.resolve(__dirname, 'scss', 'empty.scss'),
module: {
rules: [{
test: /\.scss$/,
use: [
{ loader: 'raw-loader' },
{
loader: pathToLoader,
},
],
}],
},
}, (err) => {
expect(err.message).toMatch(/Can't find sass resources in your config. Make sure loader.options.resources exists/);
done();
});
});
it('should throw error when resources are empty', (done) => {
runWebpack({
entry: path.resolve(__dirname, 'scss', 'empty.scss'),
module: {
rules: [{
test: /\.scss$/,
use: [
{ loader: 'raw-loader' },
{
loader: pathToLoader,
options: {
resources: [],
},
},
],
}],
},
}, (err) => {
expect(err.message).toMatch(/Something wrong with provided resources/);
expect(err.message).toMatch(/Make sure 'options.resources' is String or Array of Strings/);
done();
});
});
it('should throw error when resources glob did not resolve any files', (done) => {
const resource = path.resolve(__dirname, 'scss/does-not-exist/*.scss');
runWebpack({
entry: path.resolve(__dirname, 'scss', 'empty.scss'),
module: {
rules: [{
test: /\.scss$/,
use: [
{ loader: 'raw-loader' },
{
loader: pathToLoader,
options: {
resources: resource,
},
},
],
}],
},
}, (err) => {
expect(err.message).toMatch(`Couldn't find any files with the glob ${resource}.`);
done();
});
});
it('should throw error when no files were resolved from any resource globs', (done) => {
const validResource = path.resolve(__dirname, 'scss', 'empty.scss');
const invalidResource = path.resolve(__dirname, 'scss/does-not-exist/*.scss');
runWebpack({
entry: path.resolve(__dirname, 'scss', 'empty.scss'),
module: {
rules: [{
test: /\.scss$/,
use: [
{ loader: 'raw-loader' },
{
loader: pathToLoader,
options: {
resources: [validResource, invalidResource],
},
},
],
}],
},
}, (err) => {
expect(err.message).not.toMatch(validResource);
expect(err.message).toMatch(`Couldn't find any files with the glob ${invalidResource}.`);
done();
});
});
});
describe('hoisting', () => {
it('should hoist entry @use imports when option hoistUseStatements is true', () => execTest('hoist', {
resources: [
path.resolve(__dirname, './scss/shared/_variables.scss'),
],
hoistUseStatements: true,
}).then(() => {
// eslint-disable-next-line global-require
const output = require('./output/hoist').default;
expect(output).toMatchSnapshot();
}));
it('should not hoist entry @use imports when option hoistUseStatements is false', () => execTest('hoist2', {
resources: [
path.resolve(__dirname, './scss/shared/_variables.scss'),
],
hoistUseStatements: false,
}).then(() => {
// eslint-disable-next-line global-require
const output = require('./output/hoist2').default;
expect(output).toMatchSnapshot();
}));
it('should not hoist entry @use imports when option hoistUseStatements is not passed', () => execTest('hoist3', {
resources: [
path.resolve(__dirname, './scss/shared/_variables.scss'),
],
}).then(() => {
// eslint-disable-next-line global-require
const output = require('./output/hoist3').default;
expect(output).toMatchSnapshot();
}));
it('should work with multiline @use statements', () => execTest('hoist-multiline', {
resources: [
path.resolve(__dirname, './scss/shared/_variables.scss'),
],
hoistUseStatements: true,
}).then(() => {
// eslint-disable-next-line global-require
const output = require('./output/hoist-multiline').default;
expect(output).toMatchSnapshot();
}));
});
describe('imports', () => {
it('should not rewrite path for imports with ~', () => {
// eslint-disable-next-line global-require
const getNewUsePath = require('../lib/utils/rewritePaths').getRelativeUsePath;
expect(getNewUsePath('~/bootstrap', '', '')).toMatch('~/bootstrap');
});
it('should preserve import method', () => execTest('imports2', {
resources: [
path.resolve(__dirname, './scss/shared/_imports.scss'),
],
}).then(() => {
// eslint-disable-next-line global-require
const output = require('./output/imports2').default;
expect(output).toMatchSnapshot();
}));
it('should not rewrite the path for built-ins with @use', () => execTest('use-builtin', {
hoistUseStatements: true,
resources: [
path.resolve(__dirname, './scss/shared/_math.scss'),
],
}).then(() => {
// eslint-disable-next-line global-require
const output = require('./output/use-builtin').default;
expect(output).toMatchSnapshot();
}));
it('should not rewrite the path for built-ins with @use and double quotes', () => execTest('use-builtin', {
hoistUseStatements: true,
resources: [
path.resolve(__dirname, './scss/shared/_math-double-quotes.scss'),
],
}).then(() => {
// eslint-disable-next-line global-require
const output = require('./output/use-builtin').default;
expect(output).toMatchSnapshot();
}));
});
describe('getOutput', () => {
it('de-duplicates imports from the source when they\'re present in a resource and `hoiseUseStatements` is enabled', () => {
const result = getOutput(
"@use 'sass:math';\n@use 'sass:list';\n@use 'sass:map';\ndiv { padding: $padding; }",
"@use 'sass:math';\n$padding: 10px;",
{ hoistUseStatements: true },
);
expect(result).toBe(
"@use 'sass:math';\n@use 'sass:list';\n@use 'sass:map';\n\n$padding: 10px;\ndiv { padding: $padding; }",
);
});
});
});