Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions build/wd_test.bzl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
load("@aspect_rules_ts//ts:defs.bzl", "ts_project")

def wd_test(
src,
data = [],
Expand All @@ -11,23 +13,40 @@ def wd_test(
specified.) The extension `.wd-test` is also permitted instead of `.capnp`, in order to
avoid confusing other build systems that may assume a `.capnp` file should be compiled. As
an extension, `.gpu-wd-test` is supported to enable special handling for GPU tests.
data: Files which the .capnp config file may embed. Typically JavaScript files.
data: Additional files which the .capnp config file may embed. All TypeScript files will be compiled,
their resulting files will be passed to the test as well. Usually TypeScript or Javascript source files.
args: Additional arguments to pass to `workerd`. Typically used to pass `--experimental`.
"""

# Add workerd binary to "data" dependencies.
data = data + [src, "//src/workerd/server:workerd"]

ts_srcs = [src for src in data if src.endswith(".ts")]

# Default name based on src.
if name == None:
name = src.removesuffix(".capnp").removesuffix(".wd-test").removesuffix(".gpu-wd-test").removesuffix(".ts-wd-test")

if len(ts_srcs) != 0:
# TODO When TypeScript 5.6 comes out use noCheck so the test fails throwing a type error.
ts_project(
name = name + "@ts_project",
srcs = ts_srcs,
tsconfig = "tsconfig.json",
allow_js = True,
source_map = True,
composite = True,
deps = ["//src/node:node.capnp@tsproject"],
)
data += [js_src.removesuffix(".ts") + ".js" for js_src in ts_srcs]

# Add initial arguments for `workerd test` command.
args = [
"$(location //src/workerd/server:workerd)",
"test",
"$(location {})".format(src),
] + args

# Default name based on src.
if name == None:
name = src.removesuffix(".capnp").removesuffix(".wd-test").removesuffix(".gpu-wd-test")

_wd_test(
name = name,
Expand Down
1 change: 1 addition & 0 deletions build/wd_ts_bundle.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def wd_ts_bundle_capnp(
declaration = True,
tsconfig = name + "@tsconfig",
deps = deps,
visibility = ["//visibility:public"],
)

data = wd_js_bundle_capnp(
Expand Down
8 changes: 8 additions & 0 deletions src/workerd/api/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,14 @@ wd_test(
data = ["tests/js-rpc-test.js"],
)

[wd_test(
src = f,
data = [f.removesuffix(".ts-wd-test") + ".ts"],
args = ["--experimental"],
) for f in glob(
["**/*.ts-wd-test"],
)]

# Enable GPU tests if experimental GPU support is enabled. Unfortunately, this depends on the right
# drivers being available on Linux and macOS, so turn it off by default. Run GPU tests with
# `bazel test //src/workerd/api:gpu/<test name>`.
Expand Down
54 changes: 54 additions & 0 deletions src/workerd/api/http-test-ts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright (c) 2024 Cloudflare, Inc.
// Licensed under the Apache 2.0 license found in the LICENSE file or at:
// https://opensource.org/licenses/Apache-2.0

import assert from "node:assert";

async function assertRequestCacheThrowsError(cacheHeader: RequestCache,
errorName: String = 'Error',
errorMessage: String = "The 'cache' field on 'RequestInitializerDict' is not implemented.") {
assert.throws(() => {
const header = { cache : cacheHeader};
const req: RequestInit = header;
new Request('https://example.org', req);
}, {
name: errorName,
message: errorMessage,
});
}

async function assertFetchCacheRejectsError(cacheHeader: RequestCache,
errorName: String = 'Error',
errorMessage: String = "The 'cache' field on 'RequestInitializerDict' is not implemented.") {
await assert.rejects((async () => {
const header = { cache : cacheHeader};
const req: RequestInit = header;
await fetch('http://example.org', req);
})(), {
name: errorName,
message: errorMessage,
});
}

export const cacheMode = {

async test() {
assert.strictEqual("cache" in Request.prototype, false);
{
const req = new Request('https://example.org', {});
assert.strictEqual(req.cache, undefined);
}
assertRequestCacheThrowsError('default');
assertRequestCacheThrowsError('force-cache');
assertRequestCacheThrowsError('no-cache');
assertRequestCacheThrowsError('no-store');
assertRequestCacheThrowsError('only-if-cached');
assertRequestCacheThrowsError('reload');
assertFetchCacheRejectsError('default');
assertFetchCacheRejectsError('force-cache');
assertFetchCacheRejectsError('no-cache');
assertFetchCacheRejectsError('no-store');
assertFetchCacheRejectsError('only-if-cached');
assertFetchCacheRejectsError('reload');
}
}
18 changes: 18 additions & 0 deletions src/workerd/api/http-test-ts.ts-wd-test
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Workerd = import "/workerd/workerd.capnp";

const unitTests :Workerd.Config = (
services = [
( name = "http-test",
worker = (
modules = [
( name = "worker", esModule = embed "http-test-ts.js" )
],
bindings = [
( name = "SERVICE", service = "http-test" )
],
compatibilityDate = "2023-08-01",
compatibilityFlags = ["nodejs_compat", "service_binding_extra_handlers"],
)
),
],
);
22 changes: 22 additions & 0 deletions src/workerd/api/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"compilerOptions": {
"target": "esnext",
"lib": ["esnext", "webworker"],
"strict": true,
"moduleResolution": "node",
"esModuleInterop": true,
"outDir": "dist",
"sourceMap": true,
"baseUrl": ".",
"rootDir": ".",
"paths": {
"@workerd/*": ["../../bazel-bin/src/workerd/*"]
},
"checkJs": true,
"allowJs": true,
"composite": true,
"skipLibCheck": true
},
"include": ["**/*.ts"],
"exclude": [],
}
1 change: 1 addition & 0 deletions types/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ js_run_binary(
out_dirs = ["definitions"],
silent_on_success = False, # Always enable logging for debugging
tool = ":build_types_bin",
visibility = ["//visibility:public"],
)

js_binary(
Expand Down