Skip to content

Commit e0cb800

Browse files
committed
Update metro.spec.ts
1 parent ad20ef7 commit e0cb800

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

src/plugin/html.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { ROOT_ID } from "../constants";
22

3-
export const createContent = (js: string): string => {
3+
/**
4+
* @internal
5+
*/
6+
export const buildWebEntryModule = (js: string): string => {
47
// https://github.com/inokawa/react-native-react-bridge/pull/133
58
js = js.replace(/([`$])/g, "\\$1");
69
return (

src/plugin/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import metroTransformer from "metro-react-native-babel-transformer";
99
import { isEntryFile } from "./babel";
1010
import { bundle } from "./metro";
11-
import { createContent } from "./html";
11+
import { buildWebEntryModule } from "./html";
1212

1313
export const transform = async (args: any /* TODO */) => {
1414
const { filename, src } = args;
@@ -17,7 +17,7 @@ export const transform = async (args: any /* TODO */) => {
1717
const res = await bundle(filename);
1818
return metroTransformer.transform({
1919
...args,
20-
src: createContent(res),
20+
src: buildWebEntryModule(res),
2121
});
2222
}
2323

src/plugin/metro.spec.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,113 @@
11
import * as path from "node:path";
2+
import * as vm from "node:vm";
23
import { jest, describe, it, expect } from "@jest/globals";
34
import { bundle } from "./metro";
5+
import { buildWebEntryModule } from "./html";
46

57
const resolvePath = (filename: string) =>
68
path.join(__dirname, "../../fixtures", filename);
79

810
jest.setTimeout(30000);
911

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+
1023
describe("bundle", () => {
1124
it("default", async () => {
1225
const filename = "app-export-default.jsx";
1326
const filePath = resolvePath(filename);
1427
const res = await bundle(filePath);
1528
expect(res).toMatchSnapshot();
29+
30+
runInVmContext(res);
1631
});
1732

1833
it("default (tsx)", async () => {
1934
const filename = "app-export-default.tsx";
2035
const filePath = resolvePath(filename);
2136
const res = await bundle(filePath);
2237
expect(res).toMatchSnapshot();
38+
39+
runInVmContext(res);
2340
});
2441

2542
it("with json", async () => {
2643
const filename = "app-export-default-with-json.jsx";
2744
const filePath = resolvePath(filename);
2845
const res = await bundle(filePath);
2946
expect(res).toMatchSnapshot();
47+
48+
runInVmContext(res);
3049
});
3150

3251
it("with txt", async () => {
3352
const filename = "app-export-default-with-txt.jsx";
3453
const filePath = resolvePath(filename);
3554
const res = await bundle(filePath);
3655
expect(res).toMatchSnapshot();
56+
57+
runInVmContext(res);
3758
});
3859

3960
it("with md", async () => {
4061
const filename = "app-export-default-with-md.jsx";
4162
const filePath = resolvePath(filename);
4263
const res = await bundle(filePath);
4364
expect(res).toMatchSnapshot();
65+
66+
runInVmContext(res);
4467
});
4568

4669
it("with images", async () => {
4770
const filename = "app-export-default-with-images.jsx";
4871
const filePath = resolvePath(filename);
4972
const res = await bundle(filePath);
5073
expect(res).toMatchSnapshot();
74+
75+
runInVmContext(res);
5176
});
5277

5378
it("with html", async () => {
5479
const filename = "app-export-default-with-html.jsx";
5580
const filePath = resolvePath(filename);
5681
const res = await bundle(filePath);
5782
expect(res).toMatchSnapshot();
83+
84+
runInVmContext(res);
5885
});
5986

6087
it("with wasm", async () => {
6188
const filename = "app-export-default-with-wasm.jsx";
6289
const filePath = resolvePath(filename);
6390
const res = await bundle(filePath);
6491
expect(res).toMatchSnapshot();
92+
93+
runInVmContext(res);
6594
});
6695

6796
it("with backticks", async () => {
6897
const filename = "app-export-default-with-backticks.jsx";
6998
const filePath = resolvePath(filename);
7099
const res = await bundle(filePath);
71100
expect(res).toMatchSnapshot();
101+
102+
runInVmContext(res);
72103
});
73104

74105
it("default (preact)", async () => {
75106
const filename = "app-export-default-preact.jsx";
76107
const filePath = resolvePath(filename);
77108
const res = await bundle(filePath);
78109
expect(res).toMatchSnapshot();
110+
111+
runInVmContext(res);
79112
});
80113
});

0 commit comments

Comments
 (0)