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
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,12 @@
"test:matrix": "vitest --project=matrix",
"check": "tsc && tsc --project test",
"lint": "oxlint && oxfmt --check",
"knip": "knip"
"knip": "knip",
"precommit": "pnpm test --run && pnpm lint && pnpm knip && pnpm check"
},
"devDependencies": {
"@projectwallace/preset-oxlint": "^0.0.11",
"@types/node": "^26.6.5",
"@projectwallace/preset-oxlint": "^0.0.13",
"@types/node": "^26.6.10",
"@vitest/coverage-v8": "^5.0.5",
"knip": "^6.43.0",
"oxfmt": "^0.80.0",
Expand Down
42 changes: 21 additions & 21 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions test/helpers/compose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ import { INGREDIENTS } from './ingredients.ts'
let counter = 0

function content_type_for(path: string): string {
if (path.endsWith('.css')) return 'text/css'
if (path.endsWith('.js')) return 'text/javascript'
if (path.endsWith('.css')) {
return 'text/css'
}
if (path.endsWith('.js')) {
return 'text/javascript'
}
return 'text/plain'
}

Expand All @@ -17,7 +21,9 @@ function content_type_for(path: string): string {
export function composePage(server: FixtureServer, ids: string[]): string {
const chosen = ids.map((id) => {
const ingredient = INGREDIENTS.find((candidate) => candidate.id === id)
if (!ingredient) throw new Error(`Unknown ingredient: ${id}`)
if (!ingredient) {
throw new Error(`Unknown ingredient: ${id}`)
}
return ingredient
})

Expand Down
16 changes: 12 additions & 4 deletions test/helpers/fakes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ export function fake_response(init: FakeResponseInit = {}): ResponseLike {
headers: () => headers,
request: () => request,
text: () => {
if (typeof text === 'function') return text()
if (typeof text === 'function') {
return text()
}
return Promise.resolve(text)
},
}
Expand Down Expand Up @@ -61,7 +63,9 @@ function fake_frame(init: FakeFrameInit, default_url: () => string): FrameLike {
if (fn !== (read_page_css as unknown)) {
return Promise.reject(new Error('fake frame can only evaluate read_page_css'))
}
if (init.error) return Promise.reject(init.error)
if (init.error) {
return Promise.reject(init.error)
}
return Promise.resolve((init.dom_entries ?? []) as never)
},
}
Expand All @@ -79,13 +83,17 @@ export function fake_page(init: FakePageInit = {}): PageLike {
current_url = url
const result = await init.goto?.(url, options)
for (const response of init.responses ?? []) {
for (const listener of response_listeners) listener(response)
for (const listener of response_listeners) {
listener(response)
}
}
return result
},
frames: () => frames,
on(event, listener) {
if (event === 'response') response_listeners.add(listener as (response: ResponseLike) => void)
if (event === 'response') {
response_listeners.add(listener as (response: ResponseLike) => void)
}
return this
},
}
Expand Down
4 changes: 3 additions & 1 deletion test/helpers/powerset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ export function powerset<T>(items: T[]): T[][] {
for (let i = 0; i < n; i++) {
if (mask & (1 << i)) {
const item = items[i]
if (item !== undefined) subset.push(item)
if (item !== undefined) {
subset.push(item)
}
}
}
result.push(subset)
Expand Down
4 changes: 3 additions & 1 deletion test/integration/link.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ let server: FixtureServer
let page: TestPage

// Media-conditioned sheets come back in network arrival order, which is not guaranteed.
const by_css = (a: CSSSource, b: CSSSource) => a.css.localeCompare(b.css)
function by_css(a: CSSSource, b: CSSSource) {
return a.css.localeCompare(b.css)
}

beforeAll(async () => {
server = await createFixtureServer()
Expand Down
4 changes: 3 additions & 1 deletion test/integration/permutations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ const combos = powerset(INGREDIENTS.map((ingredient) => ingredient.id))

function ingredient_by_id(id: string) {
const ingredient = INGREDIENTS.find((candidate) => candidate.id === id)
if (!ingredient) throw new Error(`Unknown ingredient: ${id}`)
if (!ingredient) {
throw new Error(`Unknown ingredient: ${id}`)
}
return ingredient
}

Expand Down
5 changes: 3 additions & 2 deletions test/unit/error-handling.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import {
} from '../../src/index.ts'
import { fake_page, fake_response } from '../helpers/fakes.ts'

const unreadable_response = (url: string) =>
fake_response({
function unreadable_response(url: string) {
return fake_response({
url,
headers: { 'content-type': 'text/css' },
text: () => Promise.reject(new Error('body read failed')),
})
}

describe('navigation', () => {
test('scrape_css rejects with the error of a failed page.goto()', async () => {
Expand Down
5 changes: 3 additions & 2 deletions test/unit/link.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import { scrape_css } from '../../src/index.ts'
import { fake_page, fake_response } from '../helpers/fakes.ts'

const href = 'https://example.test/style.css'
const css_response = (css = '.a {}') =>
fake_response({ url: href, headers: { 'content-type': 'text/css' }, text: css })
function css_response(css = '.a {}') {
return fake_response({ url: href, headers: { 'content-type': 'text/css' }, text: css })
}

// Whether a disabled stylesheet is fetched is up to the browser (Chromium 153 does,
// Chromium 156 does not), so fake both behaviours instead of depending on the installed one.
Expand Down
Loading