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
21 changes: 21 additions & 0 deletions crates/trusted-server-js/lib/eslint.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// ESLint v9 flat config
import js from '@eslint/js';
import globals from 'globals';
import tseslint from 'typescript-eslint';
import importPlugin from 'eslint-plugin-import';
import jsdoc from 'eslint-plugin-jsdoc';
Expand Down Expand Up @@ -36,4 +37,24 @@ export default [
'import/order': ['error', { 'newlines-between': 'always' }],
},
},
// Honor the `_`-prefix convention for intentionally unused bindings in every
// linted file: tseslint recommended enables the rule globally, so scoping
// this to *.ts(x) would leave .mjs at the pattern-less defaults
{
rules: {
'@typescript-eslint/no-unused-vars': [
Comment thread
aram356 marked this conversation as resolved.
'error',
{ argsIgnorePattern: '^_', varsIgnorePattern: '^_', caughtErrorsIgnorePattern: '^_' },
],
},
},
// Node build scripts and Node-run test harnesses use nodeBuiltin, not node,
// so CommonJS-only names (__dirname, require, module) still fail no-undef
// in these ES modules
{
files: ['*.mjs', 'test/**/*.mjs'],
Comment thread
aram356 marked this conversation as resolved.
languageOptions: {
globals: globals.nodeBuiltin,
},
},
];
33 changes: 17 additions & 16 deletions crates/trusted-server-js/lib/package-lock.json

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

5 changes: 3 additions & 2 deletions crates/trusted-server-js/lib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
"dev": "vite build --watch",
"test": "vitest run",
"test:watch": "vitest",
"lint": "eslint \"src/**/*.{ts,tsx}\"",
"lint:fix": "eslint --fix \"src/**/*.{ts,tsx}\"",
"lint": "eslint . --max-warnings=0",
"lint:fix": "eslint --fix . --max-warnings=0",
"format": "prettier --check \"**/*.{ts,tsx,js,json,css,md}\"",
"format:write": "prettier --write \"**/*.{ts,tsx,js,json,css,md}\""
},
Expand All @@ -29,6 +29,7 @@
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-jsdoc": "^62.5.4",
"eslint-plugin-unicorn": "^62.0.0",
"globals": "^16.0.0",
"jsdom": "^28.0.0",
"prettier": "^3.2.5",
"typescript": "^5.5.4",
Expand Down
11 changes: 7 additions & 4 deletions crates/trusted-server-js/lib/test/core/auction.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';

import { buildAdRequest, parseAuctionResponse, sendAuction } from '../../src/core/auction';

describe('auction/buildAdRequest', () => {
Expand Down Expand Up @@ -247,7 +248,7 @@ describe('auction/sendAuction', () => {
],
}),
};
globalThis.fetch = vi.fn().mockResolvedValue(mockResponse) as any;
globalThis.fetch = vi.fn().mockResolvedValue(mockResponse) as unknown as typeof fetch;

const request = {
adUnits: [
Expand All @@ -274,7 +275,9 @@ describe('auction/sendAuction', () => {
});

it('returns empty array on network error', async () => {
globalThis.fetch = vi.fn().mockRejectedValue(new Error('network error')) as any;
globalThis.fetch = vi
.fn()
.mockRejectedValue(new Error('network error')) as unknown as typeof fetch;

const bids = await sendAuction('/auction', { adUnits: [] });
expect(bids).toEqual([]);
Expand All @@ -286,7 +289,7 @@ describe('auction/sendAuction', () => {
status: 200,
headers: { get: () => 'text/html' },
json: async () => ({}),
}) as any;
}) as unknown as typeof fetch;

const bids = await sendAuction('/auction', { adUnits: [] });
expect(bids).toEqual([]);
Expand All @@ -298,7 +301,7 @@ describe('auction/sendAuction', () => {
status: 500,
headers: { get: () => 'application/json' },
json: async () => ({}),
}) as any;
}) as unknown as typeof fetch;

const bids = await sendAuction('/auction', { adUnits: [] });
expect(bids).toEqual([]);
Expand Down
2 changes: 1 addition & 1 deletion crates/trusted-server-js/lib/test/core/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('config', () => {
setConfig({ debug: true });
expect(log.getLevel()).toBe('debug');

setConfig({ logLevel: 'info' as any });
setConfig({ logLevel: 'info' });
expect(log.getLevel()).toBe('info');
});
});
34 changes: 15 additions & 19 deletions crates/trusted-server-js/lib/test/core/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';

declare global {
interface Window {
tsjs?: any;
}
}
import type { AuctionBidData, AuctionSlot, TsjsApi } from '../../src/core/types';

const ORIGINAL_FETCH = global.fetch;

describe('core/index', () => {
beforeEach(async () => {
await vi.resetModules();
document.body.innerHTML = '';
delete (window as any).tsjs;
delete window.tsjs;
});

afterEach(() => {
Expand All @@ -21,7 +17,7 @@ describe('core/index', () => {

it('initializes tsjs API with expected surface', async () => {
await import('../../src/core/index');
const api = window.tsjs;
const api = window.tsjs as TsjsApi;
expect(api).toBeDefined();
expect(typeof api.version).toBe('string');
expect(Array.isArray(api.que)).toBe(true);
Expand All @@ -35,28 +31,28 @@ describe('core/index', () => {

it('defaults adSlots and bids so gated-off pages never see undefined', async () => {
await import('../../src/core/index');
const api = window.tsjs;
const api = window.tsjs as TsjsApi;
expect(api.adSlots).toEqual([]);
expect(api.bids).toEqual({});
});

it('preserves edge-injected adSlots and bids set before the bundle loads', async () => {
(window as any).tsjs = {
adSlots: [{ id: 'pre-injected' }],
bids: { 'pre-injected': { hb_pb: '1.00' } },
};
window.tsjs = {
adSlots: [{ id: 'pre-injected' } as AuctionSlot],
bids: { 'pre-injected': { hb_pb: '1.00' } } as Record<string, AuctionBidData>,
} as TsjsApi;

await import('../../src/core/index');

expect(window.tsjs.adSlots).toEqual([{ id: 'pre-injected' }]);
expect(window.tsjs.bids).toEqual({ 'pre-injected': { hb_pb: '1.00' } });
expect(window.tsjs!.adSlots).toEqual([{ id: 'pre-injected' }]);
expect(window.tsjs!.bids).toEqual({ 'pre-injected': { hb_pb: '1.00' } });
});

it('flushes queued callbacks that existed before initialization', async () => {
const callback = vi.fn(function () {
const callback = vi.fn(function (this: TsjsApi) {
expect(this).toBe(window.tsjs);
});
(window as any).tsjs = { que: [callback] };
window.tsjs = { que: [callback] as Array<() => void> } as TsjsApi;

await import('../../src/core/index');

Expand All @@ -65,7 +61,7 @@ describe('core/index', () => {

it('installs queue that executes callbacks immediately with api context', async () => {
await import('../../src/core/index');
const api = window.tsjs;
const api = window.tsjs as TsjsApi;
const fn = vi.fn();

api.que.push(fn);
Expand All @@ -76,7 +72,7 @@ describe('core/index', () => {

it('renders registered ad units using core rendering helpers', async () => {
await import('../../src/core/index');
const api = window.tsjs;
const api = window.tsjs as TsjsApi;

api.addAdUnits([
{ code: 'slot-1', mediaTypes: { banner: { sizes: [[300, 250]] } } },
Expand All @@ -92,7 +88,7 @@ describe('core/index', () => {
it('exposes requestAds from the core request module', async () => {
const { requestAds } = await import('../../src/core/request');
await import('../../src/core/index');
const api = window.tsjs;
const api = window.tsjs as TsjsApi;

expect(api.requestAds).toBe(requestAds);
});
Expand Down
4 changes: 3 additions & 1 deletion crates/trusted-server-js/lib/test/core/registry.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { describe, it, expect, beforeEach, vi } from 'vitest';

import type { AdUnit } from '../../src/core/types';

describe('registry', () => {
beforeEach(async () => {
await vi.resetModules();
Expand All @@ -17,7 +19,7 @@ describe('registry', () => {
],
},
},
} as any;
} as AdUnit;
addAdUnits(unit);

const all = getAllUnits();
Expand Down
Loading
Loading