forked from NoRedInk/elm-assets-loader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
304 lines (276 loc) · 8.23 KB
/
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import test from 'ava';
import path from 'path';
import assign from "object-assign";
import MemoryFS from 'memory-fs';
import webpack from 'webpack';
import pify from 'pify';
import elmAssetsLoader from './index.js';
const outputPath = path.join(__dirname, 'build');
const fixturesPath = path.join(__dirname, "fixtures");
const elmMakePath = path.join(__dirname, 'node_modules/.bin/elm-make');
const globalConfig = {
context: fixturesPath,
output: {
path: outputPath,
filename: 'main.js'
},
resolve: {
root: fixturesPath,
extensions: ['', '.js', '.elm']
},
module: {
loaders: [
{
test: /\.elm$/,
loaders: [
path.join(__dirname, 'index.js'),
'elm-webpack?cwd=' + fixturesPath + '&pathToMake=' + elmMakePath
]
},
{
test: /\.svg$/,
loader: 'file-loader'
}
]
},
plugins: [
new webpack.NoErrorsPlugin()
]
};
const compile = (config) => {
return compileWithStats(config)
.then(outputAndStats => outputAndStats.output);
};
const compileWithStats = (config) => {
const fs = new MemoryFS();
const compiler = webpack(config);
compiler.outputFileSystem = fs;
return pify(compiler.run.bind(compiler))()
.then(stats => {
if (stats.compilation.errors.length > 0) {
throw stats.compilation.errors.map(error => error.message);
}
const exitPath = path.join(outputPath, 'main.js');
const output = fs.readFileSync(exitPath).toString('utf-8');
return {output: output, stats: stats.toJson()};
});
}
/* finding the tagger in compiled JS code */
test('transform tagger in a package with hyphen', async t => {
const config = assign({}, globalConfig, {
entry: './Just',
elmAssetsLoader: {
package: 'elm-lang/core',
module: 'Maybe',
tagger: 'Just'
}
});
const result = await compile(config);
t.regex(result, /Just\(__webpack_require__\(\d+\)\)/);
});
test('transform tagger not in package', async t => {
const config = assign({}, globalConfig, {
entry: './UserProject',
elmAssetsLoader: {
module: 'UserProject',
tagger: 'Asset'
}
});
const result = await compile(config);
t.regex(result, /Asset\(__webpack_require__\(\d+\)\)/);
});
test('do not transform tagger in different package', async t => {
const config = assign({}, globalConfig, {
entry: './MyMaybe',
elmAssetsLoader: {
module: 'MyMaybe',
tagger: 'Just'
}
});
const result = await compile(config);
t.regex(result, /Just\((["'])dont_touch_me.png\1\)/);
});
test('do not transform tagger in different module', async t => {
const config = assign({}, globalConfig, {
entry: './MyTag',
elmAssetsLoader: {
module: 'MyTag',
tagger: 'Asset'
}
});
const result = await compile(config);
t.regex(result, /Asset\((["'])dont_touch_me.png\1\)/);
});
/* argument handling */
test('cannot detect when tagger has multiple args', async t => {
const config = assign({}, globalConfig, {
entry: './MultiArg',
elmAssetsLoader: {
module: 'MultiArg',
tagger: 'Asset'
}
});
const result = await compile(config);
// inside an A2 call
t.regex(result, /Asset,\s*(["'])elm_logo.svg\1,\s*\1elm_logo.svg\1\)/);
});
test('do not fail when something else is called with multiple args ', async t => {
const config = assign({}, globalConfig, {
entry: './IrrelevantMultiArg',
elmAssetsLoader: {
module: 'IrrelevantMultiArg',
tagger: 'AssetPath'
}
});
const result = await compile(config);
// inside an A2 call
t.regex(result, /AssetPair,\s*(["'])elm_logo.svg\1,\s*\1elm_logo.svg\1\)/);
});
test('cannot detect if tagger with multiple values is called with a single arg', async t => {
const config = assign({}, globalConfig, {
entry: './PartialMultiArg',
elmAssetsLoader: {
module: 'PartialMultiArg',
tagger: 'AssetPair'
}
});
const result = await compile(config);
t.regex(result, /AssetPair\(__webpack_require__\(\d+\)\)/);
});
test('do not transform tagger that is actually a constant func', async t => {
const config = assign({}, globalConfig, {
entry: './NoArg',
elmAssetsLoader: {
module: 'NoArg',
tagger: 'assetPath'
}
});
const result = await compile(config);
t.regex(result, /assetPath\s*=\s*(["'])star.png\1/);
});
/* dynamicRequires */
test('dynamicRequires: default - warn', async t => {
const config = assign({}, globalConfig, {
entry: './ComplexCall',
elmAssetsLoader: {
module: 'ComplexCall',
tagger: 'ComplexCallAsset'
}
});
const result = await compileWithStats(config);
t.regex(result.output, /ComplexCallAsset\(A2/);
t.regex(result.output, /ComplexCallAsset\(A2/);
t.regex(result.stats.warnings[0], /will not be run through webpack.*ComplexCallAsset/);
});
test('dynamicRequires: ok - be silent', async t => {
const config = assign({}, globalConfig, {
entry: './ComplexCall',
elmAssetsLoader: {
module: 'ComplexCall',
tagger: 'ComplexCallAsset',
dynamicRequires: 'ok'
}
});
const result = await compileWithStats(config);
t.regex(result.output, /ComplexCallAsset\(A2/);
t.is(result.stats.warnings.length, 0);
});
test('dynamicRequires: warn - just warn', async t => {
const config = assign({}, globalConfig, {
entry: './ComplexCall',
elmAssetsLoader: {
module: 'ComplexCall',
tagger: 'ComplexCallAsset',
dynamicRequires: 'warn'
}
});
const result = await compileWithStats(config);
t.regex(result.output, /ComplexCallAsset\(A2/);
t.regex(result.stats.warnings[0], /will not be run through webpack.*ComplexCallAsset/);
});
test('dynamicRequires: error - raise when a string concatenation is tagge', async t => {
const config = assign({}, globalConfig, {
entry: './ComplexCall',
elmAssetsLoader: {
module: 'ComplexCall',
tagger: 'ComplexCallAsset',
dynamicRequires: 'error'
}
});
t.throws(compile(config), /Failing hard to make sure all assets.*ComplexCallAsset/);
});
test('dynamicRequires: error - raise when a variable is tagged', async t => {
const config = assign({}, globalConfig, {
entry: './VariableCall',
elmAssetsLoader: {
module: 'VariableCall',
tagger: 'VariableCallAsset',
dynamicRequires: 'error'
}
});
t.throws(compile(config), /Failing hard to make sure all assets.*VariableCallAsset/);
});
/* localPath */
test('find module after applying localPath transformation', async t => {
const config = assign({}, globalConfig, {
entry: './LocalPathOverride',
elmAssetsLoader: {
module: 'LocalPathOverride',
tagger: 'Asset',
localPath: s => 'elm_logo.svg'
}
});
const result = await compile(config);
t.regex(result, /Asset\(__webpack_require__\(\d+\)\)/);
});
test('fail to find module when localPath is not correctly configured', async t => {
const config = assign({}, globalConfig, {
entry: './LocalPathOverride',
elmAssetsLoader: {
module: 'LocalPathOverride',
tagger: 'Asset'
}
});
t.throws(compile(config), /Cannot resolve module \'non_sensical.png\'/);
});
test('raise when localPath does not return a string', async t => {
const config = assign({}, globalConfig, {
entry: './LocalPathOverride',
elmAssetsLoader: {
module: 'LocalPathOverride',
tagger: 'Asset',
localPath: s => 42
}
});
t.throws(compile(config), /not a string/);
});
/* query params */
test('require module to be configured', async t => {
const config = assign({}, globalConfig, {
entry: './UserProject',
elmAssetsLoader: {
tagger: 'Asset'
}
});
t.throws(compile(config), /configure module and tagger/);
});
test('require tagger to be configured', async t => {
const config = assign({}, globalConfig, {
entry: './UserProject',
elmAssetsLoader: {
module: 'UserProject'
}
});
t.throws(compile(config), /configure module and tagger/);
});
test('raise when dynamicRequires is set to an unknown value', async t => {
const config = assign({}, globalConfig, {
entry: './UserProject',
elmAssetsLoader: {
module: 'UserProject',
tagger: 'Asset',
dynamicRequires: 'ignore'
}
});
t.throws(compile(config), /Expecting dynamicRequires to be one of: error | warn | ok. You gave me: ignore/);
});