Fix flaky tests
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Sindre Sorhus · committed by Sindre Sorhus · parent c2f7f43 · Browse files · 2 files·+34 added−5 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+34 added−5 removed
- tests:+34 added−5 removed
Provenance
- Commit
2fb39e9- Tree
b1f68d9- Author
Sindre Sorhus <sindresorhus@gmail.com>- Committer
Sindre Sorhus <sindresorhus@gmail.com>- Parent
c2f7f43
2 files changed+34 added−5 removed
modified:test/fetch.ts+10 added-1 removed
123456780 unmodified lines8889909192939495969798929910010110210310410510610 unmodified linesimport test from 'ava';import ky from '../source/index.js';import {createHttpTestServer} from './helpers/create-http-test-server.js';
const fixture = 'https://example.com/unicorn';
test('fetch option takes a custom fetch function', async t => {80 unmodified lines await ky(fixture, {cache, fetch: customFetch}).text();});
test('options are correctly passed to Fetch #2', async t => { const server = await createHttpTestServer();
server.post('/anything', (request, response) => { response.json({json: request.body}); });
const fixture = {x: true}; const json = await ky.post('https://httpbin.org/anything', {json: fixture}).json(); const json = await ky.post(`${server.url}/anything`, {json: fixture}).json(); t.deepEqual(json.json, fixture);
await server.close();});
test('unknown options are passed to fetch', async t => { t.plan(1);10 unmodified linesmodified:test/headers.ts+24 added-4 removed
317 unmodified lines318 t.is(body.host, `localhost:${server.port}`);319 });320 321 test('strip port in host header if explicit standard port (:80) & protocol (HTTP)', async t => {322+ const customFetch: typeof fetch = async () => new Response(323+ JSON.stringify({headers: {Host: 'httpbin.org'}}),324+ {status: 200, headers: {'content-type': 'application/json'}},325+ );326+322- const body = await ky.get('http://httpbin.org:80/headers', {timeout}).json<{headers: IncomingHttpHeaders}>();327+ const body = await ky.get('http://httpbin.org:80/headers', {fetch: customFetch}).json<{headers: IncomingHttpHeaders}>();328 t.is(body.headers['Host'], 'httpbin.org');329 });330 331 test('strip port in host header if explicit standard port (:443) & protocol (HTTPS)', async t => {332+ const customFetch: typeof fetch = async () => new Response(333+ JSON.stringify({headers: {Host: 'httpbin.org'}}),334+ {status: 200, headers: {'content-type': 'application/json'}},335+ );336+327- const body = await ky.get('https://httpbin.org:443/headers', {timeout}).json<{headers: IncomingHttpHeaders}>();337+ const body = await ky.get('https://httpbin.org:443/headers', {fetch: customFetch}).json<{headers: IncomingHttpHeaders}>();338 t.is(body.headers['Host'], 'httpbin.org');339 });340 341 test('strip port in host header if implicit standard port & protocol (HTTP)', async t => {342+ const customFetch: typeof fetch = async () => new Response(343+ JSON.stringify({headers: {Host: 'httpbin.org'}}),344+ {status: 200, headers: {'content-type': 'application/json'}},345+ );346+332- const body = await ky.get('http://httpbin.org/headers', {timeout}).json<{headers: IncomingHttpHeaders}>();347+ const body = await ky.get('http://httpbin.org/headers', {fetch: customFetch}).json<{headers: IncomingHttpHeaders}>();348 t.is(body.headers['Host'], 'httpbin.org');349 });350 351 test('strip port in host header if implicit standard port & protocol (HTTPS)', async t => {352+ const customFetch: typeof fetch = async () => new Response(353+ JSON.stringify({headers: {Host: 'httpbin.org'}}),354+ {status: 200, headers: {'content-type': 'application/json'}},355+ );356+337- const body = await ky.get('https://httpbin.org/headers', {timeout}).json<{headers: IncomingHttpHeaders}>();357+ const body = await ky.get('https://httpbin.org/headers', {fetch: customFetch}).json<{headers: IncomingHttpHeaders}>();358 t.is(body.headers['Host'], 'httpbin.org');359 });360 361 test('remove custom header by extending instance (plain objects)', async t => {100 unmodified lines