Skip to content

test(dataforseo): mock the google-ads section so the SDK stays out of the run - #153

Open
Nordalux wants to merge 1 commit into
every-app:mainfrom
Nordalux:fix/dataforseo-test-sdk-load
Open

test(dataforseo): mock the google-ads section so the SDK stays out of the run#153
Nordalux wants to merge 1 commit into
every-app:mainfrom
Nordalux:fix/dataforseo-test-sdk-load

Conversation

@Nordalux

Copy link
Copy Markdown
Contributor

Problem

src/server/lib/dataforseo/client.test.ts > meterDataforseoCall with split balances > skips billing in non-hosted mode fails intermittently in a full pnpm test:ci run with Test timed out in 5000ms. It passes when the file is run on its own.

The test itself does almost nothing — one mocked call, three assertions, no timers. The time is spent loading a module.

Cause

client.test.ts mocks the section modules the client wraps (labs, serp, business, backlinks, lighthouse, ai) under the comment "Mock every section module the client wraps". But the client does not reach those modules directly — it reaches them through loadDataforseoSections(), a single lazy import("@/server/lib/dataforseo/sections"), and that barrel also re-exports from google-ads.ts:

// src/server/lib/dataforseo/sections.ts
export {
  fetchAdsKeywordIdeas,
  fetchAdsSearchVolume,
} from "@/server/lib/dataforseo/google-ads";

google-ads.ts is the one section module the test does not mock, and it statically imports dataforseo-client:

// src/server/lib/dataforseo/google-ads.ts
import { ... } from "dataforseo-client";

So the ~3 MB SDK gets loaded for real — precisely the subtree the lazy boundary in client.ts exists to keep out of the graph:

the section fetchers and the ~3 MB dataforseo-client SDK they statically import stay out of the eager isolate startup graph and load once, on the first API call

The whole cost is charged to whichever test first touches the client. That happens to be skips billing in non-hosted mode, which is why the failure always names that test even though nothing is wrong with it. Measured locally, three consecutive isolated runs:

before after
skips billing in non-hosted mode 2125 / 2032 / 2131 ms 17 / 17 / 17 ms
next test, same code path 1–2 ms 1–2 ms

Under a 12-thread suite run the 2.1s crosses the 5s default timeout and the file goes red.

Change

Mock @/server/lib/dataforseo/google-ads alongside the other section modules. No assertion, fixture, or production file is touched.

Result

Full pnpm test:ci, both runs warm, same machine:

main this branch
result 1 failed, 732 passed 733 passed
test time 17.96s 9.63s
wall duration 16.93s 14.62s

All 18 tests in the file still pass. pnpm ci:check passes.

Note

The underlying sharp edge is that the mock list has to track the barrel's re-exports rather than the client's own wrappers — the two differ by exactly this module, and nothing fails loudly when they drift, it just gets slow. If you'd like a guard against that (for example a sections.ts re-export assertion in the test, or moving the SDK-free type re-exports out of the barrel), happy to follow up in a separate PR.

… the run

`client.test.ts` mocks every section module the client wraps, but the sections
barrel that `loadDataforseoSections()` imports also re-exports from
`google-ads.ts`, which was not mocked. That module statically imports
`dataforseo-client`, so the ~3 MB SDK was loaded for real on the first test
that touched the client — the exact subtree the lazy boundary in client.ts
exists to keep out.

The cost lands entirely on whichever test runs first. Locally that is "skips
billing in non-hosted mode" at ~2.1s while the next test over the same code
path takes 1-2ms, and under a full parallel suite run it crosses the 5s
default timeout and fails.

Mocking the module alongside the others drops that test from ~2100ms to ~17ms
and takes suite test time from ~18s to ~10s. All 18 tests in the file still
pass, and the assertions are untouched.

@sorcerai sorcerai left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed exact current diff. This isolates the test from the google-ads barrel re-export and removes the DataForSEO SDK load that causes the suite timeout; production code is unchanged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants