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
20 changes: 20 additions & 0 deletions .github/workflows/backend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ jobs:
timeout-minutes: 20
env:
SENTRY_API_SPEC_DIR: ${{ github.workspace }}/.artifacts/api-spec
SENTRY_API_TARBALL: ${{ github.workspace }}/.artifacts/sentry-api.tgz
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1

Expand All @@ -145,6 +146,25 @@ jobs:
- name: Test
run: pnpm --filter @sentry/api run test:vitest

# Install the packed package into clean projects on the oldest supported
# Node.js (engines.node) and on Bun, and use it there.
- name: Pack
run: pnpm --filter @sentry/api pack --out "$SENTRY_API_TARBALL"

- uses: actions/setup-node@1e60f620b9541d16bece96c5465dc8ee9832be0b # v4
with:
node-version: '22'

- name: Check the packed package on Node.js 22
run: node static/packages/api/test/consumers/check.mjs "$SENTRY_API_TARBALL"

- uses: oven-sh/setup-bun@0c5077e51419868618aeaa5fe8019c62421857d6 # v2.2.0
with:
bun-version: '1.3.14'

- name: Check the packed package on Bun
run: bun static/packages/api/test/consumers/check.mjs "$SENTRY_API_TARBALL"

# Selective testing - only on PRs, determine which tests to run based on coverage data.
# This job is skipped on push-to-master where the full suite runs instead.
select-tests:
Expand Down
7 changes: 7 additions & 0 deletions static/packages/api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,13 @@ Tests import the generated `src/`, `dist/` and `spec/` output, so build before t

Every `ts` example in this README is typechecked against the packed package by `test/readme.test.ts`, each as its own module. When an example uses a value it doesn't define, such as `sentry` or `options`, declare it in that test's `context`.

To check the packed package the way an outside project uses it, pack it and run `test/consumers/check.mjs` with each runtime to check. CI runs it on Node.js 22 and Bun. It installs the tarball into fresh projects from the npm registry, with no optional peers and then with each validator peer:

```sh
pnpm --filter @sentry/api pack --out /tmp/sentry-api.tgz
node static/packages/api/test/consumers/check.mjs /tmp/sentry-api.tgz
```

## License

FSL-1.1-Apache-2.0. See [LICENSE.md](LICENSE.md).
55 changes: 55 additions & 0 deletions static/packages/api/test/consumers/check.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* Checks a packed @sentry/api tarball the way an outside project uses it: it
* installs the tarball from npm's resolver into a fresh project with no
* optional peers and runs smoke.mjs, then installs each validator peer on its
* own and runs validators.mjs. Both run on the runtime that runs this script,
* so CI starts it once with each runtime it supports.
*
* node test/consumers/check.mjs path/to/sentry-api-x.y.z.tgz
* bun test/consumers/check.mjs path/to/sentry-api-x.y.z.tgz
*/
import { execFileSync } from "node:child_process";
import { copyFileSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join, resolve } from "node:path";

const [tarball] = process.argv.slice(2);
if (!tarball) throw new Error("Usage: check.mjs <packed @sentry/api tarball>");

const runtime = process.versions.bun ? `Bun ${process.versions.bun}` : `Node.js ${process.version}`;

/** A fresh ES module project with the tarball and `packages` installed. */
function consumer(packages) {
const directory = mkdtempSync(join(tmpdir(), "sentry-api-consumer-"));
writeFileSync(join(directory, "package.json"), JSON.stringify({ private: true, type: "module" }));
execFileSync("npm", ["install", "--no-audit", "--no-fund", "--ignore-scripts", resolve(tarball), ...packages], {
cwd: directory,
stdio: "inherit",
});
return directory;
}

/** Run one of this directory's scripts in `directory`, on this runtime. */
function run(directory, script, ...args) {
copyFileSync(new URL(script, import.meta.url), join(directory, script));
execFileSync(process.execPath, [script, ...args], { cwd: directory, stdio: "inherit" });
}

const bare = consumer([]);
try {
run(bare, "smoke.mjs");
const { peerDependencies } = JSON.parse(
readFileSync(join(bare, "node_modules", "@sentry", "api", "package.json"), "utf8"),
);
for (const peer of ["zod", "valibot"]) {
const directory = consumer([`${peer}@${peerDependencies[peer]}`]);
try {
run(directory, "validators.mjs", peer);
} finally {
rmSync(directory, { recursive: true, force: true });
}
}
} finally {
rmSync(bare, { recursive: true, force: true });
}
console.log(`The packed package works on ${runtime}.`);
54 changes: 54 additions & 0 deletions static/packages/api/test/consumers/smoke.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Runs inside a clean project that installed the packed package, with no
// optional peers, on the runtime check.mjs was started with.
import assert from "node:assert/strict";
import { API_BASE_PATH, createSentryClient, SentryApiError } from "@sentry/api";
import { createBrowserFetch } from "@sentry/api/browser";
import { createApiTransport } from "@sentry/api/client";
import { listOrganizations } from "@sentry/api/client/endpoints";
import { operations } from "@sentry/api/docs";
import { operations as catalog } from "@sentry/api/operations";

assert.equal(API_BASE_PATH, "/api/0");
assert(catalog.some(operation => operation.functionName === "listOrganizations"));
assert(operations.some(operation => operation.path === "/organizations/"));
for (const value of [createBrowserFetch, createApiTransport, listOrganizations]) {
assert.equal(typeof value, "function");
}

const requests = [];
const sentry = createSentryClient({
baseUrl: "https://sentry.example.test/",
headers: { Authorization: "Bearer token" },
fetch: async request => {
requests.push(request);
return request.url.includes("/missing/")
? Response.json({ detail: "Not found" }, { status: 404 })
: Response.json([{ slug: "acme" }], { headers: { "X-Hits": "1" } });
},
});

// A typed operation against the mocked fetch.
const listed = await sentry.listOrganizations();
assert(!("error" in listed));
assert.deepEqual(listed.data, [{ slug: "acme" }]);
assert.equal(listed.request.url, "https://sentry.example.test/api/0/organizations/");
assert.equal(listed.request.headers.get("authorization"), "Bearer token");
assert.equal(listed.response.headers.get("x-hits"), "1");

// An HTTP error keeps its response.
const missing = await sentry.client.get({ url: "/missing/" });
assert("error" in missing);
assert(missing.error instanceof SentryApiError);
assert.equal(missing.error.status, 404);
assert.deepEqual(missing.error.body, { detail: "Not found" });

// An already-aborted signal stops the request before it reaches fetch.
const controller = new AbortController();
const reason = new Error("stop");
controller.abort(reason);
const sent = requests.length;
const cancelled = await sentry.listOrganizations({ signal: controller.signal });
assert("error" in cancelled);
assert.equal(cancelled.error, reason);
assert.equal(cancelled.response, undefined);
assert.equal(requests.length, sent);
18 changes: 18 additions & 0 deletions static/packages/api/test/consumers/validators.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Runs inside a clean project that installed the packed package and one
// optional validator peer, named by the first argument.
import assert from "node:assert/strict";

const peer = process.argv[2];

if (peer === "zod") {
const { zListOrganizationsResponse } = await import("@sentry/api/zod");
assert.equal(zListOrganizationsResponse.safeParse([]).success, true);
assert.equal(zListOrganizationsResponse.safeParse("acme").success, false);
} else if (peer === "valibot") {
const v = await import("valibot");
const { vListOrganizationsResponse } = await import("@sentry/api/valibot");
assert.equal(v.safeParse(vListOrganizationsResponse, []).success, true);
assert.equal(v.safeParse(vListOrganizationsResponse, "acme").success, false);
} else {
throw new Error(`Unknown validator peer: ${peer}`);
}
Loading