Skip to content

Commit 516f13a

Browse files
committed
fix: remove mock-fs usage
1 parent 7fb18a4 commit 516f13a

17 files changed

+68
-139
lines changed

package-lock.json

-16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
"conventional-changelog-cli": "^2.2.2",
4545
"eslint": "^8.35.0",
4646
"husky": "^8.0.0",
47-
"mock-fs": "^5.2.0",
4847
"prettier": "^2.8.6",
4948
"react": "^18.2.0",
5049
"react-dom": "^18.2.0",

src/vite-lifecycle.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,16 @@ export function prepareTempEntries(
3030
}
3131
let htmlContent: string;
3232
try {
33+
console.log(__dirname)
3334
htmlContent = readFileSync(
3435
pageData.template ||
35-
"node_modules/vite-plugin-auto-mpa-html/assets/index.html",
36+
path.resolve(__dirname, "..", "assets", "index.html"),
3637
{
3738
encoding: "utf-8",
3839
}
3940
);
4041
} catch (e) {
42+
console.log(e)
4143
if (isErrorOfNotFound(e)) {
4244
e.message = `Page entry: ${pluginOption.sourceDir}/${k}, its template (${pageData.template}) cannot be found, please check! (${e.message})`
4345
}
@@ -96,7 +98,7 @@ export function devServerMiddleware(pluginOption: PluginOption) {
9698
const pageConfig: PagePluginConfig = JSON.parse(temp);
9799
let htmlContent = readFileSync(
98100
pageConfig.template ||
99-
"node_modules/vite-plugin-auto-mpa-html/assets/index.html",
101+
path.resolve(__dirname, "assets", "index.html"),
100102
{
101103
encoding: "utf-8",
102104
}

tests/entry.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ const pluginOption: PluginOption = {
1414
describe("Test base function - getEntryKV", () => {
1515
it("should count entries correctly", () => {
1616
const data = getEntryKV(pluginOption);
17-
expect(path.relative(__dirname, data["page1"])).toBe("../page1.html"); // generated to root dir
18-
expect(path.relative(__dirname, data["page2"])).toBe("../page2.html");
17+
expect(path.relative(__dirname, data["normal"])).toBe("../normal.html"); // generated to root dir
18+
expect(path.relative(__dirname, data["no-template"])).toBe("../no-template.html");
1919
expect(Object.keys(data).length).toBe(3);
2020
});
2121
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"data": {
3+
"title": "No template defined page"
4+
}
5+
}
File renamed without changes.

tests/fixtures/src/normal/config.json

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"template": "tests/fixtures/templates/tpl.html",
3+
"data": {
4+
"title": "Something else that not match tpl's title"
5+
}
6+
}

tests/fixtures/src/page1/config.json

-3
This file was deleted.

tests/fixtures/src/page2/config.json

-3
This file was deleted.
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<title><%= title %></title>
8+
</head>
9+
10+
<body>
11+
<div id="root"></div>
12+
</body>
13+
14+
</html>

tests/server.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ describe("Test plugin's lifecycle - devServer", async () => {
3131
tmp.use(viteServer.middlewares);
3232
tmp.use(devServerMiddleware(pluginOption));
3333

34-
let res = await request(tmp).get("/src/page1/assets/index.css");
34+
let res = await request(tmp).get("/src/normal/assets/index.css");
3535
expect(res.text).toMatch(":root{background-color:#fff}");
36-
res = await request(tmp).get("/page2.html");
36+
res = await request(tmp).get("/normal.html");
3737
expect(res.text).toMatch("<title>Minimal React Vite Project</title>");
3838
expect(res.text).toMatch(
39-
`<script type="module" src="./${pluginOption.sourceDir}/page2/${pluginOption.entryName}"></script>`
39+
`<script type="module" src="./${pluginOption.sourceDir}/normal/${pluginOption.entryName}"></script>`
4040
);
4141
});
4242

tests/vite-feature.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// This test is designed for Vite native features, related to HTML replacement.
2-
import { describe, it, expect } from "vitest";
2+
import { describe, it, expect, expectTypeOf } from "vitest";
33
import react from '@vitejs/plugin-react'
44
import autoMpaHTMLPlugin from '../index.js'
55
import { build } from "vite";

tests/vite-lifecycle.test.ts

+33-108
Original file line numberDiff line numberDiff line change
@@ -7,144 +7,70 @@ import {
77
beforeEach,
88
afterEach,
99
} from "vitest";
10-
import mock from "mock-fs";
1110
import fs from "node:fs";
1211
// Test functions
1312
import { prepareTempEntries, cleanTempEntries } from "../src/vite-lifecycle.js";
1413
import { PluginOption } from "../src/types.js";
1514

1615
const pluginOption: PluginOption = {
17-
sourceDir: "src",
16+
sourceDir: "tests/fixtures/src",
1817
configName: "config.json",
19-
entryName: "main.js",
18+
entryName: "main.jsx",
2019
sharedData: {},
2120
ejsOption: {},
2221
};
2322

2423
const dest = "dist";
2524
const entriesKV = {
26-
page1: "",
27-
page2: "",
28-
};
29-
const noTemplateDefinedProjectConstruct = {
30-
"node_modules": mock.load(
31-
path.resolve(__dirname, '..', "node_modules")
32-
),
33-
"node_modules/vite-plugin-auto-mpa-html/assets/index.html": mock.load(
34-
path.resolve(__dirname, "..", "assets/index.html")
35-
),
36-
src: {
37-
page1: {
38-
"config.json": '{"data":{"title":"Page 1"}}',
39-
"main.js": "",
40-
},
41-
page2: {
42-
"config.json": '{"data":{"title":"Page 2"}}',
43-
"main.js": "",
44-
},
45-
},
46-
};
47-
const commonProjectConstruct = {
48-
"node_modules": mock.load(
49-
path.resolve(__dirname, '..', "node_modules")
50-
),
51-
"node_modules/vite-plugin-auto-mpa-html/assets/index.html": mock.load(
52-
path.resolve(__dirname, "..", "assets/index.html")
53-
),
54-
src: {
55-
page1: {
56-
assets: {
57-
"index.css": ":root{background-color:#fff}",
58-
},
59-
"config.json":
60-
'{"data":{"title":"Page 1"},"template":"templates/mobile.html"}',
61-
"main.js": "",
62-
},
63-
page2: {
64-
"config.json":
65-
'{"data":{"title":"Page 2"},"template":"templates/index.html"}',
66-
"main.js": "",
67-
},
68-
},
69-
templates: {
70-
"mobile.html": `<!DOCTYPE html>
71-
<html lang="en">
72-
<head>
73-
<meta charset="UTF-8" />
74-
<link rel="icon" type="image/icon" href="/favicon.ico" />
75-
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
76-
<title>templates/mobile.html</title>
77-
</head>
78-
<body>
79-
<div id="app"></div>
80-
</body>
81-
</html>`,
82-
"index.html": `<!DOCTYPE html>
83-
<html lang="en">
84-
<head>
85-
<meta charset="UTF-8" />
86-
<link rel="icon" type="image/icon" href="/favicon.ico" />
87-
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
88-
<title>templates/index.html</title>
89-
</head>
90-
<body>
91-
<div id="app"></div>
92-
</body>
93-
</html>`,
94-
},
95-
"vite.config.js": `export default {{
96-
root: __dirname,
97-
}}`,
98-
};
25+
normal: "",
26+
"no-template": "",
27+
"html-env-replacement": ""
28+
}
9929

10030
describe("Test plugin's lifecycle - buildStart", () => {
10131
afterEach(() => {
102-
mock.restore();
10332
vi.restoreAllMocks();
10433
});
10534

10635
it("common project struct, should use user defined template", () => {
107-
mock(commonProjectConstruct);
10836
// Do a generation
10937
const generatedKeys = prepareTempEntries(pluginOption, dest, entriesKV);
11038
expect(Array.isArray(generatedKeys)).toBe(true);
11139
expect(generatedKeys.length).toBe(Object.keys(entriesKV).length);
112-
generatedKeys.forEach((key) => {
113-
const filePath = path.join(`${key}.html`);
114-
const configPath = path.join(
115-
`${pluginOption.sourceDir}/${key}/${pluginOption.configName}`
116-
);
117-
let configData = fs.readFileSync(configPath, { encoding: "utf-8" });
118-
expect(fs.existsSync(filePath)).toBe(true); // temporary generated an entry HTML for build
119-
const fileContent = fs.readFileSync(filePath, { encoding: "utf-8" });
120-
expect(fileContent).toMatch("</html>"); // correctly end html doc.
121-
expect(fileContent).toMatch(
122-
`<title>${JSON.parse(configData).template}</title>`
123-
); // correctly render ejs template with given data
124-
expect(fileContent).toMatch(
125-
`<script type="module" src="./${pluginOption.sourceDir}/${key}/${pluginOption.entryName}"></script>`
126-
); // contain needed entry module
127-
});
40+
const filePath = path.join(`normal.html`);
41+
const configPath = path.join(
42+
`${pluginOption.sourceDir}/normal/${pluginOption.configName}`
43+
);
44+
expect(fs.existsSync(filePath)).toBe(true); // temporary generated an entry HTML for build
45+
const fileContent = fs.readFileSync(filePath, { encoding: "utf-8" });
46+
expect(fileContent).toMatch("</html>"); // correctly end html doc.
47+
expect(fileContent).toMatch(
48+
`<title>Minimal React Vite Project</title>`
49+
); // correctly render ejs template with given data
50+
expect(fileContent).toMatch(
51+
`<script type="module" src="./${pluginOption.sourceDir}/normal/${pluginOption.entryName}"></script>`
52+
); // contain needed entry module
12853
});
12954

13055
it("when template is not defined, should fallback to package preset `index.html`", () => {
131-
mock(noTemplateDefinedProjectConstruct);
13256
// Do a generation
13357
const generatedKeys = prepareTempEntries(pluginOption, dest, entriesKV);
13458
expect(Array.isArray(generatedKeys)).toBe(true);
13559
expect(generatedKeys.length).toBe(Object.keys(entriesKV).length);
136-
generatedKeys.forEach((key) => {
137-
const filePath = path.join(`${key}.html`);
138-
const configPath = path.join(
139-
`${pluginOption.sourceDir}/${key}/${pluginOption.configName}`
140-
);
141-
let configData = fs.readFileSync(configPath, { encoding: "utf-8" });
142-
expect(fs.existsSync(filePath)).toBe(true); // temporary generated an entry HTML for build
143-
const fileContent = fs.readFileSync(filePath, { encoding: "utf-8" });
144-
expect(fileContent).toMatch(
145-
`<title>${JSON.parse(configData).data.title}</title>`
146-
); // correctly render ejs template with given data
147-
});
60+
const filePath = path.join(`no-template.html`);
61+
const configPath = path.join(
62+
`${pluginOption.sourceDir}/no-template/${pluginOption.configName}`
63+
);
64+
let configData = fs.readFileSync(configPath, { encoding: "utf-8" });
65+
expect(fs.existsSync(filePath)).toBe(true); // temporary generated an entry HTML for build
66+
const fileContent = fs.readFileSync(filePath, { encoding: "utf-8" });
67+
expect(fileContent).toMatch("</html>"); // correctly end html doc.
68+
expect(fileContent).toMatch(
69+
`<title>${JSON.parse(configData).data.title}</title>`
70+
); // correctly render ejs template with given data
71+
expect(fileContent).toMatch(
72+
`<script type="module" src="./${pluginOption.sourceDir}/no-template/${pluginOption.entryName}"></script>`
73+
); // contain needed entry module
14874
});
14975

15076
// Skip EJS Option tests
@@ -153,7 +79,6 @@ describe("Test plugin's lifecycle - buildStart", () => {
15379
describe("Test plugin's lifecycle - buildEnd", () => {
15480
let generatedKeys: string[];
15581
beforeEach(() => {
156-
mock(commonProjectConstruct);
15782
// pre generate file struct
15883
generatedKeys = prepareTempEntries(pluginOption, dest, entriesKV);
15984
});

0 commit comments

Comments
 (0)