Skip to content

Commit

Permalink
chore: add ssr e2e tests (#1201)
Browse files Browse the repository at this point in the history
Co-authored-by: brightwwu <[email protected]>
  • Loading branch information
wre232114 and brightwwu authored Apr 17, 2024
1 parent 7c2884f commit b29c614
Show file tree
Hide file tree
Showing 31 changed files with 217 additions and 158 deletions.
5 changes: 5 additions & 0 deletions .changeset/tidy-swans-love.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@farmfe/core': patch
---

Fix loadConfig error message
4 changes: 2 additions & 2 deletions crates/plugin_runtime/src/render_resource_pot/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ mod source_replacer;
/// ```js
/// {
/// // commonjs or hybrid module system
/// "a.js": async function(module, exports, require) {
/// const b = await require('./b');
/// "a.js": function(module, exports, require) {
/// const b = require('./b');
/// console.log(b);
/// },
/// // esm module system
Expand Down
10 changes: 3 additions & 7 deletions e2e/default.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,13 @@ import { existsSync, readdirSync, statSync } from 'fs';
import { join } from 'path';
import { startProjectAndTest } from './vitestSetup.js';
import { logger } from './utils.js';
// import { ssrExamples } from './test-utils.js';

const excludeExamples: string[] = [
// TODO: add e2e test for ssr
'react-ssr',
'vue-ssr',
'solid-ssr'
];
const excludeExamples: string[] = [];

test('Default E2E Tests', async () => {
const examples = readdirSync('./examples');
// const examples = ['x-data-spreadsheet'];
// const examples = ['react-ssr', 'solid-ssr', 'vue-ssr'];
logger(`Running E2E tests for ${examples.length} examples`);

for (const example of examples) {
Expand Down
7 changes: 2 additions & 5 deletions e2e/test-utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
export * from './vitestSetup.js';

export * from './vitestSetup'




export const ssrExamples = ['react-ssr', 'vue-ssr', 'solid-ssr'];
14 changes: 7 additions & 7 deletions e2e/vitestSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,15 @@ export const startProjectAndTest = async (
cb: (page: Page) => Promise<void>,
command = 'start'
) => {
// using bin path to spawn child process to avoid port conflicts issue
const cliBinPath = getFarmCLIBinPath(examplePath);
// // using bin path to spawn child process to avoid port conflicts issue
// const cliBinPath = getFarmCLIBinPath(examplePath);

if (!cliBinPath) {
throw new Error(`example ${examplePath} does not install @farmfe/cli`);
}
// if (!cliBinPath) {
// throw new Error(`example ${examplePath} does not install @farmfe/cli`);
// }
const port = getServerPort();
logger(`Executing node ${cliBinPath} ${command} in ${examplePath}`);
const child = execa('node', [cliBinPath, command], {
logger(`Executing npm run ${command} in ${examplePath}`);
const child = execa('npm', ['run', command], {
cwd: examplePath,
stdin: 'pipe',
encoding: 'utf8',
Expand Down
3 changes: 2 additions & 1 deletion examples/css-modules/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
},
"scripts": {
"start": "farm start",
"build": "farm build"
"build": "farm build",
"preview": "farm preview"
}
}
3 changes: 2 additions & 1 deletion examples/less/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
},
"scripts": {
"start": "farm start",
"build": "farm build"
"build": "farm build",
"preview": "farm preview"
}
}
5 changes: 3 additions & 2 deletions examples/qrcode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
"private": true,
"scripts": {
"dev": "farm start",
"start": "farm start",
"build": "farm build",
"preview": "farm build"
"preview": "farm preview"
},
"devDependencies": {
"@farmfe/cli": "workspace:^",
Expand All @@ -14,4 +15,4 @@
"dependencies": {
"qrcode": "^1.5.3"
}
}
}
3 changes: 2 additions & 1 deletion examples/react-sass-js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
},
"scripts": {
"start": "farm start",
"build": "farm build"
"build": "farm build",
"preview": "farm preview"
}
}
11 changes: 8 additions & 3 deletions examples/react-ssr/farm.config.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import path from 'path';

/**
* @type {import('@farmfe/core').UserConfig}
*/
Expand Down Expand Up @@ -35,10 +37,13 @@ export default {
.getCompiler()
.resource('index_client.html')
.toString();
console.log('html template', template);
const render = await import('./dist/index.js').then(
(m) => m.default
// console.log('html template', template);
console.log(
path.join(path.dirname(import.meta.url), 'dist', 'index.js')
);
const render = await import(
path.join(path.dirname(import.meta.url), 'dist', 'index.js')
).then((m) => m.default);
const renderedHtml = render(ctx.path);
// console.log(renderedHtml);
const html = template.replace(
Expand Down
2 changes: 1 addition & 1 deletion examples/react-ssr/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@
"watch": "farm watch --config farm.config.server.mjs",
"build": "pnpm build:server && farm build",
"build:server": "farm build --config farm.config.server.mjs",
"preview": "farm preview"
"preview": "node server.js"
}
}
13 changes: 6 additions & 7 deletions examples/react-ssr/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,13 @@ async function createServer() {
let render;

template = await fsp.readFile(resolve('build/index_client.html'), 'utf8');
render = require(resolve('dist/index.js'));
const serverEntry = resolve('dist/index.js');
render = require(serverEntry);

const renderedHtml = render(url);
console.log(renderedHtml);

let html = template.replace(
'{app-html-to-replace}',
renderedHtml
);
let html = template.replace('{app-html-to-replace}', renderedHtml);
console.log(template.includes('{app-html-to-replace}'));
console.log(html.includes('{app-html-to-replace}'));

Expand All @@ -43,7 +41,8 @@ async function createServer() {
}

createServer().then((app) => {
app.listen(3000, () => {
console.log('HTTP server is running at http://localhost:3000');
const port = process.env.FARM_DEFAULT_SERVER_PORT || 3000;
app.listen(port, () => {
console.log('HTTP server is running at http://localhost:' + port);
});
});
5 changes: 3 additions & 2 deletions examples/sass-plugin-rust/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
},
"scripts": {
"start": "farm start",
"build": "farm build"
"build": "farm build",
"preview": "farm preview"
}
}
}
3 changes: 2 additions & 1 deletion examples/sass/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
},
"scripts": {
"start": "farm start",
"build": "farm build"
"build": "farm build",
"preview": "farm preview"
}
}
18 changes: 11 additions & 7 deletions examples/solid-ssr/farm.config.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import solid from 'vite-plugin-solid';
import { generateHydrationScript } from 'solid-js/web';
import path from 'path';
import { pathToFileURL } from 'url';

/**
* @type {import('@farmfe/core').UserConfig}
Expand All @@ -14,13 +16,11 @@ export default {
},
// sourcemap: true,
css: {
// modules: {
// indentName: 'farm-[name]-[hash]'
// },
prefixer: {
targets: ['last 2 versions', 'Firefox ESR', '> 1%', 'ie >= 11']
}
},
// minify: false,
persistentCache: false
// treeShaking: true,
// minify: true,babcl
Expand All @@ -39,14 +39,18 @@ export default {
.getCompiler()
.resource('client.html')
.toString();
const render = await import('./dist/index.js').then(
(m) => m.default
);
const curDir =
process.platform === 'win32'
? pathToFileURL(__dirname)
: __dirname;
const render = await import(
path.join(curDir.toString(), 'dist', 'index.js')
).then((m) => m.default);
const renderedHtml = render(ctx.path);

const html = template
.replace('<div>app-html-to-replace</div>', renderedHtml)
.replace('</head>', generateHydrationScript());
.replace('<meta hydration />', generateHydrationScript());
console.log(renderedHtml);
ctx.body = html;
ctx.type = 'text/html';
Expand Down
1 change: 1 addition & 0 deletions examples/solid-ssr/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<meta hydration />
</head>
<body>
<div id="root"><div>app-html-to-replace</div></div>
Expand Down
2 changes: 1 addition & 1 deletion examples/solid-ssr/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@
"watch": "farm watch --config farm.config.server.mjs",
"build": "pnpm build:server && farm build",
"build:server": "farm build --config farm.config.server.mjs",
"preview": "farm preview"
"preview": "node server.js"
}
}
19 changes: 13 additions & 6 deletions examples/solid-ssr/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import path from 'path';
import fsp from 'fs/promises';
import express from 'express';
import { generateHydrationScript } from 'solid-js/web';
import { pathToFileURL } from 'url';

function resolve(p) {
return path.resolve(p);
Expand All @@ -17,15 +18,20 @@ async function createServer() {

try {
let template = await fsp.readFile(resolve('build/client.html'), 'utf8');
let render = (await import(resolve('dist/index.js'))).default;
const serverEntry = resolve('dist/index.js');
let render = (
await import(
process.platform === 'win32'
? pathToFileURL(serverEntry)
: serverEntry
)
).default;

const renderedHtml = render(url);

let html = template
.replace('<div>app-html-to-replace</div>', renderedHtml)
.replace('</head>', generateHydrationScript());
console.log(template.includes('<div>app-html-to-replace</div>'));
console.log(html.includes('<div>app-html-to-replace</div>'));
.replace('<meta hydration>', generateHydrationScript());

res.setHeader('Content-Type', 'text/html');
return res.status(200).end(html);
Expand All @@ -39,7 +45,8 @@ async function createServer() {
}

createServer().then((app) => {
app.listen(3000, () => {
console.log('HTTP server is running at http://localhost:3000');
const port = process.env.FARM_DEFAULT_SERVER_PORT || 3000;
app.listen(port, () => {
console.log('HTTP server is running at http://localhost:' + port);
});
});
3 changes: 2 additions & 1 deletion examples/tailwind/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
},
"scripts": {
"start": "farm start",
"build": "farm build"
"build": "farm build",
"preview": "farm preview"
}
}
3 changes: 2 additions & 1 deletion examples/vue-jsx/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
},
"scripts": {
"start": "farm start",
"build": "farm build"
"build": "farm build",
"preview": "farm preview"
}
}
52 changes: 0 additions & 52 deletions examples/vue-ssr/farm.config.mjs

This file was deleted.

Loading

0 comments on commit b29c614

Please sign in to comment.