Skip to content

Commit

Permalink
test: Proxy request in watch mode
Browse files Browse the repository at this point in the history
  • Loading branch information
rschristian committed Mar 15, 2022
1 parent 3245624 commit 69e119c
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
7 changes: 6 additions & 1 deletion packages/cli/tests/server.js
Expand Up @@ -6,5 +6,10 @@ exports.getServer = (dir, port = 3000) => {
maxAge: 0,
single: true,
};
return polka().use(sirv(dir, opts)).listen(port);
return polka()
.use(sirv(dir, opts))
.get('/proxied/request', (_req, res) => {
res.end('Hello World!');
})
.listen(port);
};
14 changes: 14 additions & 0 deletions packages/cli/tests/subjects/proxy/index.js
@@ -0,0 +1,14 @@
import { h } from 'preact';
import { useEffect, useState } from 'preact/hooks';

export default () => {
const [val, setVal] = useState('');

useEffect(() => {
fetch('/proxied/request')
.then(res => res.text())
.then(data => setVal(data));
}, []);

return <h1>Data retrieved from proxied server: {val}</h1>;
};
5 changes: 5 additions & 0 deletions packages/cli/tests/subjects/proxy/package.json
@@ -0,0 +1,5 @@
{
"private": true,
"name": "preact-proxy",
"proxy": "http://localhost:8086"
}
19 changes: 19 additions & 0 deletions packages/cli/tests/watch.test.js
Expand Up @@ -3,6 +3,8 @@ const { resolve } = require('path');
const startChrome = require('./lib/chrome');
const { create, watch } = require('./lib/cli');
const { determinePort } = require('../lib/commands/watch');
const { subject } = require('./lib/output');
const { getServer } = require('./server');

const { loadPage, waitUntilExpression } = startChrome;
let chrome, server;
Expand Down Expand Up @@ -34,6 +36,23 @@ describe('preact', () => {

server.close();
});

it('should proxy requests when "proxy" exists in package.json', async () => {
const api = getServer('', 8086);
let app = await subject('proxy');

server = await watch(app, 8087);

let page = await loadPage(chrome, 'http://127.0.0.1:8087/');

await waitUntilExpression(
page,
`document.querySelector('h1').innerText === 'Data retrieved from proxied server: Hello World!'`
);

server.close();
api.server.close();
});
});

describe('should determine the correct port', () => {
Expand Down

0 comments on commit 69e119c

Please sign in to comment.