Skip to content

Commit dd7e463

Browse files
authored
Create typescript tests for apis (#2480)
1 parent 2b3012b commit dd7e463

File tree

7 files changed

+127
-4
lines changed

7 files changed

+127
-4
lines changed

build/wd_test.bzl

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
load("@aspect_rules_ts//ts:defs.bzl", "ts_project")
2+
13
def wd_test(
24
src,
35
data = [],
@@ -11,23 +13,40 @@ def wd_test(
1113
specified.) The extension `.wd-test` is also permitted instead of `.capnp`, in order to
1214
avoid confusing other build systems that may assume a `.capnp` file should be compiled. As
1315
an extension, `.gpu-wd-test` is supported to enable special handling for GPU tests.
14-
data: Files which the .capnp config file may embed. Typically JavaScript files.
16+
data: Additional files which the .capnp config file may embed. All TypeScript files will be compiled,
17+
their resulting files will be passed to the test as well. Usually TypeScript or Javascript source files.
1518
args: Additional arguments to pass to `workerd`. Typically used to pass `--experimental`.
1619
"""
1720

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

24+
ts_srcs = [src for src in data if src.endswith(".ts")]
25+
26+
# Default name based on src.
27+
if name == None:
28+
name = src.removesuffix(".capnp").removesuffix(".wd-test").removesuffix(".gpu-wd-test").removesuffix(".ts-wd-test")
29+
30+
if len(ts_srcs) != 0:
31+
# TODO When TypeScript 5.6 comes out use noCheck so the test fails throwing a type error.
32+
ts_project(
33+
name = name + "@ts_project",
34+
srcs = ts_srcs,
35+
tsconfig = "tsconfig.json",
36+
allow_js = True,
37+
source_map = True,
38+
composite = True,
39+
deps = ["//src/node:node.capnp@tsproject"],
40+
)
41+
data += [js_src.removesuffix(".ts") + ".js" for js_src in ts_srcs]
42+
2143
# Add initial arguments for `workerd test` command.
2244
args = [
2345
"$(location //src/workerd/server:workerd)",
2446
"test",
2547
"$(location {})".format(src),
2648
] + args
2749

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

3251
_wd_test(
3352
name = name,

build/wd_ts_bundle.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ def wd_ts_bundle_capnp(
5858
declaration = True,
5959
tsconfig = name + "@tsconfig",
6060
deps = deps,
61+
visibility = ["//visibility:public"],
6162
)
6263

6364
data = wd_js_bundle_capnp(

src/workerd/api/BUILD.bazel

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,14 @@ wd_test(
227227
data = ["tests/js-rpc-test.js"],
228228
)
229229

230+
[wd_test(
231+
src = f,
232+
data = [f.removesuffix(".ts-wd-test") + ".ts"],
233+
args = ["--experimental"],
234+
) for f in glob(
235+
["**/*.ts-wd-test"],
236+
)]
237+
230238
# Enable GPU tests if experimental GPU support is enabled. Unfortunately, this depends on the right
231239
# drivers being available on Linux and macOS, so turn it off by default. Run GPU tests with
232240
# `bazel test //src/workerd/api:gpu/<test name>`.

src/workerd/api/http-test-ts.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright (c) 2024 Cloudflare, Inc.
2+
// Licensed under the Apache 2.0 license found in the LICENSE file or at:
3+
// https://opensource.org/licenses/Apache-2.0
4+
5+
import assert from "node:assert";
6+
7+
async function assertRequestCacheThrowsError(cacheHeader: RequestCache,
8+
errorName: String = 'Error',
9+
errorMessage: String = "The 'cache' field on 'RequestInitializerDict' is not implemented.") {
10+
assert.throws(() => {
11+
const header = { cache : cacheHeader};
12+
const req: RequestInit = header;
13+
new Request('https://example.org', req);
14+
}, {
15+
name: errorName,
16+
message: errorMessage,
17+
});
18+
}
19+
20+
async function assertFetchCacheRejectsError(cacheHeader: RequestCache,
21+
errorName: String = 'Error',
22+
errorMessage: String = "The 'cache' field on 'RequestInitializerDict' is not implemented.") {
23+
await assert.rejects((async () => {
24+
const header = { cache : cacheHeader};
25+
const req: RequestInit = header;
26+
await fetch('http://example.org', req);
27+
})(), {
28+
name: errorName,
29+
message: errorMessage,
30+
});
31+
}
32+
33+
export const cacheMode = {
34+
35+
async test() {
36+
assert.strictEqual("cache" in Request.prototype, false);
37+
{
38+
const req = new Request('https://example.org', {});
39+
assert.strictEqual(req.cache, undefined);
40+
}
41+
assertRequestCacheThrowsError('default');
42+
assertRequestCacheThrowsError('force-cache');
43+
assertRequestCacheThrowsError('no-cache');
44+
assertRequestCacheThrowsError('no-store');
45+
assertRequestCacheThrowsError('only-if-cached');
46+
assertRequestCacheThrowsError('reload');
47+
assertFetchCacheRejectsError('default');
48+
assertFetchCacheRejectsError('force-cache');
49+
assertFetchCacheRejectsError('no-cache');
50+
assertFetchCacheRejectsError('no-store');
51+
assertFetchCacheRejectsError('only-if-cached');
52+
assertFetchCacheRejectsError('reload');
53+
}
54+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using Workerd = import "/workerd/workerd.capnp";
2+
3+
const unitTests :Workerd.Config = (
4+
services = [
5+
( name = "http-test",
6+
worker = (
7+
modules = [
8+
( name = "worker", esModule = embed "http-test-ts.js" )
9+
],
10+
bindings = [
11+
( name = "SERVICE", service = "http-test" )
12+
],
13+
compatibilityDate = "2023-08-01",
14+
compatibilityFlags = ["nodejs_compat", "service_binding_extra_handlers"],
15+
)
16+
),
17+
],
18+
);

src/workerd/api/tsconfig.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"compilerOptions": {
3+
"target": "esnext",
4+
"lib": ["esnext", "webworker"],
5+
"strict": true,
6+
"moduleResolution": "node",
7+
"esModuleInterop": true,
8+
"outDir": "dist",
9+
"sourceMap": true,
10+
"baseUrl": ".",
11+
"rootDir": ".",
12+
"paths": {
13+
"@workerd/*": ["../../bazel-bin/src/workerd/*"]
14+
},
15+
"checkJs": true,
16+
"allowJs": true,
17+
"composite": true,
18+
"skipLibCheck": true
19+
},
20+
"include": ["**/*.ts"],
21+
"exclude": [],
22+
}

types/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ js_run_binary(
3636
out_dirs = ["definitions"],
3737
silent_on_success = False, # Always enable logging for debugging
3838
tool = ":build_types_bin",
39+
visibility = ["//visibility:public"],
3940
)
4041

4142
js_binary(

0 commit comments

Comments
 (0)