test(javascript): share response consumer fixtures (#16445)

This commit is contained in:
Vincent Koc authored and GitHub committed 2026-12-03 19:15:57 +00:00
1 parent 1e6d47667e
commit 5c23e89e36
5 files changed
+28 -49

No files matched your search

@@ -1,6 +1,6 @@
import { expect, it } from "vitest";
import { analyzeJavaScriptSemantics } from "./javascriptSemanticAnalysis.js";
import { responseReaderFacts } from "./javascriptSemanticResponseConsumers.fixture.js";
/**
* Only `fetch` resolves to a `Response`, and only once its promise settles.
@@ -9,15 +9,6 @@ import { analyzeJavaScriptSemantics } from "./javascriptSemanticAnalysis.js";
* `fetch` returns a `Promise`. In every one of those the call throws at the call
* site instead of reading a body, so it is not a response consumer.
*/
const consumersFor = (source: string) =>
analyzeJavaScriptSemantics(source)
.requestOperations.filter(({ kind }) => kind === "response-consumer")
.map(({ method, endpoint, resolution }) => ({
method,
endpoint,
resolution,
}));
it.each([
[
"an un-awaited fetch",
@@ -38,7 +29,7 @@ it.each([
`function f(){ const ws = new WebSocket("wss://api.test/x"); return ws.json(); }`,
],
])("emits no response consumer for %s", (_name, source) => {
expect(consumersFor(source)).toEqual([]);
expect(responseReaderFacts(source)).toEqual([]);
});
it.each([
@@ -47,7 +38,7 @@ it.each([
["arrayBuffer", "arrayBuffer"],
] as const)("keeps the settled fetch body reader for %s", (_name, method) => {
expect(
consumersFor(
responseReaderFacts(
`async function f(){ const res = await fetch("https://api.test/x"); return res.${method}(); }`,
),
).toEqual([
@@ -61,7 +52,7 @@ it.each([
it("keeps a settled fetch reader through a wrapper expression", () => {
expect(
consumersFor(
responseReaderFacts(
`async function f(){ const res = await fetch("https://api.test/x"); return (res).json(); }`,
),
).toHaveLength(1);
@@ -70,7 +61,7 @@ it("keeps a settled fetch reader through a wrapper expression", () => {
it("keeps the fetch candidate when the binding also holds another request", () => {
// The mixed binding keeps its fetch link, so the consumer stays a candidate
// rather than losing the endpoint entirely.
const consumers = consumersFor(`
const consumers = responseReaderFacts(`
import http from "node:http";
async function f(){
let res = await fetch("https://api.test/x");
@@ -1,10 +1,9 @@
import { expect, it } from "vitest";
import { analyzeJavaScriptSemantics } from "./javascriptSemanticAnalysis.js";
import { responseConsumers } from "./javascriptSemanticResponseConsumers.fixture.js";
const consumers = (source: string) =>
analyzeJavaScriptSemantics(source).requestOperations.filter(
({ kind }) => kind === "response-consumer",
);
responseConsumers(analyzeJavaScriptSemantics(source));
it.each([
'let response = await fetch("data:text/plain,original"); response = {text: () => "replacement"}; response.text();',
@@ -1,19 +1,10 @@
import { expect, it } from "vitest";
import { analyzeJavaScriptSemantics } from "./javascriptSemanticAnalysis.js";
const responses = (source: string) =>
analyzeJavaScriptSemantics(source)
.requestOperations.filter(({ kind }) => kind === "response-consumer")
.map(({ method, endpoint, resolution }) => ({
method,
endpoint,
resolution,
}));
import { responseReaderFacts } from "./javascriptSemanticResponseConsumers.fixture.js";
it("keeps the endpoint of a uniquely consumed request response", () => {
expect(
responses(`
responseReaderFacts(`
async function load() {
const a = await fetch("https://example.test/a");
const b = await fetch("https://example.test/b");
@@ -36,7 +27,7 @@ it("keeps the endpoint of a uniquely consumed request response", () => {
it("leaves an ambiguous response consumer without an endpoint", () => {
expect(
responses(`
responseReaderFacts(`
async function load(flag) {
let response = await fetch("https://example.test/a");
if (flag) response = await fetch("https://example.test/b");
@@ -48,7 +39,7 @@ it("leaves an ambiguous response consumer without an endpoint", () => {
it("leaves a response replaced by a non-request value without an endpoint", () => {
expect(
responses(`
responseReaderFacts(`
async function load(cachedResponse) {
let response = await fetch("https://example.test/a");
response = cachedResponse;
@@ -87,7 +78,7 @@ it.each([
] as const)(
"keeps a response endpoint only when its request reaches the read: %s",
(_name, source, endpoint) => {
expect(responses(source)).toEqual([
expect(responseReaderFacts(source)).toEqual([
expect.objectContaining({ method: "json", endpoint }),
]);
},
@@ -1,6 +1,7 @@
import { expect, it } from "vitest";
import { analyzeJavaScriptSemantics } from "./javascriptSemanticAnalysis.js";
import { responseConsumers } from "./javascriptSemanticResponseConsumers.fixture.js";
it.each([
["response.json();", "/original"],
@@ -25,9 +26,7 @@ it.each([
(operation) => operation.kind === "request",
);
expect(request?.endpoint).toBe("/original");
const consumers = result.requestOperations.filter(
(operation) => operation.kind === "response-consumer",
);
const consumers = responseConsumers(result);
expect(consumers).toHaveLength(1);
expect(consumers[0]).toMatchObject({
endpoint,
@@ -41,11 +40,7 @@ it("does not prove an incoming response was initialized by a conditional request
const result = analyzeJavaScriptSemantics(
"let response;if(flag)response=await fetch('/original');response.json();",
);
expect(
result.requestOperations.filter(
(operation) => operation.kind === "response-consumer",
),
).toEqual([
expect(responseConsumers(result)).toEqual([
expect.objectContaining({ endpoint: null, resolution: "partial" }),
]);
});
@@ -62,11 +57,7 @@ it.each([
(operation) => operation.kind === "request",
),
).toEqual([expect.objectContaining({ endpoint: "/original" })]);
expect(
result.requestOperations.filter(
(operation) => operation.kind === "response-consumer",
),
).toEqual([]);
expect(responseConsumers(result)).toEqual([]);
},
);
@@ -74,11 +65,7 @@ it("keeps the existing contract for a response linked to multiple requests", ()
const result = analyzeJavaScriptSemantics(
"let response=await fetch('/original');response=await fetch('/other');response.json();",
);
expect(
result.requestOperations.filter(
(operation) => operation.kind === "response-consumer",
),
).toEqual([
expect(responseConsumers(result)).toEqual([
expect.objectContaining({ endpoint: null, resolution: "partial" }),
]);
});
@@ -0,0 +1,11 @@
import { analyzeJavaScriptSemantics } from "./javascriptSemanticAnalysis.js";
export const responseConsumers = (
result: ReturnType<typeof analyzeJavaScriptSemantics>,
) =>
result.requestOperations.filter(({ kind }) => kind === "response-consumer");
export const responseReaderFacts = (source: string) =>
responseConsumers(analyzeJavaScriptSemantics(source)).map(
({ method, endpoint, resolution }) => ({ method, endpoint, resolution }),
);