-
Notifications
You must be signed in to change notification settings - Fork 921
/
runtime.test.js
290 lines (262 loc) · 7.3 KB
/
runtime.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
const {testRuntimeFixture} = require('../../fixture-utils');
const dedent = require('dedent');
describe('runtime', () => {
beforeAll(() => {
// Needed until we make Snowpack's JS Build Interface quiet by default
require('snowpack').logger.level = 'error';
});
it('Can invalidate proxy files', async () => {
const fixture = await testRuntimeFixture({
'main.js': dedent`
import data from './data.json';
export function getData() {
return data;
}
`,
'data.json': dedent`
[ 1, 2 ]
`,
'package.json': dedent`
{
"version": "1.0.1",
"name": "@snowpack/test-runtime-invalidate"
}
`,
});
let mod = await fixture.runtime.importModule('/main.js');
expect(mod.exports.getData()).toStrictEqual([1, 2]);
// Change the file
await fixture.writeFile(
'data.json',
dedent`
[ 1, 2, 3 ]
`,
);
try {
fixture.runtime.invalidateModule('/data.json');
mod = await fixture.runtime.importModule('/main.js');
expect(mod.exports.getData()).toStrictEqual([1, 2, 3]);
} finally {
await fixture.cleanup();
}
});
// This test is skipped due to the way Jest runs tests, you can't use dynamic import
// which this test depends on. See:
// https://github.com/nodejs/node/issues/35889
it.skip('Provides import.meta.fileURL in SSR', async () => {
const fixture = await testRuntimeFixture({
'packages/dep/package.json': dedent`
{
"name": "@snowpack/test-runtime-metaurl-dep",
"version": "0.0.1",
"main": "main.js"
}
`,
'packages/dep/main.js': dedent`
import fs from 'node:fs';
const readFile = fs.promises.readFile;
export async function getData(url) {
const json = await fs.readFile(url, 'utf-8');
const data = JSON.parse(json);
return data;
}
`,
'main.js': dedent`
import fs from 'node:fs/promises';
import { getData as getDataDepFn } from '@snowpack/test-runtime-metaurl-dep';
const url = new URL('./data.json', import.meta.url);
const depUrl = new URL('./packages/dep/package.json', import.meta.url);
export async function getData() {
const json = await fs.readFile(url, 'utf-8');
const data = JSON.parse(json);
return data;
}
export async function getDepVersion() {
return (await getDataDepFn(depUrl)).version;
}
`,
'data.json': dedent`
[ 1, 2 ]
`,
'package.json': dedent`
{
"version": "1.0.1",
"name": "@snowpack/test-runtime-metaurl"
}
`,
'snowpack.config.json': dedent`
{
"packageOptions": {
"external": ["node:fs/promises"]
}
}
`,
});
try {
let mod = await fixture.runtime.importModule('/main.js');
expect(await mod.exports.getData()).toStrictEqual([1, 2]);
expect(await mod.exports.getDepVersion()).equal('0.0.1');
} finally {
await fixture.cleanup();
}
});
it('Can import a CommonJS module as the default export', async () => {
const fixture = await testRuntimeFixture({
'packages/other/package.json': dedent`
{
"version": "1.0.0",
"name": "other",
"main": "main.js"
}
`,
'packages/other/main.js': dedent`
module.exports = () => 'works';
`,
'main.js': dedent`
import fn from 'other';
export function test() {
return fn();
}
`,
'package.json': dedent`
{
"version": "1.0.1",
"name": "@snowpack/test-runtime-import-cjs",
"dependencies": {
"other": "file:./packages/other"
}
}
`,
'snowpack.config.json': dedent`
{
"packageOptions": {
"external": ["other"]
}
}
`,
});
try {
let mod = await fixture.runtime.importModule('/main.js');
expect(await mod.exports.test()).toEqual('works');
} finally {
await fixture.cleanup();
}
});
it('Can handle folders with dots', async () => {
const fixture = await testRuntimeFixture({
'public/example.com/index.html': `<html></html>\n`,
'public/example/index.html': `<html></html>\n`,
'snowpack.config.js': `
module.exports = {
mount: {
public: { url: '/', static: true }
}
}
`,
});
try {
let contents = (await fixture.loadUrl('/example.com/')).contents.toString('utf8');
expect(contents).toEqual('<html></html>\n');
} finally {
await fixture.cleanup();
}
});
it('Executes scripts in the correct order', async () => {
const fixture = await testRuntimeFixture({
'one.js': dedent`
global.NUM = 1;
export default {};
`,
'two.js': dedent`
global.NUM++;
export default global.NUM;
`,
'main.js': dedent`
import one from './one.js';
import two from './two.js';
export const val = two;
`,
});
try {
let mod = await fixture.runtime.importModule('/main.js');
expect(await mod.exports.val).toEqual(2);
} finally {
await fixture.cleanup();
}
});
it('Executes scripts only once', async () => {
const fixture = await testRuntimeFixture({
'dep.js': dedent`
export let state = 0;
export default function() {
state++;
};
`,
'one.js': dedent`
import inc from './dep.js';
inc();
`,
'two.js': dedent`
import inc from './dep.js';
inc();
`,
'main.js': dedent`
import './one.js';
import './two.js';
import { state } from './dep.js';
export const val = state;
`,
});
try {
let mod = await fixture.runtime.importModule('/main.js');
expect(mod.exports.val).toEqual(2);
} finally {
await fixture.cleanup();
}
});
it('Installs dynamic imports', async () => {
const fixture = await testRuntimeFixture({
'packages/@owner/dyn2/package.json': dedent`
{
"version": "1.0.0",
"name": "@owner/dyn2",
"module": "main.js"
}
`,
'packages/@owner/dyn2/sub/sibling.js': dedent`
export default 3;
`,
'packages/@owner/dyn2/sub/path.js': dedent`
const promise = import('./sibling.js');
export default async function() {
let mod = await promise;
return mod.default;
}
`,
'package.json': dedent`
{
"version": "1.0.1",
"name": "@snowpack/test-runtime-import-dynamic-pkg",
"dependencies": {
"@owner/dyn2": "file:./packages/@owner/dyn2"
}
}
`,
'main.js': dedent`
import fn from '@owner/dyn2/sub/path.js';
export default async function() {
let value = await fn();
return value;
}
`,
});
try {
let mod = await fixture.runtime.importModule('/main.js');
let promise = mod.exports.default();
let value = await promise;
expect(value).toEqual(3);
} finally {
await fixture.cleanup();
}
});
});