import { jest } from '@jest/globals'; import { HttpClient, type HttpClientPlugin } from '../dist/index.js'; describe('plugin', () => { it('can load plugin', () => { const client = new HttpClient(); const plugin = jest.fn>>((impl) => impl); expect(client.use(plugin)).toBe(client); expect(client.plugins).toEqual([plugin]); expect(client.use(plugin)).toBe(client); expect(client.plugins).toEqual([plugin]); expect(plugin).toHaveBeenCalledTimes(1); expect(plugin).toHaveBeenCalledWith(client); }); it('rejects bad plugin', () => { const client = new HttpClient(); // @ts-expect-error bad plugin for test const plugin = jest.fn>>((impl) => ({ ...impl })); Object.defineProperty(plugin, 'name', { value: 'bad plugin' }); expect(() => client.use(plugin)).toThrow('Invalid plugin: bad plugin'); expect(client.plugins).toEqual([]); expect(plugin).toHaveBeenCalledTimes(1); expect(plugin).toHaveBeenCalledWith(client); // @ts-expect-error bad input for test expect(() => client.use({})).toThrow('Invalid plugin: [object Object]'); expect(client.plugins).toEqual([]); // @ts-expect-error bad input for test expect(() => client.use(1)).toThrow('Invalid plugin: 1'); expect(client.plugins).toEqual([]); // @ts-expect-error bad input for test expect(() => client.use(null)).toThrow('Invalid plugin: null'); expect(client.plugins).toEqual([]); // @ts-expect-error bad input for test expect(() => client.use(undefined)).toThrow('Invalid plugin: undefined'); expect(client.plugins).toEqual([]); expect(() => client.use( // @ts-expect-error bad input for test class { a = 1; }, ), ).toThrow(`Class constructors cannot be invoked without 'new'`); expect(client.plugins).toEqual([]); }); });