-
Notifications
You must be signed in to change notification settings - Fork 382
/
module-resolver.spec.ts
278 lines (248 loc) · 9.38 KB
/
module-resolver.spec.ts
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
/*
* Copyright (c) 2018, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: MIT
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
*/
import { compile } from '../../compiler/compiler';
import { pretify } from '../../__tests__/utils';
import { DiagnosticLevel } from '@lwc/errors';
const VALID_CONFIG = {
isExplicitImport: true,
outputConfig: {
minify: false,
compat: false,
format: 'amd',
},
name: 'class_and_template',
namespace: 'x',
};
describe('module resolver', () => {
test('compiler should resolve bundle with manually imported and rendered template', async () => {
const noOutputConfig = {
...VALID_CONFIG,
files: {
'class_and_template.js': `
import { LightningElement } from 'lwc';
import mytemplate from './class_and_template.html';
export default class Test extends LightningElement {
render() {
return mytemplate;
}
}`,
'class_and_template.html': `<template><p>Manually Imported Template</p></template>`,
},
};
const { success, result } = await compile(noOutputConfig);
expect(success).toBe(true);
expect(pretify(result.code)).toBe(
pretify(`define('x/class_and_template', ['exports', 'lwc'], function (exports, lwc) {
function tmpl($api, $cmp, $slotset, $ctx) {
const {
t: api_text,
h: api_element
} = $api;
return [api_element("p", {
key: 0
}, [api_text("Manually Imported Template")])];
}
var mytemplate = lwc.registerTemplate(tmpl);
tmpl.stylesheets = [];
tmpl.stylesheetTokens = {
hostAttribute: "x-class_and_template_class_and_template-host",
shadowAttribute: "x-class_and_template_class_and_template"
};
class Test extends lwc.LightningElement {
render() {
return mytemplate;
}
}
exports.default = Test;
Object.defineProperty(exports, '__esModule', { value: true });
});`)
);
});
test('compiler should resolve bundle with manually imported template that does not match component name', async () => {
const noOutputConfig = {
...VALID_CONFIG,
files: {
'class_and_template.js': `
import { LightningElement } from 'lwc';
import mytemplate from './anotherTemplate.html';
export default class Test extends LightningElement {
render() {
return mytemplate;
}
}`,
'anotherTemplate.html': `<template><p>Another Template</p></template>`,
},
};
const { success, result } = await compile(noOutputConfig);
expect(success).toBe(true);
expect(pretify(result.code)).toBe(
pretify(`define('x/class_and_template', ['exports', 'lwc'], function (exports, lwc) {
function tmpl($api, $cmp, $slotset, $ctx) {
const {
t: api_text,
h: api_element
} = $api;
return [api_element("p", {
key: 0
}, [api_text("Another Template")])];
}
var mytemplate = lwc.registerTemplate(tmpl);
tmpl.stylesheets = [];
tmpl.stylesheetTokens = {
hostAttribute: "x-class_and_template_anotherTemplate-host",
shadowAttribute: "x-class_and_template_anotherTemplate"
};
class Test extends lwc.LightningElement {
render() {
return mytemplate;
}
}
exports.default = Test;
Object.defineProperty(exports, '__esModule', { value: true });
});`)
);
});
test('compiler should resolve bundle with local import', async () => {
const COMPILER_CONFIG_BASEDIR = {
name: 'foo',
namespace: 'x',
files: {
'foo.js': `import { nested } from './lib/foo';`,
'lib/foo.js': ``,
},
};
const { result, success } = await compile(COMPILER_CONFIG_BASEDIR);
expect(success).toBe(true);
expect(result).toBeDefined();
});
test('compiler should report fatal diagnostic if local import cannot be resolved', async () => {
const COMPILER_CONFIG_BASEDIR = {
name: 'foo',
namespace: 'x',
files: {
'foo.js': `import { nested } from './lib/foo';`,
},
};
const { diagnostics, success } = await compile(COMPILER_CONFIG_BASEDIR);
expect(success).toBe(false);
expect(diagnostics).toMatchObject([
{
level: 0,
message: expect.stringContaining(
'LWC1011: Failed to resolve import "./lib/foo" from "foo.js". Please add "lib/foo" file to the component folder.'
),
},
]);
});
test('compiler should report name case mismatch diagnostic for local import', async () => {
const COMPILER_CONFIG_BASEDIR = {
name: 'foo',
namespace: 'x',
files: {
'foo.js': `import { nested } from './lib/foo';`,
'lib/Foo.js': ``,
},
};
const { diagnostics, success } = await compile(COMPILER_CONFIG_BASEDIR);
expect(success).toBe(false);
expect(diagnostics).toMatchObject([
{
level: 0,
message: expect.stringContaining(
'LWC1011: Failed to resolve import "./lib/foo" from "foo.js". Please add "lib/foo" file to the component folder.'
),
},
]);
});
test('compiler should report fatal diagnostic when invalid entry path is specified', async () => {
const COMPILER_CONFIG_BASEDIR = {
name: 'modules/foo',
namespace: 'x',
files: {
'foo.js': ``,
},
};
const { diagnostics, success } = await compile(COMPILER_CONFIG_BASEDIR);
expect(success).toBe(false);
expect(diagnostics[0].level).toBe(DiagnosticLevel.Fatal);
});
test('#492 - compiler should not report external modules in diagnostics', async () => {
const { success, diagnostics } = await compile({
namespace: 'x',
name: 'foo',
files: {
'foo.js': `
import { LightningElement } from 'lwc';
export default class Test extends LightningElement {}
`,
'foo.html': `
<template>
<x-foo></x-foo>
</template>
`,
},
});
expect(success).toBe(true);
expect(diagnostics).toHaveLength(0);
});
});
describe('module entry validation', () => {
test('compiler should fail module resolution if an entry name starts with capital letter', async () => {
const { diagnostics, success } = await compile({
name: 'MycmpCamelcased',
namespace: 'c',
files: {
'mycmpCamelcased.js': ``,
'mycmpCamelcased.html': ``,
},
});
expect(success).toBe(false);
expect(diagnostics).toMatchObject([
{
level: 0,
message: expect.stringContaining(
'Illegal folder name "MycmpCamelcased". The folder name must start with a lowercase character: "mycmpCamelcased".'
),
},
]);
});
test('compiler should not fail module resolution if an entry contains non-leading capital letter', async () => {
const { success } = await compile({
name: 'myCmp',
namespace: 'c',
files: {
'myCmp.js': `
import { LightningElement } from 'lwc';
export default class App extends LightningElement {}
`,
'myCmp.html': `<template></template>`,
},
});
expect(success).toBe(true);
});
});
describe('module file name validation', () => {
test('compilation should fail when folder and its entry ".js" file names do not case match', async () => {
const { diagnostics, success } = await compile({
name: 'mycmp',
namespace: 'c',
files: {
'Mycmp.js': ``,
'Mycmp.html': ``,
},
});
expect(success).toBe(false);
expect(diagnostics).toMatchObject([
{
level: 0,
message: expect.stringContaining(
'LWC1010: Failed to resolve entry for module "mycmp".'
),
},
]);
});
});