Skip to content

Commit

Permalink
test: added formatting and module exports funcitonality checking
Browse files Browse the repository at this point in the history
  • Loading branch information
Varun0157 committed Mar 14, 2024
1 parent 7dbdb22 commit 2f88288
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 22 deletions.
14 changes: 14 additions & 0 deletions .github/workflows/exports.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: exports
on: [push]

jobs:
format:
runs-on: ubuntu-latest
name: module exports
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: "20"
- name: jest check
run: npm run test
14 changes: 14 additions & 0 deletions .github/workflows/formatting.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: formatting
on: [push]

jobs:
format:
runs-on: ubuntu-latest
name: prettier
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: "20"
- name: prettier check
run: npx prettier -c ./src --config ./src/.prettierrc
2 changes: 1 addition & 1 deletion src/checkTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ function checkKey(
item: string,
key: string,
expectedTypes: string[],
optional: boolean
optional: boolean,
): string | undefined {
if (!optional && !obj.hasOwnProperty(key)) {
return `${key} key must be present in each ${item} item`;
Expand Down
2 changes: 1 addition & 1 deletion src/constructCurl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export function getCurlRequest(request: RequestSpec): string {
curl += ` '${getURL(
request.httpRequest.baseUrl,
request.httpRequest.url,
getParamsForUrl(request.httpRequest.params, request.options.rawParams)
getParamsForUrl(request.httpRequest.params, request.options.rawParams),
)}'`;

return curl;
Expand Down
2 changes: 1 addition & 1 deletion src/executeRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export function constructGotRequest(allData: RequestSpec): GotRequest {
const completeUrl: string = getURL(
allData.httpRequest.baseUrl,
allData.httpRequest.url,
getParamsForUrl(allData.httpRequest.params, allData.options.rawParams)
getParamsForUrl(allData.httpRequest.params, allData.options.rawParams),
);

const options: OptionsOfTextResponseBody = {
Expand Down
8 changes: 1 addition & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
export {
RequestPosition,
ResponseData,
RequestSpec,
GotRequest,
TestResult
} from "./models";
export { RequestPosition, ResponseData, RequestSpec, GotRequest, TestResult } from "./models";

export { getRequestPositions, getAllRequestSpecs, getRequestSpec } from "./parseBundle";

Expand Down
10 changes: 5 additions & 5 deletions src/mergeData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ function getMergedParams(commonParams: RawParams, requestParams: RawParams): Par

function getMergedHeaders(
commonHeaders: RawHeaders,
requestHeaders: RawHeaders
requestHeaders: RawHeaders,
): { [name: string]: string } {
if (Array.isArray(commonHeaders)) {
commonHeaders = getArrayHeadersAsObject(commonHeaders);
Expand Down Expand Up @@ -79,7 +79,7 @@ function getMergedOptions(cOptions: RawOptions = {}, rOptions: RawOptions = {}):

function getMergedSetVars(
setvars: RawSetVars = {},
captures: Captures = {}
captures: Captures = {},
): { mergedVars: SetVar[]; hasJsonVars: boolean } {
const mergedVars: SetVar[] = [];
let hasJsonVars = false;
Expand Down Expand Up @@ -152,7 +152,7 @@ function mergePrefixBasedTests(tests: RawTests) {

function getMergedTests(
cTests: RawTests = {},
rTests: RawTests = {}
rTests: RawTests = {},
): { mergedTests: Tests; hasJsonTests: boolean } {
// Convert $. and h. at root level into headers and json keys
mergePrefixBasedTests(cTests);
Expand Down Expand Up @@ -209,11 +209,11 @@ export function getMergedData(commonData: Common, requestData: RawRequest): Requ

const { mergedTests: tests, hasJsonTests: hasJsonTests } = getMergedTests(
commonData?.tests,
requestData.tests
requestData.tests,
);
const { mergedVars: setvars, hasJsonVars: hasJsonVars } = getMergedSetVars(
requestData.setvars,
requestData.capture
requestData.capture,
);

const mergedData: RequestSpec = {
Expand Down
2 changes: 1 addition & 1 deletion src/parseBundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function getRawRequests(doc: string): {
function checkAndMergeRequest(
commonData: Common,
allRequests: { [name: string]: RawRequest },
name: string
name: string,
): RequestSpec {
let request = allRequests[name];
if (request === undefined) throw new Error(`Request ${name} is not defined in this bundle`);
Expand Down
8 changes: 4 additions & 4 deletions src/replaceVars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { RequestSpec } from "./models";

function replaceVariablesInArray(
data: any[],
variables: Variables
variables: Variables,
): { data: any[]; undefinedVars: string[] } {
let newData: any[] = [];
let undefs: string[] = [];
Expand All @@ -22,7 +22,7 @@ function replaceVariablesInArray(

function replaceVariablesInDict(
obj: { [key: string]: any },
variables: Variables
variables: Variables,
): { data: { [key: string]: any }; undefinedVars: string[] } {
let newData: { [key: string]: any } = {};
let undefs: string[] = [];
Expand All @@ -39,7 +39,7 @@ function replaceVariablesInDict(

function replaceVariablesInObject(
data: { [key: string]: any } | any[],
variables: Variables
variables: Variables,
): { data: any; undefinedVars: string[] } {
if (Array.isArray(data)) {
return replaceVariablesInArray(data, variables);
Expand Down Expand Up @@ -79,7 +79,7 @@ const VAR_REGEX_WITHOUT_BRACES = /(?<!\\)\$([_a-zA-Z]\w*)(?=\W|$)/g;

function replaceVariablesInString(
text: string,
variables: Variables
variables: Variables,
): { data: any; undefinedVars: string[] } {
// maintaining a separate boolean instead of initially setting valueInNativeType to, say, undefined,
// because valueInNativeType may actually end up being undefined.
Expand Down
2 changes: 1 addition & 1 deletion src/utils/typeUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export function getStringIfNotScalar(data: any) {

export function getStringValueIfDefined<
T extends undefined | Exclude<any, undefined>,
R = T extends undefined ? undefined : string
R = T extends undefined ? undefined : string,
>(value: T): R {
if (typeof value === "undefined") return undefined as R;
if (typeof value === "object") return JSON.stringify(value) as R; // handles dicts, arrays, null, date (all obj)
Expand Down
2 changes: 1 addition & 1 deletion src/variableParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export function getEnvironments(bundleContent: string | undefined, varFileConten
export function loadVariables(
envName: string | undefined,
bundleContent: string | undefined,
varFileContents: string[]
varFileContents: string[],
): Variables {
if (!envName) return {};

Expand Down
10 changes: 10 additions & 0 deletions tests/execute_got_request.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import got from "got";

import { executeGotRequest } from "../src";

test("execute simple-get GOT request", async () => {
const response = await executeGotRequest(got("https://postman-echo.com/get", {method: "GET"}));
expect(response.byteLength).toBeGreaterThan(0);
expect(response.executionTime).toBeGreaterThan(0);
expect(response.error.length).toBeLessThan(1);
});

0 comments on commit 2f88288

Please sign in to comment.