From 05a6ecf11c9e0f438645d61113f347d67edc204e Mon Sep 17 00:00:00 2001 From: AdityaAtulTewari Date: Mon, 5 Aug 2024 23:41:00 -0500 Subject: [PATCH] Create typescript tests for apis --- build/wd_test.bzl | 27 +++++++++++-- build/wd_ts_bundle.bzl | 1 + src/workerd/api/BUILD.bazel | 8 ++++ src/workerd/api/http-test-ts.ts | 54 +++++++++++++++++++++++++ src/workerd/api/http-test-ts.ts-wd-test | 18 +++++++++ src/workerd/api/tsconfig.json | 22 ++++++++++ types/BUILD.bazel | 1 + 7 files changed, 127 insertions(+), 4 deletions(-) create mode 100644 src/workerd/api/http-test-ts.ts create mode 100644 src/workerd/api/http-test-ts.ts-wd-test create mode 100644 src/workerd/api/tsconfig.json diff --git a/build/wd_test.bzl b/build/wd_test.bzl index 2a271d4064f..8a08a32c209 100644 --- a/build/wd_test.bzl +++ b/build/wd_test.bzl @@ -1,3 +1,5 @@ +load("@aspect_rules_ts//ts:defs.bzl", "ts_project") + def wd_test( src, data = [], @@ -11,13 +13,33 @@ 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)", @@ -25,9 +47,6 @@ def wd_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, diff --git a/build/wd_ts_bundle.bzl b/build/wd_ts_bundle.bzl index aaed27a3e79..cd8903a7faf 100644 --- a/build/wd_ts_bundle.bzl +++ b/build/wd_ts_bundle.bzl @@ -58,6 +58,7 @@ def wd_ts_bundle_capnp( declaration = True, tsconfig = name + "@tsconfig", deps = deps, + visibility = ["//visibility:public"], ) data = wd_js_bundle_capnp( diff --git a/src/workerd/api/BUILD.bazel b/src/workerd/api/BUILD.bazel index aab0bb0ba17..b897c267814 100644 --- a/src/workerd/api/BUILD.bazel +++ b/src/workerd/api/BUILD.bazel @@ -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/`. diff --git a/src/workerd/api/http-test-ts.ts b/src/workerd/api/http-test-ts.ts new file mode 100644 index 00000000000..69566f194fd --- /dev/null +++ b/src/workerd/api/http-test-ts.ts @@ -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'); + } +} diff --git a/src/workerd/api/http-test-ts.ts-wd-test b/src/workerd/api/http-test-ts.ts-wd-test new file mode 100644 index 00000000000..4b7847bec79 --- /dev/null +++ b/src/workerd/api/http-test-ts.ts-wd-test @@ -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"], + ) + ), + ], +); diff --git a/src/workerd/api/tsconfig.json b/src/workerd/api/tsconfig.json new file mode 100644 index 00000000000..cb5c6eef1c5 --- /dev/null +++ b/src/workerd/api/tsconfig.json @@ -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": [], +} diff --git a/types/BUILD.bazel b/types/BUILD.bazel index 53832be6bc9..2ea0d5feaee 100644 --- a/types/BUILD.bazel +++ b/types/BUILD.bazel @@ -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(