forked from babel/babel-loader
/
loader.test.js
218 lines (199 loc) · 5.67 KB
/
loader.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
import test from "ava";
import fs from "fs";
import path from "path";
import rimraf from "rimraf";
import { satisfies } from "semver";
import webpack from "webpack";
import createTestDirectory from "./helpers/createTestDirectory";
const outputDir = path.join(__dirname, "output/loader");
const babelLoader = path.join(__dirname, "../lib");
const globalConfig = {
mode: "development",
entry: path.join(__dirname, "fixtures/basic.js"),
module: {
rules: [
{
test: /\.jsx?/,
loader: babelLoader,
options: {
presets: ["@babel/preset-env"],
},
exclude: /node_modules/,
},
],
},
};
// Create a separate directory for each test so that the tests
// can run in parallel
test.beforeEach.cb(t => {
createTestDirectory(outputDir, t.title, (err, directory) => {
if (err) return t.end(err);
t.context.directory = directory;
t.end();
});
});
test.afterEach.cb(t => rimraf(t.context.directory, t.end));
test.cb("should transpile the code snippet", t => {
const config = Object.assign({}, globalConfig, {
output: {
path: t.context.directory,
},
});
webpack(config, (err, stats) => {
t.is(err, null);
t.deepEqual(stats.compilation.errors, []);
t.deepEqual(stats.compilation.warnings, []);
fs.readdir(t.context.directory, (err, files) => {
t.is(err, null);
t.true(files.length === 1);
fs.readFile(path.resolve(t.context.directory, files[0]), (err, data) => {
t.is(err, null);
const test = "var App = function App()";
const subject = data.toString();
t.not(subject.indexOf(test), -1);
t.end();
});
});
});
});
test.cb("should not throw error on syntax error", t => {
const config = Object.assign({}, globalConfig, {
entry: path.join(__dirname, "fixtures/syntax.js"),
output: {
path: t.context.directory,
},
});
webpack(config, (err, stats) => {
t.true(stats.compilation.errors.length === 1);
t.true(stats.compilation.errors[0] instanceof Error);
t.deepEqual(stats.compilation.warnings, []);
t.end();
});
});
test.cb("should not throw without config", t => {
const config = {
mode: "development",
entry: path.join(__dirname, "fixtures/basic.js"),
output: {
path: t.context.directory,
},
module: {
rules: [
{
test: /\.jsx?/,
use: babelLoader,
exclude: /node_modules/,
},
],
},
};
webpack(config, (err, stats) => {
t.is(err, null);
t.deepEqual(stats.compilation.errors, []);
t.deepEqual(stats.compilation.warnings, []);
t.end();
});
});
test.cb(
"should return compilation errors with the message included in the stack trace",
t => {
const config = Object.assign({}, globalConfig, {
entry: path.join(__dirname, "fixtures/syntax.js"),
output: {
path: t.context.directory,
},
});
webpack(config, (err, stats) => {
t.is(err, null);
t.deepEqual(stats.compilation.warnings, []);
const moduleBuildError = stats.compilation.errors[0];
const babelLoaderError = moduleBuildError.error;
t.regex(babelLoaderError.stack, /Unexpected token/);
t.end();
});
},
);
test.cb("should load ESM config files", t => {
const config = Object.assign({}, globalConfig, {
entry: path.join(__dirname, "fixtures/constant.js"),
output: {
path: t.context.directory,
},
module: {
rules: [
{
test: /\.js$/,
loader: babelLoader,
exclude: /node_modules/,
options: {
// Use relative path starting with a dot to satisfy module loader.
// https://github.com/nodejs/node/issues/31710
// File urls doesn't work with current resolve@1.12.0 package.
extends: (
"." +
path.sep +
path.relative(
process.cwd(),
path.resolve(__dirname, "fixtures/babelrc.mjs"),
)
).replace(/\\/g, "/"),
babelrc: false,
},
},
],
},
});
webpack(config, (err, stats) => {
t.is(err, null);
// Node supports ESM without a flag starting from 12.13.0 and 13.2.0.
if (satisfies(process.version, `^12.13.0 || >=13.2.0`)) {
t.deepEqual(
stats.compilation.errors.map(e => e.message),
[],
);
} else {
t.is(stats.compilation.errors.length, 1);
const moduleBuildError = stats.compilation.errors[0];
const babelLoaderError = moduleBuildError.error;
t.true(babelLoaderError instanceof Error);
// Error messages are slightly different between versions:
// "modules aren't supported" or "modules not supported".
t.regex(babelLoaderError.message, /supported/i);
}
t.deepEqual(stats.compilation.warnings, []);
t.end();
});
});
test.cb("should track external dependencies", t => {
const dep = path.join(__dirname, "fixtures/metadata.js");
const config = Object.assign({}, globalConfig, {
entry: path.join(__dirname, "fixtures/constant.js"),
output: {
path: t.context.directory,
},
module: {
rules: [
{
test: /\.js$/,
loader: babelLoader,
options: {
babelrc: false,
configFile: false,
plugins: [
api => {
api.cache.never();
api.addExternalDependency(dep);
return { visitor: {} };
},
],
},
},
],
},
});
webpack(config, (err, stats) => {
t.true(stats.compilation.fileDependencies.has(dep));
t.deepEqual(stats.compilation.warnings, []);
t.end();
});
});