Skip to content

Commit

Permalink
Merge pull request #3160 from Zemnmez/next-server
Browse files Browse the repository at this point in the history
Implement next dev server and tests.
  • Loading branch information
Zemnmez authored May 30, 2023
2 parents e1ee026 + b28a94a commit a67f085
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 3 deletions.
3 changes: 1 addition & 2 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,12 @@ rules_js_dependencies()

load("@rules_nodejs//nodejs:repositories.bzl", "DEFAULT_NODE_VERSION", "nodejs_register_toolchains")


# force node version to be above 18
# this should eventually be loaded from package.json engines.

nodejs_register_toolchains(
name = "nodejs",
node_version = DEFAULT_NODE_VERSION if int(DEFAULT_NODE_VERSION.split(".")[0]) > 18 else "18.13.0"
node_version = DEFAULT_NODE_VERSION if int(DEFAULT_NODE_VERSION.split(".")[0]) > 18 else "18.13.0",
)

load("@aspect_rules_js//npm:npm_import.bzl", "npm_translate_lock")
Expand Down
6 changes: 6 additions & 0 deletions ts/next.js/rules.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ def next_project(name, srcs, **kwargs):
output_dir = True,
)

bin.next_binary(
name = "dev",
data = srcs,
args = ["dev", native.package_name()],
)

bin.next(
name = "out",
srcs = [":build"] + srcs,
Expand Down
16 changes: 15 additions & 1 deletion ts/next.js/testing/example/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("//ts:rules.bzl", "ts_project")
load("//ts:rules.bzl", "jest_test", "ts_project")
load("//ts/next.js:rules.bzl", "next_project")
load("@npm//:next/package_json.bzl", next = "bin")

Expand All @@ -10,6 +10,7 @@ ts_project(
"//:node_modules/@types/react",
"//:node_modules/react",
"//:node_modules/react-dom",
"//ts",
"//ts/math",
],
)
Expand All @@ -18,3 +19,16 @@ next_project(
name = "example",
srcs = [":ts_sources"],
)

jest_test(
name = "test_running_dev_server",
srcs = ["launch_server_test.js"],
data = [
":dev",
":ts_sources",
],
env = {
"NEXT_SERVER_BINARY": "../../../$(location :dev)",
"BAZEL_BINDIR": "$(BINDIR)",
},
)
41 changes: 41 additions & 0 deletions ts/next.js/testing/example/launch_server_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import child_process from 'node:child_process';
import http from 'node:http';
import * as readline from 'node:readline/promises';

import { isDefined, isNotNull, must } from 'ts/guard';

test('next.js dev server launch!', async () => {
const next_server_binary = 'ts/next.js/testing/example/dev.sh';
const BAZEL_BINDIR = must(isDefined)(process?.env?.BAZEL_BINDIR);

const proc = child_process.execFile(next_server_binary, {
env: { BAZEL_BINDIR },
});

console.info('Waiting for process to go up...');

await new Promise((ok, err) => {
proc.once('error', err);
proc.once('spawn', ok);
});

const output: NodeJS.WritableStream = must(isNotNull)(proc.stdin);
const input: NodeJS.ReadableStream = must(isNotNull)(proc.stdout);

for await (const line of readline.createInterface({
input, // not really needed, we're not asking Qs.
output,
})) {
console.info(line);
const m = /https:\/\/localhost:\d+/g.exec(line);
if (m?.[0]) {
// attempt to connect to the port
const resp: http.IncomingMessage = await new Promise(ok =>
http.get(m?.[0], resp => ok(resp))
);
expect(resp.statusCode).toBe(200);

break;
}
}
});

0 comments on commit a67f085

Please sign in to comment.