-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathcompilation.spec.ts
More file actions
129 lines (124 loc) · 4.36 KB
/
compilation.spec.ts
File metadata and controls
129 lines (124 loc) · 4.36 KB
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
import path from 'node:path';
import { describe, test, expect } from 'vitest';
import { CompilerError } from '@lwc/errors';
import { compileComponentForSSR } from '../index';
expect.addSnapshotSerializer({
test(val) {
return val instanceof CompilerError;
},
serialize(val: CompilerError, config, indentation, depth, refs, printer) {
return printer(
{
message: val.message,
location: val.location,
filename: val.filename,
},
config,
indentation,
depth,
refs
);
},
});
describe('component compilation', () => {
test('implicit templates imports do not use full file paths', () => {
const src = `
import { LightningElement } from 'lwc';
export default class extends LightningElement {}
`;
const filename = path.resolve('component.js');
const { code } = compileComponentForSSR(src, filename, {});
expect(code).toContain('import tmpl from "./component.html"');
});
test('explicit templates imports do not use full file paths', () => {
const src = `
import { LightningElement } from 'lwc';
import explicit from './explicit.html';
export default class extends LightningElement {}
`;
const filename = path.resolve('component.js');
const { code } = compileComponentForSSR(src, filename, {});
expect(code).toContain('import explicit from "./explicit.html"');
});
test('supports .ts file imports', () => {
const src = `
import { LightningElement } from 'lwc';
export default class extends LightningElement {}
`;
const filename = path.resolve('component.ts');
const { code } = compileComponentForSSR(src, filename, {});
expect(code).toContain('import tmpl from "./component.html"');
});
describe('wire decorator', () => {
test('error when using @wire and @track together', () => {
const src = `import { track, wire, LightningElement } from "lwc";
import { getFoo } from "data-service";
export default class Test extends LightningElement {
@track
@wire(getFoo, { key1: "$prop1", key2: ["fixed", "array"] })
wiredWithTrack;
}
`;
expect(() => compileComponentForSSR(src, 'test.js', {}))
.toThrowErrorMatchingInlineSnapshot(`
{
"filename": "test.js",
"location": {
"column": 2,
"length": 59,
"line": 5,
"start": 156,
},
"message": "LWC1095: @wire method or property cannot be used with @track",
}
`);
});
test('throws when wired method is combined with @api', () => {
const src = `import { api, wire, LightningElement } from "lwc";
import { getFoo } from "data-service";
export default class Test extends LightningElement {
@api
@wire(getFoo, { key1: "$prop1", key2: ["fixed"] })
wiredWithApi() {}
}
`;
expect(() => compileComponentForSSR(src, 'test.js', {}))
.toThrowErrorMatchingInlineSnapshot(`
{
"filename": "test.js",
"location": {
"column": 2,
"length": 50,
"line": 5,
"start": 152,
},
"message": "LWC1095: @wire method or property cannot be used with @api",
}
`);
});
test('throws when computed property is expression', () => {
const src = `import { wire, LightningElement } from "lwc";
import { getFoo } from "data-service";
const symbol = Symbol.for("key");
export default class Test extends LightningElement {
// accidentally an array expression = oops!
@wire(getFoo, { [[symbol]]: "$prop1", key2: ["fixed", "array"] })
wiredFoo;
}
`;
expect(() => compileComponentForSSR(src, 'test.js', {}))
.toThrowErrorMatchingInlineSnapshot(`
{
"filename": "test.js",
"location": {
"column": 2,
"length": 9,
"line": 7,
"start": 288,
},
"message": "LWC1200: Computed property in @wire config must be a constant or primitive literal.",
}
`);
});
});
});