|
1 | 1 | import * as path from "node:path"; |
| 2 | +import * as vm from "node:vm"; |
2 | 3 | import { jest, describe, it, expect } from "@jest/globals"; |
3 | 4 | import { bundle } from "./metro"; |
| 5 | +import { buildWebEntryModule } from "./html"; |
4 | 6 |
|
5 | 7 | const resolvePath = (filename: string) => |
6 | 8 | path.join(__dirname, "../../fixtures", filename); |
7 | 9 |
|
8 | 10 | jest.setTimeout(30000); |
9 | 11 |
|
| 12 | +const runInVmContext = (code: string) => { |
| 13 | + const context = vm.createContext({}); |
| 14 | + |
| 15 | + vm.runInContext( |
| 16 | + buildWebEntryModule(code) |
| 17 | + // vm does not support esm yet |
| 18 | + .replace(/^export default /, ""), |
| 19 | + context |
| 20 | + ); |
| 21 | +}; |
| 22 | + |
10 | 23 | describe("bundle", () => { |
11 | 24 | it("default", async () => { |
12 | 25 | const filename = "app-export-default.jsx"; |
13 | 26 | const filePath = resolvePath(filename); |
14 | 27 | const res = await bundle(filePath); |
15 | 28 | expect(res).toMatchSnapshot(); |
| 29 | + |
| 30 | + runInVmContext(res); |
16 | 31 | }); |
17 | 32 |
|
18 | 33 | it("default (tsx)", async () => { |
19 | 34 | const filename = "app-export-default.tsx"; |
20 | 35 | const filePath = resolvePath(filename); |
21 | 36 | const res = await bundle(filePath); |
22 | 37 | expect(res).toMatchSnapshot(); |
| 38 | + |
| 39 | + runInVmContext(res); |
23 | 40 | }); |
24 | 41 |
|
25 | 42 | it("with json", async () => { |
26 | 43 | const filename = "app-export-default-with-json.jsx"; |
27 | 44 | const filePath = resolvePath(filename); |
28 | 45 | const res = await bundle(filePath); |
29 | 46 | expect(res).toMatchSnapshot(); |
| 47 | + |
| 48 | + runInVmContext(res); |
30 | 49 | }); |
31 | 50 |
|
32 | 51 | it("with txt", async () => { |
33 | 52 | const filename = "app-export-default-with-txt.jsx"; |
34 | 53 | const filePath = resolvePath(filename); |
35 | 54 | const res = await bundle(filePath); |
36 | 55 | expect(res).toMatchSnapshot(); |
| 56 | + |
| 57 | + runInVmContext(res); |
37 | 58 | }); |
38 | 59 |
|
39 | 60 | it("with md", async () => { |
40 | 61 | const filename = "app-export-default-with-md.jsx"; |
41 | 62 | const filePath = resolvePath(filename); |
42 | 63 | const res = await bundle(filePath); |
43 | 64 | expect(res).toMatchSnapshot(); |
| 65 | + |
| 66 | + runInVmContext(res); |
44 | 67 | }); |
45 | 68 |
|
46 | 69 | it("with images", async () => { |
47 | 70 | const filename = "app-export-default-with-images.jsx"; |
48 | 71 | const filePath = resolvePath(filename); |
49 | 72 | const res = await bundle(filePath); |
50 | 73 | expect(res).toMatchSnapshot(); |
| 74 | + |
| 75 | + runInVmContext(res); |
51 | 76 | }); |
52 | 77 |
|
53 | 78 | it("with html", async () => { |
54 | 79 | const filename = "app-export-default-with-html.jsx"; |
55 | 80 | const filePath = resolvePath(filename); |
56 | 81 | const res = await bundle(filePath); |
57 | 82 | expect(res).toMatchSnapshot(); |
| 83 | + |
| 84 | + runInVmContext(res); |
58 | 85 | }); |
59 | 86 |
|
60 | 87 | it("with wasm", async () => { |
61 | 88 | const filename = "app-export-default-with-wasm.jsx"; |
62 | 89 | const filePath = resolvePath(filename); |
63 | 90 | const res = await bundle(filePath); |
64 | 91 | expect(res).toMatchSnapshot(); |
| 92 | + |
| 93 | + runInVmContext(res); |
65 | 94 | }); |
66 | 95 |
|
67 | 96 | it("with backticks", async () => { |
68 | 97 | const filename = "app-export-default-with-backticks.jsx"; |
69 | 98 | const filePath = resolvePath(filename); |
70 | 99 | const res = await bundle(filePath); |
71 | 100 | expect(res).toMatchSnapshot(); |
| 101 | + |
| 102 | + runInVmContext(res); |
72 | 103 | }); |
73 | 104 |
|
74 | 105 | it("default (preact)", async () => { |
75 | 106 | const filename = "app-export-default-preact.jsx"; |
76 | 107 | const filePath = resolvePath(filename); |
77 | 108 | const res = await bundle(filePath); |
78 | 109 | expect(res).toMatchSnapshot(); |
| 110 | + |
| 111 | + runInVmContext(res); |
79 | 112 | }); |
80 | 113 | }); |
0 commit comments