aglinxinyuan commented on code in PR #9198: URL: https://github.com/apache/texera/pull/9198#discussion_r4673027513
########## frontend/src/jsdom-selector-cache-setup.ts: ########## @@ -0,0 +1,47 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/** + * Empties jsdom's CSS-selector cache between tests. + * + * jsdom answers `querySelector` through nwsapi, which memoizes every selector together with the + * nodes it last matched, for the lifetime of the document. The unit-test builder runs a worker's + * spec files against one document, and TestBed queries a fresh `#root<N>` per fixture, so every + * finished fixture, and through its `__ngContext__` the whole TestBed graph, stays cached until the + * worker exits. The tables are emptied in place because jsdom's engine reads the exported objects. + * + * `beforeEach` runs after the previous test's TestBed teardown, whose own queries would otherwise be + * cached again; `afterAll` covers the file's last test. This is a setupFile (see `vitest.config.ts`). + */ +type NwsapiCaches = { selectResolvers: Record<string, unknown>; matchResolvers: Record<string, unknown> }; + +function resetSelectorCache(): void { + const document = globalThis.document as unknown as Record<symbol, { _nwsapi?: NwsapiCaches } | undefined>; + if (!document) return; + // jsdom keeps its implementation object behind the DOM wrapper, under a symbol it does not export. + const implSymbol = Object.getOwnPropertySymbols(document).find(symbol => symbol.description === "impl"); + const nwsapi = implSymbol && document[implSymbol]?._nwsapi; + if (!nwsapi) return; + for (const cache of [nwsapi.selectResolvers, nwsapi.matchResolvers]) { + for (const selector of Object.keys(cache)) delete cache[selector]; + } +} + +beforeEach(resetSelectorCache); +afterAll(resetSelectorCache); Review Comment: Added `frontend/src/jsdom-selector-cache-setup.spec.ts` in 117febe7a4. It reads nwsapi's caches through the same private jsdom fields and checks across test boundaries that: - a TestBed fixture fills the real `selectResolvers` and `matchResolvers`; - after that fixture's teardown, the next test finds the same objects emptied in place; - a later fixture still finds its host element, and its queries cache into those objects again; - the reset leaves an engine that hasn't started yet alone, without throwing. I checked locally that the spec fails when the setup is left out of `vitest.config.ts`, runs in `afterEach`, replaces the cache objects instead of emptying them, loses its guard for an engine that hasn't started, or looks under a field jsdom doesn't have. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
