Skip to content

Commit

Permalink
chore: improve test case
Browse files Browse the repository at this point in the history
  • Loading branch information
shulandmimi committed Oct 13, 2024
1 parent 584ec1a commit ab0012d
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 21 deletions.
4 changes: 2 additions & 2 deletions e2e/default.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ describe('Default E2E Tests', async () => {
// const examples = ['react-ssr', 'solid-ssr', 'vue-ssr'];
logger(`Running E2E tests for ${examples.length} examples`);

console.log('exclude examples', excludeExamples);

for (const example of examples) {
const examplePath = join('./examples', example);
const hasE2eTestFile = existsSync(join(examplePath, 'e2e.spec.ts'));
Expand All @@ -25,8 +27,6 @@ describe('Default E2E Tests', async () => {
example,
'hasE2eTestFile',
hasE2eTestFile,
'excludeExamples',
excludeExamples,
'hasIndexHtml',
hasIndexHtml
);
Expand Down
9 changes: 4 additions & 5 deletions e2e/vitestGlobalSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ import { createServer, Server } from 'http';
let browserServer: BrowserServer | undefined;
let client: Server | undefined;

const buffer = new SharedArrayBuffer(2);
const u16 = new Uint16Array(buffer);
let port = 23000;

function addPort() {
return Atomics.add(u16, 0, 10);
return (port += 10);
}

function setPort(port: number) {
return Atomics.store(u16, 0, port);
function setPort(_port: number) {
return (port = _port);
}

setPort(9100);
Expand Down
32 changes: 23 additions & 9 deletions examples/react-fast-refresh/e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,46 @@ const projectPath = dirname(fileURLToPath(import.meta.url));

const delay = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));

const waitMatchConsole = (page: Page, text: string) => new Promise((resolve) => {
const waitMatchConsole = (page: Page, text: string, timeout = 5000) => new Promise((resolve, reject) => {
let timer: NodeJS.Timeout | null = setTimeout(() => {
reject('wait match console message timeout');
}, timeout);

let cleanTimer = () => {
if (timer) {
clearTimeout(timer);
timer = null;;
}
}
let handler = (message: ConsoleMessage) => {
console.log({ message })

if(message.text().includes(text)) {
cleanTimer();
resolve(undefined);
};

page.off('console', handler);
};


page.on('console', handler);
})

async function testFileHmr(page: Page, element: ElementHandle<SVGElement | HTMLElement>, filename: string, originText: string, afterText: string) {
async function expectTestFileHmr(page: Page, element: ElementHandle<SVGElement | HTMLElement>, filename: string, originText: string, afterText: string) {

const matchUpdateMessage = `[Farm HMR] ${normalize(filename)} updated`;

let waitClassUpdate = waitMatchConsole(page, matchUpdateMessage);
const waitUpdatePromise = waitMatchConsole(page, matchUpdateMessage);

const recover = await editFile(path.join(projectPath, filename), originText, afterText);

try {
await waitClassUpdate;
await delay(300);
await waitUpdatePromise;
await delay(1000);
expect((await element.textContent())).toContain(afterText);
} finally {
await recover?.()
}
await delay(300);
}

describe(`e2e tests - ${name}`, async () => {
Expand All @@ -55,9 +67,11 @@ describe(`e2e tests - ${name}`, async () => {

expect(content).toContain('function component');

await testFileHmr(page, root, './src/components/ClassC.tsx', 'class component', 'class component update');
await expectTestFileHmr(page, root, './src/components/ClassC.tsx', 'class component', 'class component update');

await delay(3000);

await testFileHmr(page, root, './src/components/FnC.tsx', 'function component', 'function component update');
await expectTestFileHmr(page, root, './src/components/FnC.tsx', 'function component', 'function component update');
},
command
);
Expand Down
21 changes: 16 additions & 5 deletions examples/server-proxy/e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,22 @@ async function launchServer() {
hello: 'world'
});
});
return new Promise((r) => {
server = app.listen(3000, () => {
console.log('server up');
r(null);
});

return new Promise((res,rej) => {
try {
let timer = setTimeout(() => {
rej('listen port 3000 timeout.');
}, 3000);

server = app.listen(3000, () => {
clearTimeout(timer);
console.log('server up');
res(null);
});

} catch (error) {
rej(error);
}
});
}

Expand Down

0 comments on commit ab0012d

Please sign in to comment.