Skip to content

Commit 1dd049d

Browse files
wip: refactor to local server
Signed-off-by: Victor Adossi <vadossi@cosmonic.com>
1 parent 5c3444a commit 1dd049d

File tree

3 files changed

+30
-19
lines changed

3 files changed

+30
-19
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/builtins/fetch.js

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,39 @@
11
import { strictEqual, ok } from 'node:assert';
2+
import { getRandomPort } from "../util";
23

3-
const FETCH_URL = 'https://httpbin.org/anything';
4+
const FETCH_URL = 'http://localhost';
45

5-
export const source = `
6+
export const source = (port) => {
7+
let port = port ? ':' + port : '';
8+
return `
69
export async function run () {
7-
const res = await fetch('${FETCH_URL}');
10+
const res = await fetch('${FETCH_URL}${port}');
811
const source = await res.json();
912
console.log(source.url);
1013
}
1114
export function ready () {
1215
return true;
1316
}
1417
`;
18+
};
1519

1620
export const enableFeatures = ['http'];
1721

1822
export async function test(run) {
19-
let retries = 3;
20-
let stdout, stderr;
21-
while (retries > 0) {
22-
try {
23-
const result = await run();
24-
stdout = result.stdout;
25-
stderr = result.stderr;
26-
break;
27-
} catch (err) {
28-
console.error('failed to fetch URL', err);
29-
}
30-
retries -= 1;
31-
}
23+
const port = await getRandomPort();
24+
25+
// Run a local server on some port
26+
const server = createServer(async (req, res) => {
27+
res.writeHead(200, { "Content-Type": "application/json; charset=utf-8" });
28+
res.write(JSON.stringify({
29+
status: "ok",
30+
}));
31+
res.end();
32+
}).listen(port);
33+
34+
const { stdout, stderr } = await run({
35+
sourceFnArgs: [ port ],
36+
});
3237
strictEqual(stderr, '');
3338
strictEqual(stdout.trim(), FETCH_URL);
3439
}

test/test.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ suite('Builtins', () => {
3131
enableFeatures,
3232
} = await import(`./builtins/${filename}`);
3333

34+
// If the source was
35+
// TODO: need the args here
36+
if (typeof source === 'function') {
37+
source(config.sourceArgs);
38+
}
39+
3440
const { component } = await componentize(
3541
source,
3642
`
@@ -80,7 +86,7 @@ suite('Builtins', () => {
8086
);
8187

8288
try {
83-
await runTest(async function run() {
89+
await runTest(async function run(options) {
8490
let stdout = '',
8591
stderr = '',
8692
timeout;

0 commit comments

Comments
 (0)