Open-Source API Development Ecosystem • https://hoppscotch.io • Offline, On-Prem & Cloud • Web, Desktop & CLI • Open-Source Alternative to Postman, Insomnia
TypeScript apiapi-clientapi-restapi-testingdeveloper-toolsgraphqlhttphttp-clientpwarestrest-apispatestingtesting-toolstoolsvuevuejswebsocket
fix(cli): environment resolution in the single-entry export format (#3687)
James George0df194f9c5f7
diff --git a/packages/hoppscotch-cli/package.json b/packages/hoppscotch-cli/package.jsonindex ca22d0873..0a204dbcc 100644--- a/packages/hoppscotch-cli/package.json+++ b/packages/hoppscotch-cli/package.json@@ -1,6 +1,6 @@{"name": "@hoppscotch/cli",- "version": "0.5.0",+ "version": "0.5.1","description": "A CLI to run Hoppscotch test scripts in CI environments.","homepage": "https://hoppscotch.io","main": "dist/index.js",diff --git a/packages/hoppscotch-cli/src/__tests__/commands/test.spec.ts b/packages/hoppscotch-cli/src/__tests__/commands/test.spec.tsindex c706c7915..db69d61d5 100644--- a/packages/hoppscotch-cli/src/__tests__/commands/test.spec.ts+++ b/packages/hoppscotch-cli/src/__tests__/commands/test.spec.ts@@ -118,6 +118,15 @@ describe("Test 'hopp test <file> --env <file>' command:", () => {const { error } = await runCLI(args);expect(error).toBeNull();});++ test("Correctly resolves environment variables referenced in the request body", async () => {+ const COLL_PATH = getTestJsonFilePath("req-body-env-vars-coll.json");+ const ENVS_PATH = getTestJsonFilePath("req-body-env-vars-envs.json");+ const args = `test ${COLL_PATH} --env ${ENVS_PATH}`;++ const { error } = await runCLI(args);+ expect(error).toBeNull();+ });});describe("Test 'hopp test <file> --delay <delay_in_ms>' command:", () => {diff --git a/packages/hoppscotch-cli/src/__tests__/samples/req-body-env-vars-coll.json b/packages/hoppscotch-cli/src/__tests__/samples/req-body-env-vars-coll.jsonnew file mode 100644index 000000000..1bcb22649--- /dev/null+++ b/packages/hoppscotch-cli/src/__tests__/samples/req-body-env-vars-coll.json@@ -0,0 +1,30 @@+{+ "v": 2,+ "name": "Test environment variables in request body",+ "folders": [],+ "requests": [+ {+ "v": "1",+ "name": "test-request",+ "endpoint": "https://echo.hoppscotch.io",+ "method": "POST",+ "headers": [],+ "params": [],+ "auth": {+ "authType": "none",+ "authActive": true+ },+ "body": {+ "contentType": "application/json",+ "body": "{\n \"firstName\": \"<<firstName>>\",\n \"lastName\": \"<<lastName>>\",\n \"greetText\": \"<<salutation>>, <<fullName>>\",\n \"fullName\": \"<<fullName>>\",\n \"id\": \"<<id>>\"\n}"+ },+ "preRequestScript": "",+ "testScript": "pw.test(\"Status code is 200\", ()=> {\n pw.expect(pw.response.status).toBe(200);\n});\n\npw.test(\"Successfully resolves environments recursively\", ()=> {\n pw.expect(pw.env.getResolve(\"recursiveVarX\")).toBe(\"Hello\")\n pw.expect(pw.env.getResolve(\"recursiveVarY\")).toBe(\"Hello\")\n pw.expect(pw.env.getResolve(\"salutation\")).toBe(\"Hello\")\n});\n\npw.test(\"Successfully resolves environments referenced in the request body\", () => {\n const expectedId = \"7\"\n const expectedFirstName = \"John\"\n const expectedLastName = \"Doe\"\n const expectedFullName = `${expectedFirstName} ${expectedLastName}`\n const expectedGreetText = `Hello, ${expectedFullName}`\n\n pw.expect(pw.env.getResolve(\"recursiveVarX\")).toBe(\"Hello\")\n pw.expect(pw.env.getResolve(\"recursiveVarY\")).toBe(\"Hello\")\n pw.expect(pw.env.getResolve(\"salutation\")).toBe(\"Hello\")\n\n const { id, firstName, lastName, fullName, greetText } = JSON.parse(pw.response.body.data)\n\n pw.expect(id).toBe(expectedId)\n pw.expect(expectedFirstName).toBe(firstName)\n pw.expect(expectedLastName).toBe(lastName)\n pw.expect(fullName).toBe(expectedFullName)\n pw.expect(greetText).toBe(expectedGreetText)\n});"+ }+ ],+ "auth": {+ "authType": "none",+ "authActive": true+ },+ "headers": []+}diff --git a/packages/hoppscotch-cli/src/__tests__/samples/req-body-env-vars-envs.json b/packages/hoppscotch-cli/src/__tests__/samples/req-body-env-vars-envs.jsonnew file mode 100644index 000000000..323bbb881--- /dev/null+++ b/packages/hoppscotch-cli/src/__tests__/samples/req-body-env-vars-envs.json@@ -0,0 +1,37 @@+{+ "name": "Response body sample",+ "variables": [+ {+ "key": "firstName",+ "value": "John"+ },+ {+ "key": "lastName",+ "value": "Doe"+ },+ {+ "key": "id",+ "value": "7"+ },+ {+ "key": "fullName",+ "value": "<<firstName>> <<lastName>>"+ },+ {+ "key": "recursiveVarX",+ "value": "<<recursiveVarY>>"+ },+ {+ "key": "recursiveVarY",+ "value": "<<salutation>>"+ },+ {+ "key": "salutation",+ "value": "Hello"+ },+ {+ "key": "greetText",+ "value": "<<salutation>> <<fullName>>"+ }+ ]+}\ No newline at end of filediff --git a/packages/hoppscotch-cli/src/__tests__/utils.ts b/packages/hoppscotch-cli/src/__tests__/utils.tsindex afa4494f5..afe437136 100644--- a/packages/hoppscotch-cli/src/__tests__/utils.ts+++ b/packages/hoppscotch-cli/src/__tests__/utils.ts@@ -22,12 +22,10 @@ export const trimAnsi = (target: string) => {export const getErrorCode = (out: string) => {const ansiTrimmedStr = trimAnsi(out);-return ansiTrimmedStr.split(" ")[0];};export const getTestJsonFilePath = (file: string) => {- const filePath = `${process.cwd()}/src/__tests__/samples/${file}`;-+ const filePath = resolve(__dirname, `../../src/__tests__/samples/${file}`);return filePath;};diff --git a/packages/hoppscotch-cli/src/options/test/env.ts b/packages/hoppscotch-cli/src/options/test/env.tsindex 8d4ded992..1cd8b140b 100644--- a/packages/hoppscotch-cli/src/options/test/env.ts+++ b/packages/hoppscotch-cli/src/options/test/env.ts@@ -37,8 +37,7 @@ export async function parseEnvsData(path: string) {envPairs.push({ key, value });}} else if (HoppEnvExportObjectResult.success) {- const { key, value } = HoppEnvExportObjectResult.data.variables[0];- envPairs.push({ key, value });+ envPairs.push(...HoppEnvExportObjectResult.data.variables);}return <HoppEnvs>{ global: [], selected: envPairs };