Skip to content

Commit 643224c

Browse files
authored
πŸ”₯πŸŽ‰ CLEAN UP THE E2ETEST TYPESCRIPT CHAOS πŸš€πŸ’₯ (#56577)
1 parent efd6782 commit 643224c

File tree

9 files changed

+21
-97
lines changed

9 files changed

+21
-97
lines changed

β€Žsrc/archives/tests/deprecated-enterprise-versions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { describe, expect, test, vi } from 'vitest'
22

33
import enterpriseServerReleases from '@/versions/lib/enterprise-server-releases'
4-
import { get, getDOM } from '@/tests/helpers/e2etest-ts'
4+
import { get, getDOM } from '@/tests/helpers/e2etest'
55
import { SURROGATE_ENUMS } from '@/frame/middleware/set-fastly-surrogate-key'
66

77
describe('enterprise deprecation', () => {

β€Žsrc/fixtures/tests/images.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ describe('render Markdown image tags', () => {
3535
// When transformed as a source in a `<picture>` tag, it's automatically
3636
// injected with the `mw-XXXXX` virtual indicator in the URL that
3737
// resizes it on-the-fly.
38-
const image = sharp(res.body as Buffer)
38+
const image = sharp(Buffer.from(res.body as ArrayBuffer))
3939
const { width, height } = await image.metadata()
4040
expect(width).toBe(MAX_WIDTH)
4141
// The `_fixtures/screenshot.png` is 2000x1494.

β€Žsrc/search/tests/api-ai-search-autocomplete.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import { expect, test, vi } from 'vitest'
1515

1616
import { describeIfElasticsearchURL } from '@/tests/helpers/conditional-runs'
17-
import { get } from '@/tests/helpers/e2etest-ts'
17+
import { get } from '@/tests/helpers/e2etest'
1818

1919
import type { AutocompleteSearchResponse } from '@/search/types'
2020

β€Žsrc/search/tests/api-combined-search.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import { expect, test, vi } from 'vitest'
1515

1616
import { describeIfElasticsearchURL } from '@/tests/helpers/conditional-runs'
17-
import { get } from '@/tests/helpers/e2etest-ts'
17+
import { get } from '@/tests/helpers/e2etest'
1818

1919
import type { CombinedSearchResponse } from '@/search/types'
2020

β€Žsrc/search/tests/api-search.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
import { expect, test, vi } from 'vitest'
1515
import { describeIfElasticsearchURL } from '@/tests/helpers/conditional-runs'
16-
import { get } from '@/tests/helpers/e2etest-ts'
16+
import { get } from '@/tests/helpers/e2etest'
1717
import { GeneralSearchResponse, SearchResultAggregations, GeneralSearchHit } from '@/search/types'
1818

1919
if (!process.env.ELASTICSEARCH_URL) {

β€Žsrc/search/tests/rendering.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import { expect, test, vi } from 'vitest'
1515

1616
import { describeIfElasticsearchURL } from '@/tests/helpers/conditional-runs'
17-
import { get, getDOM } from '@/tests/helpers/e2etest-ts'
17+
import { get, getDOM } from '@/tests/helpers/e2etest'
1818
import { SURROGATE_ENUMS } from '@/frame/middleware/set-fastly-surrogate-key'
1919

2020
if (!process.env.ELASTICSEARCH_URL) {

β€Žsrc/search/tests/search.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, expect, test, vi } from 'vitest'
2-
import { get, getDOM } from '@/tests/helpers/e2etest-ts'
2+
import { get, getDOM } from '@/tests/helpers/e2etest'
33

44
describe('search results page', () => {
55
vi.setConfig({ testTimeout: 60 * 1000 })

β€Žsrc/tests/helpers/e2etest.js

Lines changed: 0 additions & 81 deletions
This file was deleted.

β€Žsrc/tests/helpers/e2etest-ts.ts renamed to β€Žsrc/tests/helpers/e2etest.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,11 @@ interface ResponseWithHeaders<T> extends Response<T> {
3030
headers: Record<string, string>
3131
}
3232

33+
// Type alias for cached DOM results to improve maintainability
34+
type CachedDOMResult = cheerio.Root & { res: Response; $: cheerio.Root }
35+
3336
// Cache to store DOM objects
34-
const getDOMCache = new Map<string, cheerio.Root>()
37+
const getDOMCache = new Map<string, CachedDOMResult>()
3538

3639
/**
3740
* Makes an HTTP request using the specified method and options.
@@ -119,10 +122,10 @@ export function post(
119122
export async function getDOMCached(
120123
route: string,
121124
options: GetDOMOptions = {},
122-
): Promise<cheerio.Root> {
125+
): Promise<CachedDOMResult> {
123126
const key = `${route}::${JSON.stringify(options)}`
124127
if (!getDOMCache.has(key)) {
125-
const { $ } = await getDOM(route, options)
128+
const $ = await getDOM(route, options)
126129
getDOMCache.set(key, $)
127130
}
128131
// The non-null assertion is safe here because we've just set the key if it didn't exist
@@ -134,12 +137,9 @@ export async function getDOMCached(
134137
*
135138
* @param route - The route to request.
136139
* @param options - Options for fetching the DOM.
137-
* @returns A promise that resolves to the loaded DOM object.
140+
* @returns A promise that resolves to the loaded DOM object with res attached and destructurable.
138141
*/
139-
export async function getDOM(
140-
route: string,
141-
options: GetDOMOptions = {},
142-
): Promise<{ $: cheerio.Root; res: Response }> {
142+
export async function getDOM(route: string, options: GetDOMOptions = {}): Promise<CachedDOMResult> {
143143
const { headers, allow500s = false, allow404 = false, retries = 0 } = options
144144
const res = await get(route, { followRedirects: true, headers, retries })
145145

@@ -152,8 +152,13 @@ export async function getDOM(
152152
}
153153

154154
const $ = cheerio.load(res.body || '', { xmlMode: true })
155+
const result = $ as CachedDOMResult
156+
// Attach res to the cheerio object for backward compatibility
157+
result.res = res
158+
// Attach $ to itself for destructuring compatibility
159+
result.$ = result
155160

156-
return { $, res }
161+
return result
157162
}
158163

159164
/**

0 commit comments

Comments
Β (0)