From 243b45c8048c00b89c62102f9045f1f86fd552d9 Mon Sep 17 00:00:00 2001 From: Ryan Christian Date: Mon, 14 Mar 2022 19:39:37 -0500 Subject: [PATCH] test: Proxy request in watch mode --- packages/cli/tests/server.js | 7 ++++++- packages/cli/tests/subjects/proxy/index.js | 14 ++++++++++++++ .../cli/tests/subjects/proxy/package.json | 5 +++++ packages/cli/tests/watch.test.js | 19 +++++++++++++++++++ 4 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 packages/cli/tests/subjects/proxy/index.js create mode 100644 packages/cli/tests/subjects/proxy/package.json diff --git a/packages/cli/tests/server.js b/packages/cli/tests/server.js index b0fbef8a3..abbae9672 100644 --- a/packages/cli/tests/server.js +++ b/packages/cli/tests/server.js @@ -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); }; diff --git a/packages/cli/tests/subjects/proxy/index.js b/packages/cli/tests/subjects/proxy/index.js new file mode 100644 index 000000000..e154e7db6 --- /dev/null +++ b/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

Data retrieved from proxied server: {val}

; +}; diff --git a/packages/cli/tests/subjects/proxy/package.json b/packages/cli/tests/subjects/proxy/package.json new file mode 100644 index 000000000..090ae9420 --- /dev/null +++ b/packages/cli/tests/subjects/proxy/package.json @@ -0,0 +1,5 @@ +{ + "private": true, + "name": "preact-proxy", + "proxy": "http://localhost:8086" +} diff --git a/packages/cli/tests/watch.test.js b/packages/cli/tests/watch.test.js index bff7f6945..9dbf49e01 100644 --- a/packages/cli/tests/watch.test.js +++ b/packages/cli/tests/watch.test.js @@ -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; @@ -34,6 +36,23 @@ describe('preact', () => { server.close(); }); + + it.only('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', () => {