1
0
Fork 0

feat: Remove unnecessary nested object

pull/1/head
Ambrose Chua 2021-09-10 01:55:07 +08:00
parent ff4300c306
commit 4891d78fb4
8 changed files with 93 additions and 19 deletions

View File

@ -1,6 +1,8 @@
import request from './request.js';
import fetch from './fetch.js';
const tests = {
...request,
...fetch,
};
export default tests;

20
fixtures/fetch.js Normal file
View File

@ -0,0 +1,20 @@
const tests = {
fetch: {
async acceptContentLength() {
try {
const ctrl = new AbortController();
setTimeout(() => ctrl.abort(), 5000);
const response = await fetch('https://httpbin.org/get', {
headers: {'Content-Length': 2},
signal: ctrl.signal,
});
const o = await response.json();
const length = o.headers['Content-Length'];
return `Content-Length: ${length}`;
} catch (error) {
return error.toString();
}
},
},
};
export default tests;

View File

@ -1,5 +1,6 @@
import process from 'node:process';
import fetch, {Request, Response, Headers} from 'node-fetch';
import {AbortController} from 'node-abort-controller';
import run from './run.js';
@ -7,6 +8,7 @@ global.Request = Request;
global.Response = Response;
global.Headers = Headers;
global.fetch = fetch;
global.AbortController = AbortController;
async function main(file) {
const tests = await import('./' + file + '.js');

View File

@ -1,20 +1,48 @@
/* eslint-disable no-new */
const tests = {
request: {
async invalidURL() {
async unexpectedPercent() {
try {
new Request('http://example.com%');
return {error: null};
return null;
} catch (error) {
return {error: error.toString()};
return error.toString();
}
},
async rejectCredentials() {
try {
new Request('http://user:pass@example.com');
return {error: null};
return null;
} catch (error) {
return {error: error.toString()};
return error.toString();
}
},
async rejectBodyInGET() {
try {
new Request('http://example.com', {body: 'a'});
return null;
} catch (error) {
return error.toString();
}
},
async rejectEmptyBodyInGET() {
try {
new Request('http://example.com', {body: ''});
return null;
} catch (error) {
return error.toString();
}
},
async acceptContentLength() {
try {
new Request('http://example.com', {
headers: {
'Content-Length': 0,
},
});
return null;
} catch (error) {
return error.toString();
}
},
},

6
package-lock.json generated
View File

@ -4106,6 +4106,12 @@
"resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.2.tgz",
"integrity": "sha512-hZXc7K2e+PgeI1eDBe/10Ard4ekbfrrqG8Ep+8Jmf4JID2bNg7NvCPOZN+kfF574pFQI7mum2AUqDidoKqcTOw=="
},
"node-abort-controller": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/node-abort-controller/-/node-abort-controller-3.0.0.tgz",
"integrity": "sha512-IqMCPbihDpbHV4bNws015hU0svIBGyzPjJearwXMGJyungWdblbBcboNojTz9bWOrrJD3zIwmcr5w5c+NH+2+A==",
"dev": true
},
"node-fetch": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-3.0.0.tgz",

View File

@ -28,6 +28,7 @@
"devDependencies": {
"@types/koa": "^2.13.4",
"@types/node": "^16.9.0",
"node-abort-controller": "^3.0.0",
"prettier": "^2.4.0",
"typescript": "^4.4.2",
"xo": "^0.44.0"

View File

@ -1,7 +1,7 @@
import logger from '@wdio/logger';
import Table from 'cli-table3';
import {Platform, Result, Context} from './types';
import {Platform, Result, ResultObject, Context} from './types';
import PlatformBrowser from './platform-browser';
import PlatformNode from './platform-node';
import PlatformDeno from './platform-deno';
@ -9,7 +9,7 @@ import {chrome, firefox, safari} from './browser-drivers';
import {FixturesServer} from './server';
const log = logger('fetch-compare');
logger.setLevel('fetch-compare', 'info');
logger.setLevel('fetch-compare', 'warn');
const silent = true;
const platforms: Record<string, Platform> = {
@ -20,15 +20,28 @@ const platforms: Record<string, Platform> = {
deno: new PlatformDeno(),
};
function* tabularNames(reference: Result): Iterable<[string, string, string]> {
function formatValue(v: ResultObject): string {
if (v === null) {
return 'null';
}
if (v === undefined) {
return 'undefined';
}
if (typeof v === 'string' || typeof v === 'number') {
return v.toString();
}
return JSON.stringify(v);
}
function* tabularNames(reference: Result): Iterable<[string, string]> {
/* eslint-disable guard-for-in */
for (const groupName in reference) {
const results = reference[groupName];
for (const resultName in results) {
const resultObject = results[resultName];
for (const key in resultObject) {
yield [groupName, resultName, key];
}
yield [groupName, resultName];
}
}
/* eslint-enable guard-for-in */
@ -41,13 +54,9 @@ function* tabularResults(results: Record<string, Result>): Iterable<string[]> {
}
const names = Object.keys(results);
for (const [group, result, key] of tabularNames(ref)) {
for (const [group, result] of tabularNames(ref)) {
for (const n of names) {
yield [
`${group}:${result}:${key}`,
n,
results[n][group][result][key]?.toString() ?? 'null',
];
yield [`${group}:${result}`, n, formatValue(results[n][group][result])];
}
}
}
@ -68,6 +77,7 @@ async function run() {
/* eslint-disable guard-for-in, no-await-in-loop */
for (const name in platforms) {
try {
log.debug(name, 'starting');
results[name] = await platforms[name].run(ctx, 'all');
log.debug(name, results[name]);
} catch (error: unknown) {

View File

@ -1,4 +1,9 @@
export type ResultObject = Record<string, string | number | null>;
export type ResultObject =
| string
| number
| null
| undefined
| Record<string, unknown>;
export type Result = Record<string, Record<string, ResultObject>>;
export interface Context {