From d2865c637c2bf82d01b63ca1b62da87a39f1b059 Mon Sep 17 00:00:00 2001 From: Andrew Bastin Date: Sat, 25 Sep 2021 00:59:09 +0530 Subject: [PATCH 1/7] refactor: bring js-sandbox project to the monorepo --- packages/hoppscotch-js-sandbox/.eslintrc.js | 29 ++ packages/hoppscotch-js-sandbox/.gitignore | 5 + .../hoppscotch-js-sandbox/.prettierignore | 8 + packages/hoppscotch-js-sandbox/.prettierrc.js | 3 + packages/hoppscotch-js-sandbox/LICENSE | 21 + packages/hoppscotch-js-sandbox/README.md | 56 +++ packages/hoppscotch-js-sandbox/jest.config.js | 6 + packages/hoppscotch-js-sandbox/jest.setup.ts | 1 + packages/hoppscotch-js-sandbox/package.json | 59 +++ .../src/__tests__/preRequest.spec.ts | 64 +++ .../src/__tests__/testing/expect/toBe.spec.ts | 98 +++++ .../testing/expect/toBeLevelxxx.spec.ts | 361 +++++++++++++++++ .../__tests__/testing/expect/toBeType.spec.ts | 157 ++++++++ .../testing/expect/toHaveLength.spec.ts | 157 ++++++++ .../src/__tests__/testing/test-runner.spec.ts | 59 +++ .../src/__tests__/utils.spec.ts | 57 +++ packages/hoppscotch-js-sandbox/src/demo.ts | 55 +++ .../hoppscotch-js-sandbox/src/global.d.ts | 1 + packages/hoppscotch-js-sandbox/src/index.ts | 26 ++ .../hoppscotch-js-sandbox/src/preRequest.ts | 77 ++++ .../hoppscotch-js-sandbox/src/test-runner.ts | 367 ++++++++++++++++++ packages/hoppscotch-js-sandbox/src/utils.ts | 32 ++ packages/hoppscotch-js-sandbox/tsconfig.json | 26 ++ 23 files changed, 1725 insertions(+) create mode 100644 packages/hoppscotch-js-sandbox/.eslintrc.js create mode 100644 packages/hoppscotch-js-sandbox/.gitignore create mode 100644 packages/hoppscotch-js-sandbox/.prettierignore create mode 100644 packages/hoppscotch-js-sandbox/.prettierrc.js create mode 100644 packages/hoppscotch-js-sandbox/LICENSE create mode 100644 packages/hoppscotch-js-sandbox/README.md create mode 100644 packages/hoppscotch-js-sandbox/jest.config.js create mode 100644 packages/hoppscotch-js-sandbox/jest.setup.ts create mode 100644 packages/hoppscotch-js-sandbox/package.json create mode 100644 packages/hoppscotch-js-sandbox/src/__tests__/preRequest.spec.ts create mode 100644 packages/hoppscotch-js-sandbox/src/__tests__/testing/expect/toBe.spec.ts create mode 100644 packages/hoppscotch-js-sandbox/src/__tests__/testing/expect/toBeLevelxxx.spec.ts create mode 100644 packages/hoppscotch-js-sandbox/src/__tests__/testing/expect/toBeType.spec.ts create mode 100644 packages/hoppscotch-js-sandbox/src/__tests__/testing/expect/toHaveLength.spec.ts create mode 100644 packages/hoppscotch-js-sandbox/src/__tests__/testing/test-runner.spec.ts create mode 100644 packages/hoppscotch-js-sandbox/src/__tests__/utils.spec.ts create mode 100644 packages/hoppscotch-js-sandbox/src/demo.ts create mode 100644 packages/hoppscotch-js-sandbox/src/global.d.ts create mode 100644 packages/hoppscotch-js-sandbox/src/index.ts create mode 100644 packages/hoppscotch-js-sandbox/src/preRequest.ts create mode 100644 packages/hoppscotch-js-sandbox/src/test-runner.ts create mode 100644 packages/hoppscotch-js-sandbox/src/utils.ts create mode 100644 packages/hoppscotch-js-sandbox/tsconfig.json diff --git a/packages/hoppscotch-js-sandbox/.eslintrc.js b/packages/hoppscotch-js-sandbox/.eslintrc.js new file mode 100644 index 000000000..4a403131a --- /dev/null +++ b/packages/hoppscotch-js-sandbox/.eslintrc.js @@ -0,0 +1,29 @@ +module.exports = { + root: true, + env: { + node: true, + jest: true, + browser: true, + }, + parser: "@typescript-eslint/parser", + parserOptions: { + sourceType: "module", + requireConfigFile: false, + ecmaVersion: 2021, + }, + plugins: ["prettier"], + extends: [ + "prettier/prettier", + "eslint:recommended", + "plugin:prettier/recommended", + "plugin:@typescript-eslint/eslint-recommended", + "plugin:@typescript-eslint/recommended", + ], + rules: { + semi: [2, "never"], + "prettier/prettier": ["warn", { semi: false }], + "import/no-named-as-default": "off", + "no-undef": "off", + "@typescript-eslint/no-explicit-any": "off", + }, +} diff --git a/packages/hoppscotch-js-sandbox/.gitignore b/packages/hoppscotch-js-sandbox/.gitignore new file mode 100644 index 000000000..3ca1e543f --- /dev/null +++ b/packages/hoppscotch-js-sandbox/.gitignore @@ -0,0 +1,5 @@ +node_modules +coverage +.husky/ +dist +lib \ No newline at end of file diff --git a/packages/hoppscotch-js-sandbox/.prettierignore b/packages/hoppscotch-js-sandbox/.prettierignore new file mode 100644 index 000000000..fac841ece --- /dev/null +++ b/packages/hoppscotch-js-sandbox/.prettierignore @@ -0,0 +1,8 @@ +.dependabot +.github +.hoppscotch +.vscode +package-lock.json +node_modules +dist +lib \ No newline at end of file diff --git a/packages/hoppscotch-js-sandbox/.prettierrc.js b/packages/hoppscotch-js-sandbox/.prettierrc.js new file mode 100644 index 000000000..a43ab29de --- /dev/null +++ b/packages/hoppscotch-js-sandbox/.prettierrc.js @@ -0,0 +1,3 @@ +module.exports = { + semi: false, +} diff --git a/packages/hoppscotch-js-sandbox/LICENSE b/packages/hoppscotch-js-sandbox/LICENSE new file mode 100644 index 000000000..81f990d60 --- /dev/null +++ b/packages/hoppscotch-js-sandbox/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2020 + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/packages/hoppscotch-js-sandbox/README.md b/packages/hoppscotch-js-sandbox/README.md new file mode 100644 index 000000000..e1193cb4d --- /dev/null +++ b/packages/hoppscotch-js-sandbox/README.md @@ -0,0 +1,56 @@ +
+ + The Hoppscotch UFO logo + +
+
+ +# Hoppscotch JavaScript Sandbox ALPHA + +
+ +This package deals with providing a JavaScript sandbox for executing various security sensitive external scripts. + +## Usage +- Install the [npm package](https://www.npmjs.com/package/@hoppscotch/js-sandbox) +``` +npm install --save @hoppscotch/js-sandbox +``` + +## How does this work ? +This package makes use of [quickjs-empscripten](https://www.npmjs.com/package/quickjs-emscripten) for building sandboxes for running external code on Hoppscotch. + +Currently implemented sandboxes: +- Hoppscotch Test Scripts + +## Development +- Clone the repository +``` +git clone https://github.com/hoppscotch/hopp-js-sandbox +``` + +- Install the package deps +``` +npm install +``` + +- Try out the demo [`src/demo.ts`](https://github.com/hoppscotch/hopp-js-sandbox/blob/main/src/demo.ts) using +``` +npm run demo +``` + +## Versioning +This project follows [Semantic Versioning](https://semver.org/) but as the project is still pre-1.0. The code and the public exposed API should not be considered to be fixed and stable. Things can change at any time! + +## License +This project is licensed under the [MIT License](https://opensource.org/licenses/MIT) - see [`LICENSE`](https://github.com/hoppscotch/hopp-js-sandbox/blob/main/LICENSE) for more details. + +
+
+
+
+ + + ###### built with ❤︎ by the [Hoppscotch Team](https://github.com/hoppscotch) and [contributors](https://github.com/AndrewBastin/hopp-js-sandbox/graphs/contributors). + +
\ No newline at end of file diff --git a/packages/hoppscotch-js-sandbox/jest.config.js b/packages/hoppscotch-js-sandbox/jest.config.js new file mode 100644 index 000000000..70aa99805 --- /dev/null +++ b/packages/hoppscotch-js-sandbox/jest.config.js @@ -0,0 +1,6 @@ +export default { + preset: "ts-jest", + testEnvironment: "node", + collectCoverage: true, + setupFilesAfterEnv: ['./jest.setup.ts'], +} diff --git a/packages/hoppscotch-js-sandbox/jest.setup.ts b/packages/hoppscotch-js-sandbox/jest.setup.ts new file mode 100644 index 000000000..562234114 --- /dev/null +++ b/packages/hoppscotch-js-sandbox/jest.setup.ts @@ -0,0 +1 @@ +require('@relmify/jest-fp-ts'); \ No newline at end of file diff --git a/packages/hoppscotch-js-sandbox/package.json b/packages/hoppscotch-js-sandbox/package.json new file mode 100644 index 000000000..8f62e8b60 --- /dev/null +++ b/packages/hoppscotch-js-sandbox/package.json @@ -0,0 +1,59 @@ +{ + "name": "@hoppscotch/js-sandbox", + "version": "0.3.1", + "description": "JavaScript sandboxes for running external scripts used by Hoppscotch clients", + "main": "./lib/index.js", + "types": "./lib/", + "type": "module", + "engines": { + "node": ">=14", + "pnpm": ">=3" + }, + "scripts": { + "demo": "esrun src/demo.ts", + "lint": "eslint --ext .ts,.js --ignore-path .gitignore", + "test": "npx jest", + "build": "npx tsc", + "clean": "npx tsc --build --clean", + "prepublish": "npm run build", + "do-lintfix": "pnpm run lint", + "do-test": "pnpm run test" + }, + "keywords": [ + "hoppscotch", + "sandbox", + "js-sandbox", + "apis", + "test-runner" + ], + "author": "The Hoppscotch Team (https://hoppscotch.com/)", + "license": "MIT", + "dependencies": { + "fp-ts": "^2.11.3", + "lodash": "^4.17.21", + "quickjs-emscripten": "^0.13.0" + }, + "devDependencies": { + "@digitak/esrun": "^1.2.4", + "@relmify/jest-fp-ts": "^1.1.1", + "@types/jest": "^26.0.23", + "@types/lodash": "^4.14.173", + "@types/node": "^15.12.5", + "@typescript-eslint/eslint-plugin": "^4.28.1", + "@typescript-eslint/parser": "^4.28.1", + "eslint": "^7.29.0", + "eslint-config-prettier": "^8.3.0", + "eslint-plugin-prettier": "^3.4.0", + "io-ts": "^2.2.16", + "jest": "^27.0.6", + "prettier": "^2.3.2", + "pretty-quick": "^3.1.1", + "ts-jest": "^27.0.3", + "typescript": "^4.3.5" + }, + "jest": { + "setupFilesAfterEnv": [ + "@relmify/jest-fp-ts" + ] + } +} diff --git a/packages/hoppscotch-js-sandbox/src/__tests__/preRequest.spec.ts b/packages/hoppscotch-js-sandbox/src/__tests__/preRequest.spec.ts new file mode 100644 index 000000000..2910c5d23 --- /dev/null +++ b/packages/hoppscotch-js-sandbox/src/__tests__/preRequest.spec.ts @@ -0,0 +1,64 @@ +import { execPreRequestScript } from "../preRequest" +import "@relmify/jest-fp-ts" + +describe("execPreRequestScript", () => { + test("returns the updated envirionment properly", () => { + return expect( + execPreRequestScript( + ` + pw.env.set("bob", "newbob") + `, + [{ key: "bob", value: "oldbob" }, { key: "foo", value: "bar" }] + )() + ).resolves.toEqualRight([ + { key: "bob", value: "newbob" }, + { key: "foo", value: "bar" } + ]) + }) + + test("fails if the key is not a string", () => { + return expect( + execPreRequestScript( + ` + pw.env.set(10, "newbob") + `, + [{ key: "bob", value: "oldbob" }, { key: "foo", value: "bar" }] + )() + ).resolves.toBeLeft() + }) + + test("fails if the value is not a string", () => { + return expect( + execPreRequestScript( + ` + pw.env.set("bob", 10) + `, + [{ key: "bob", value: "oldbob" }, { key: "foo", value: "bar" }] + )() + ).resolves.toBeLeft() + }) + + test("fails for invalid syntax", () => { + return expect( + execPreRequestScript( + ` + pw.env.set("bob", + `, + [{ key: "bob", value: "oldbob" }, { key: "foo", value: "bar" }] + )() + ).resolves.toBeLeft() + }) + + test("creates new env variable if doesn't exist", () => { + return expect( + execPreRequestScript( + ` + pw.env.set("foo", "bar") + `, + [] + )() + ).resolves.toEqualRight( + [{ key: "foo", value: "bar" }] + ) + }) +}) \ No newline at end of file diff --git a/packages/hoppscotch-js-sandbox/src/__tests__/testing/expect/toBe.spec.ts b/packages/hoppscotch-js-sandbox/src/__tests__/testing/expect/toBe.spec.ts new file mode 100644 index 000000000..9b7cccc31 --- /dev/null +++ b/packages/hoppscotch-js-sandbox/src/__tests__/testing/expect/toBe.spec.ts @@ -0,0 +1,98 @@ +import { execTestScript, TestResponse } from "../../../test-runner" +import "@relmify/jest-fp-ts" + +const fakeResponse: TestResponse = { + status: 200, + body: "hoi", + headers: [] +} + +describe("toBe", () => { + describe("general assertion (no negation)", () => { + test("expect equals expected passes assertion", () => { + return expect( + execTestScript( + ` + pw.expect(2).toBe(2) + `, + fakeResponse + )() + ).resolves.toEqualRight([ + expect.objectContaining({ + expectResults: [{ status: "pass", message: "Expected '2' to be '2'" }], + }), + ]) + }) + + test("expect not equals expected fails assertion", () => { + return expect( + execTestScript( + ` + pw.expect(2).toBe(4) + `, + fakeResponse + )() + ).resolves.toEqualRight([ + expect.objectContaining({ + expectResults: [{ status: "fail", message: "Expected '2' to be '4'" }], + }), + ]) + }) + }) + + describe("general assertion (with negation)", () => { + test("expect equals expected fails assertion", () => { + return expect( + execTestScript( + ` + pw.expect(2).not.toBe(2) + `, + fakeResponse + )() + ).resolves.toEqualRight([ + expect.objectContaining({ + expectResults: [{ + status: "fail", + message: "Expected '2' to not be '2'", + }], + }), + ]) + }) + + test("expect not equals expected passes assertion", () => { + return expect( + execTestScript( + ` + pw.expect(2).not.toBe(4) + `, + fakeResponse + )() + ).resolves.toEqualRight([ + expect.objectContaining({ + expectResults: [{ + status: "pass", + message: "Expected '2' to not be '4'", + }], + }), + ]) + }) + }) +}) + +test("strict checks types", () => { + return expect( + execTestScript( + ` + pw.expect(2).toBe("2") + `, + fakeResponse + )() + ).resolves.toEqualRight([ + expect.objectContaining({ + expectResults: [{ + status: "fail", + message: "Expected '2' to be '2'", + }], + }), + ]) +}) diff --git a/packages/hoppscotch-js-sandbox/src/__tests__/testing/expect/toBeLevelxxx.spec.ts b/packages/hoppscotch-js-sandbox/src/__tests__/testing/expect/toBeLevelxxx.spec.ts new file mode 100644 index 000000000..46da1de64 --- /dev/null +++ b/packages/hoppscotch-js-sandbox/src/__tests__/testing/expect/toBeLevelxxx.spec.ts @@ -0,0 +1,361 @@ +import { execTestScript, TestResponse } from "../../../test-runner" +import "@relmify/jest-fp-ts" + +const fakeResponse: TestResponse = { + status: 200, + body: "hoi", + headers: [] +} + +describe("toBeLevel2xx", () => { + test("assertion passes for 200 series with no negation", async () => { + for (let i = 200; i < 300; i++) { + await expect( + execTestScript(`pw.expect(${i}).toBeLevel2xx()`, fakeResponse)() + ).resolves.toEqualRight([ + expect.objectContaining({ + expectResults: [{ + status: "pass", + message: `Expected '${i}' to be 200-level status`, + }], + }), + ]) + } + }) + + test("assertion fails for non 200 series with no negation", async () => { + for (let i = 300; i < 500; i++) { + await expect( + execTestScript(`pw.expect(${i}).toBeLevel2xx()`, fakeResponse)() + ).resolves.toEqualRight([ + expect.objectContaining({ + expectResults: [{ + status: "fail", + message: `Expected '${i}' to be 200-level status`, + }], + }), + ]) + } + }) + + test("give error if the expect value was not a number with no negation", async () => { + await expect( + execTestScript(`pw.expect("foo").toBeLevel2xx()`, fakeResponse)() + ).resolves.toEqualRight([ + expect.objectContaining({ + expectResults: [{ + status: "error", + message: "Expected 200-level status but could not parse value 'foo'", + }], + }) + ]) + }) + + test("assertion fails for 200 series with negation", async () => { + for (let i = 200; i < 300; i++) { + await expect( + execTestScript(`pw.expect(${i}).not.toBeLevel2xx()`, fakeResponse)() + ).resolves.toEqualRight([ + expect.objectContaining({ + expectResults: [{ + status: "fail", + message: `Expected '${i}' to not be 200-level status`, + }], + }), + ]) + } + }) + + test("assertion passes for non 200 series with negation", async () => { + for (let i = 300; i < 500; i++) { + await expect( + execTestScript(`pw.expect(${i}).not.toBeLevel2xx()`, fakeResponse)() + ).resolves.toEqualRight([ + expect.objectContaining({ + expectResults: [{ + status: "pass", + message: `Expected '${i}' to not be 200-level status`, + }], + }), + ]) + } + }) + + test("give error if the expect value was not a number with negation", async () => { + await expect( + execTestScript(`pw.expect("foo").not.toBeLevel2xx()`, fakeResponse)() + ).resolves.toEqualRight([ + expect.objectContaining({ + expectResults: [{ + status: "error", + message: "Expected 200-level status but could not parse value 'foo'", + }], + }) + ]) + }) +}) + +describe("toBeLevel3xx", () => { + test("assertion passes for 300 series with no negation", async () => { + for (let i = 300; i < 400; i++) { + await expect( + execTestScript(`pw.expect(${i}).toBeLevel3xx()`, fakeResponse)() + ).resolves.toEqualRight([ + expect.objectContaining({ + expectResults: [{ + status: "pass", + message: `Expected '${i}' to be 300-level status`, + }], + }), + ]) + } + }) + + test("assertion fails for non 300 series with no negation", async () => { + for (let i = 400; i < 500; i++) { + await expect( + execTestScript(`pw.expect(${i}).toBeLevel3xx()`, fakeResponse)() + ).resolves.toEqualRight([ + expect.objectContaining({ + expectResults: [{ + status: "fail", + message: `Expected '${i}' to be 300-level status`, + }], + }), + ]) + } + }) + + test("give error if the expect value is not a number without negation", () => { + return expect( + execTestScript(`pw.expect("foo").toBeLevel3xx()`, fakeResponse)() + ).resolves.toEqualRight([ + expect.objectContaining({ + expectResults: [{ + status: "error", + message: "Expected 300-level status but could not parse value 'foo'", + }], + }) + ]) + }) + + test("assertion fails for 400 series with negation", async () => { + for (let i = 300; i < 400; i++) { + await expect( + execTestScript(`pw.expect(${i}).not.toBeLevel3xx()`, fakeResponse)() + ).resolves.toEqualRight([ + expect.objectContaining({ + expectResults: [{ + status: "fail", + message: `Expected '${i}' to not be 300-level status`, + }], + }), + ]) + } + }) + + test("assertion passes for non 200 series with negation", async () => { + for (let i = 400; i < 500; i++) { + await expect( + execTestScript(`pw.expect(${i}).not.toBeLevel3xx()`, fakeResponse)() + ).resolves.toEqualRight([ + expect.objectContaining({ + expectResults: [{ + status: "pass", + message: `Expected '${i}' to not be 300-level status`, + }], + }), + ]) + } + }) + + test("give error if the expect value is not a number with negation", () => { + return expect( + execTestScript(`pw.expect("foo").not.toBeLevel3xx()`, fakeResponse)() + ).resolves.toEqualRight([ + expect.objectContaining({ + expectResults: [{ + status: "error", + message: "Expected 300-level status but could not parse value 'foo'", + }] + }) + ]) + }) + +}) + +describe("toBeLevel4xx", () => { + test("assertion passes for 400 series with no negation", async () => { + for (let i = 400; i < 500; i++) { + await expect( + execTestScript(`pw.expect(${i}).toBeLevel4xx()`, fakeResponse)() + ).resolves.toEqualRight([ + expect.objectContaining({ + expectResults: [{ + status: "pass", + message: `Expected '${i}' to be 400-level status`, + }], + }), + ]) + } + }) + + test("assertion fails for non 400 series with no negation", async () => { + for (let i = 500; i < 600; i++) { + await expect( + execTestScript(`pw.expect(${i}).toBeLevel4xx()`, fakeResponse)() + ).resolves.toEqualRight([ + expect.objectContaining({ + expectResults: [{ + status: "fail", + message: `Expected '${i}' to be 400-level status`, + }], + }), + ]) + } + }) + + test("give error if the expected value is not a number without negation", () => { + return expect( + execTestScript(`pw.expect("foo").toBeLevel4xx()`, fakeResponse)() + ).resolves.toEqualRight([ + expect.objectContaining({ + expectResults: [{ + status: "error", + message: "Expected 400-level status but could not parse value 'foo'", + }], + }) + ]) + }) + + test("assertion fails for 400 series with negation", async () => { + for (let i = 400; i < 500; i++) { + await expect( + execTestScript(`pw.expect(${i}).not.toBeLevel4xx()`, fakeResponse)() + ).resolves.toEqualRight([ + expect.objectContaining({ + expectResults: [{ + status: "fail", + message: `Expected '${i}' to not be 400-level status`, + }], + }), + ]) + } + }) + + test("assertion passes for non 400 series with negation", async () => { + for (let i = 500; i < 600; i++) { + await expect( + execTestScript(`pw.expect(${i}).not.toBeLevel4xx()`, fakeResponse)() + ).resolves.toEqualRight([ + expect.objectContaining({ + expectResults: [{ + status: "pass", + message: `Expected '${i}' to not be 400-level status`, + }], + }), + ]) + } + }) + + test("give error if the expected value is not a number with negation", () => { + return expect( + execTestScript(`pw.expect("foo").not.toBeLevel4xx()`, fakeResponse)() + ).resolves.toEqualRight([ + expect.objectContaining({ + expectResults: [{ + status: "error", + message: "Expected 400-level status but could not parse value 'foo'", + }], + }) + ]) + }) +}) + +describe("toBeLevel5xx", () => { + test("assertion passes for 500 series with no negation", async () => { + for (let i = 500; i < 600; i++) { + await expect( + execTestScript(`pw.expect(${i}).toBeLevel5xx()`, fakeResponse)() + ).resolves.toEqualRight([ + expect.objectContaining({ + expectResults: [{ + status: "pass", + message: `Expected '${i}' to be 500-level status`, + }], + }), + ]) + } + }) + + test("assertion fails for non 500 series with no negation", async () => { + for (let i = 200; i < 500; i++) { + await expect( + execTestScript(`pw.expect(${i}).toBeLevel5xx()`, fakeResponse)() + ).resolves.toEqualRight([ + expect.objectContaining({ + expectResults: [{ + status: "fail", + message: `Expected '${i}' to be 500-level status`, + }], + }), + ]) + } + }) + + test("give error if the expect value is not a number with no negation", () => { + return expect( + execTestScript(`pw.expect("foo").toBeLevel5xx()`, fakeResponse)() + ).resolves.toEqualRight([ + expect.objectContaining({ + expectResults: [{ + status: "error", + message: "Expected 500-level status but could not parse value 'foo'", + }], + }) + ]) + }) + + test("assertion fails for 500 series with negation", async () => { + for (let i = 500; i < 600; i++) { + await expect( + execTestScript(`pw.expect(${i}).not.toBeLevel5xx()`, fakeResponse)() + ).resolves.toEqualRight([ + expect.objectContaining({ + expectResults: [{ + status: "fail", + message: `Expected '${i}' to not be 500-level status`, + }], + }), + ]) + } + }) + + test("assertion passes for non 500 series with negation", async () => { + for (let i = 200; i < 500; i++) { + await expect( + execTestScript(`pw.expect(${i}).not.toBeLevel5xx()`, fakeResponse)() + ).resolves.toEqualRight([ + expect.objectContaining({ + expectResults: [{ + status: "pass", + message: `Expected '${i}' to not be 500-level status`, + }], + }), + ]) + } + }) + + test("give error if the expect value is not a number with negation", () => { + return expect( + execTestScript(`pw.expect("foo").not.toBeLevel5xx()`, fakeResponse)() + ).resolves.toEqualRight([ + expect.objectContaining({ + expectResults: [{ + status: "error", + message: "Expected 500-level status but could not parse value 'foo'", + }], + }) + ]) + }) +}) \ No newline at end of file diff --git a/packages/hoppscotch-js-sandbox/src/__tests__/testing/expect/toBeType.spec.ts b/packages/hoppscotch-js-sandbox/src/__tests__/testing/expect/toBeType.spec.ts new file mode 100644 index 000000000..f9c5132d0 --- /dev/null +++ b/packages/hoppscotch-js-sandbox/src/__tests__/testing/expect/toBeType.spec.ts @@ -0,0 +1,157 @@ +import { execTestScript, TestResponse } from "../../../test-runner" + +const fakeResponse: TestResponse = { + status: 200, + body: "hoi", + headers: [] +} + +describe("toBeType", () => { + test("asserts true for valid type expectations with no negation", () => { + return expect( + execTestScript( + ` + pw.expect(2).toBeType("number") + pw.expect("2").toBeType("string") + pw.expect(true).toBeType("boolean") + pw.expect({}).toBeType("object") + pw.expect(undefined).toBeType("undefined") + `, fakeResponse + )() + ).resolves.toEqualRight([ + expect.objectContaining({ + expectResults: [ + { status: "pass", message: `Expected '2' to be type 'number'` }, + { status: "pass", message: `Expected '2' to be type 'string'` }, + { status: "pass", message: `Expected 'true' to be type 'boolean'` }, + { status: "pass", message: `Expected '[object Object]' to be type 'object'` }, + { status: "pass", message: `Expected 'undefined' to be type 'undefined'` }, + ], + }), + ]) + }) + + test("asserts false for invalid type expectations with no negation", () => { + return expect( + execTestScript( + ` + pw.expect(2).toBeType("string") + pw.expect("2").toBeType("number") + pw.expect(true).toBeType("string") + pw.expect({}).toBeType("number") + pw.expect(undefined).toBeType("number") + `, + fakeResponse + )() + ).resolves.toEqualRight([ + expect.objectContaining({ + expectResults: [ + { status: "fail", message: `Expected '2' to be type 'string'`}, + { status: "fail", message: `Expected '2' to be type 'number'`}, + { status: "fail", message: `Expected 'true' to be type 'string'`}, + { status: "fail", message: `Expected '[object Object]' to be type 'number'`}, + { status: "fail", message: `Expected 'undefined' to be type 'number'`}, + ], + }), + ]) + }) + + test("asserts false for valid type expectations with negation", () => { + return expect( + execTestScript( + ` + pw.expect(2).not.toBeType("number") + pw.expect("2").not.toBeType("string") + pw.expect(true).not.toBeType("boolean") + pw.expect({}).not.toBeType("object") + pw.expect(undefined).not.toBeType("undefined") + `, fakeResponse + )() + ).resolves.toEqualRight([ + expect.objectContaining({ + expectResults: [ + { status: "fail", message: `Expected '2' to not be type 'number'` }, + { status: "fail", message: `Expected '2' to not be type 'string'` }, + { status: "fail", message: `Expected 'true' to not be type 'boolean'` }, + { status: "fail", message: `Expected '[object Object]' to not be type 'object'` }, + { status: "fail", message: `Expected 'undefined' to not be type 'undefined'` }, + ], + }), + ]) + }) + + test("asserts true for invalid type expectations with negation", () => { + return expect( + execTestScript( + ` + pw.expect(2).not.toBeType("string") + pw.expect("2").not.toBeType("number") + pw.expect(true).not.toBeType("string") + pw.expect({}).not.toBeType("number") + pw.expect(undefined).not.toBeType("number") + `, + fakeResponse + )() + ).resolves.toEqualRight([ + expect.objectContaining({ + expectResults: [ + { status: "pass", message: `Expected '2' to not be type 'string'` }, + { status: "pass", message: `Expected '2' to not be type 'number'` }, + { status: "pass", message: `Expected 'true' to not be type 'string'` }, + { status: "pass", message: `Expected '[object Object]' to not be type 'number'` }, + { status: "pass", message: `Expected 'undefined' to not be type 'number'` }, + ], + }), + ]) + }) + + test("gives error for invalid type names without negation", () => { + return expect( + execTestScript( + ` + pw.expect(2).toBeType("foo") + pw.expect("2").toBeType("bar") + pw.expect(true).toBeType("baz") + pw.expect({}).toBeType("qux") + pw.expect(undefined).toBeType("quux") + `, + fakeResponse + )() + ).resolves.toEqualRight([ + expect.objectContaining({ + expectResults: [ + { status: "error", message: `Argument for toBeType should be "string", "boolean", "number", "object", "undefined", "bigint", "symbol" or "function"` }, + { status: "error", message: `Argument for toBeType should be "string", "boolean", "number", "object", "undefined", "bigint", "symbol" or "function"` }, + { status: "error", message: `Argument for toBeType should be "string", "boolean", "number", "object", "undefined", "bigint", "symbol" or "function"` }, + { status: "error", message: `Argument for toBeType should be "string", "boolean", "number", "object", "undefined", "bigint", "symbol" or "function"` }, + { status: "error", message: `Argument for toBeType should be "string", "boolean", "number", "object", "undefined", "bigint", "symbol" or "function"` }, + ] + }) + ]) + }) + + test("gives error for invalid type names with negation", () => { + return expect( + execTestScript( + ` + pw.expect(2).not.toBeType("foo") + pw.expect("2").not.toBeType("bar") + pw.expect(true).not.toBeType("baz") + pw.expect({}).not.toBeType("qux") + pw.expect(undefined).not.toBeType("quux") + `, + fakeResponse + )() + ).resolves.toEqualRight([ + expect.objectContaining({ + expectResults: [ + { status: "error", message: `Argument for toBeType should be "string", "boolean", "number", "object", "undefined", "bigint", "symbol" or "function"` }, + { status: "error", message: `Argument for toBeType should be "string", "boolean", "number", "object", "undefined", "bigint", "symbol" or "function"` }, + { status: "error", message: `Argument for toBeType should be "string", "boolean", "number", "object", "undefined", "bigint", "symbol" or "function"` }, + { status: "error", message: `Argument for toBeType should be "string", "boolean", "number", "object", "undefined", "bigint", "symbol" or "function"` }, + { status: "error", message: `Argument for toBeType should be "string", "boolean", "number", "object", "undefined", "bigint", "symbol" or "function"` }, + ] + }) + ]) + }) +}) diff --git a/packages/hoppscotch-js-sandbox/src/__tests__/testing/expect/toHaveLength.spec.ts b/packages/hoppscotch-js-sandbox/src/__tests__/testing/expect/toHaveLength.spec.ts new file mode 100644 index 000000000..1d8db0c85 --- /dev/null +++ b/packages/hoppscotch-js-sandbox/src/__tests__/testing/expect/toHaveLength.spec.ts @@ -0,0 +1,157 @@ +import { execTestScript, TestResponse } from "../../../test-runner" + +const fakeResponse: TestResponse = { + status: 200, + body: "hoi", + headers: [] +} + +describe("toHaveLength", () => { + test("asserts true for valid lengths with no negation", () => { + return expect( + execTestScript( + ` + pw.expect([1, 2, 3, 4]).toHaveLength(4) + pw.expect([]).toHaveLength(0) + `, + fakeResponse + )() + ).resolves.toEqualRight([ + expect.objectContaining({ + expectResults: [ + { status: "pass", message: "Expected the array to be of length '4'" }, + { status: "pass", message: "Expected the array to be of length '0'" }, + ], + }), + ]) + }) + + test("asserts false for invalid lengths with no negation", () => { + return expect( + execTestScript( + ` + pw.expect([]).toHaveLength(4) + pw.expect([1, 2, 3, 4]).toHaveLength(0) + `, + fakeResponse + )() + ).resolves.toEqualRight([ + expect.objectContaining({ + expectResults: [ + { status: "fail", message: "Expected the array to be of length '4'" }, + { status: "fail", message: "Expected the array to be of length '0'" }, + ], + }), + ]) + }) + + test("asserts false for valid lengths with negation", () => { + return expect( + execTestScript( + ` + pw.expect([1, 2, 3, 4]).not.toHaveLength(4) + pw.expect([]).not.toHaveLength(0) + `, + fakeResponse + )() + ).resolves.toEqualRight([ + expect.objectContaining({ + expectResults: [ + { status: "fail", message: "Expected the array to not be of length '4'" }, + { status: "fail", message: "Expected the array to not be of length '0'" }, + ], + }), + ]) + }) + + test("asserts true for invalid lengths with negation", () => { + return expect( + execTestScript( + ` + pw.expect([]).not.toHaveLength(4) + pw.expect([1, 2, 3, 4]).not.toHaveLength(0) + `, + fakeResponse + )() + ).resolves.toEqualRight([ + expect.objectContaining({ + expectResults: [ + { status: "pass", message: "Expected the array to not be of length '4'" }, + { status: "pass", message: "Expected the array to not be of length '0'" }, + ], + }), + ]) + }) + + test("gives error if not called on an array or a string with no negation", () => { + return expect( + execTestScript( + ` + pw.expect(5).toHaveLength(0) + pw.expect(true).toHaveLength(0) + `, + fakeResponse + )() + ).resolves.toEqualRight([ + expect.objectContaining({ + expectResults: [ + { status: "error", message: "Expected toHaveLength to be called for an array or string" }, + { status: "error", message: "Expected toHaveLength to be called for an array or string" }, + ] + }) + ]) + }) + + test("gives error if not called on an array or a string with negation", () => { + return expect( + execTestScript( + ` + pw.expect(5).not.toHaveLength(0) + pw.expect(true).not.toHaveLength(0) + `, + fakeResponse + )() + ).resolves.toEqualRight([ + expect.objectContaining({ + expectResults: [ + { status: "error", message: "Expected toHaveLength to be called for an array or string" }, + { status: "error", message: "Expected toHaveLength to be called for an array or string" }, + ] + }) + ]) + }) + + test("gives an error if toHaveLength parameter is not a number without negation", () => { + return expect( + execTestScript( + ` + pw.expect([1, 2, 3, 4]).toHaveLength("a") + `, + fakeResponse + )() + ).resolves.toEqualRight([ + expect.objectContaining({ + expectResults: [ + { status: "error", message: "Argument for toHaveLength should be a number" }, + ], + }) + ]) + }) + + test("gives an error if toHaveLength parameter is not a number with negation", () => { + return expect( + execTestScript( + ` + pw.expect([1, 2, 3, 4]).not.toHaveLength("a") + `, + fakeResponse + )() + ).resolves.toEqualRight([ + expect.objectContaining({ + expectResults: [ + { status: "error", message: "Argument for toHaveLength should be a number" }, + ], + }) + ]) + }) +}) diff --git a/packages/hoppscotch-js-sandbox/src/__tests__/testing/test-runner.spec.ts b/packages/hoppscotch-js-sandbox/src/__tests__/testing/test-runner.spec.ts new file mode 100644 index 000000000..dc93bed2b --- /dev/null +++ b/packages/hoppscotch-js-sandbox/src/__tests__/testing/test-runner.spec.ts @@ -0,0 +1,59 @@ +import { execTestScript, TestResponse } from "../../test-runner" + +const fakeResponse: TestResponse = { + status: 200, + body: "hoi", + headers: [] +} + +describe("execTestScript function behavior", () => { + test("returns a resolved promise for a valid test scripts with all green", () => { + return expect( + execTestScript( + ` + pw.test("Arithmetic operations", () => { + const size = 500 + 500; + pw.expect(size).toBe(1000); + pw.expect(size - 500).toBe(500); + pw.expect(size * 4).toBe(4000); + pw.expect(size / 4).toBe(250); + }); + `, + fakeResponse + )() + ).resolves.toBeRight(); + }) + + test("resolves for tests with failed expectations", () => { + return expect( + execTestScript( + ` + pw.test("Arithmetic operations", () => { + const size = 500 + 500; + pw.expect(size).toBe(1000); + pw.expect(size - 500).not.toBe(500); + pw.expect(size * 4).toBe(4000); + pw.expect(size / 4).not.toBe(250); + }); + `, fakeResponse + )() + ).resolves.toBeRight() + }) + + // TODO: We need a more concrete behavior for this + test("rejects for invalid syntax on tests", () => { + return expect( + execTestScript( + ` + pw.test("Arithmetic operations", () => { + const size = 500 + 500; + pw.expect(size). + pw.expect(size - 500).not.toBe(500); + pw.expect(size * 4).toBe(4000); + pw.expect(size / 4).not.toBe(250); + }); + `, fakeResponse + )() + ).resolves.toBeLeft() + }) +}) diff --git a/packages/hoppscotch-js-sandbox/src/__tests__/utils.spec.ts b/packages/hoppscotch-js-sandbox/src/__tests__/utils.spec.ts new file mode 100644 index 000000000..50d03fdb5 --- /dev/null +++ b/packages/hoppscotch-js-sandbox/src/__tests__/utils.spec.ts @@ -0,0 +1,57 @@ +import { match } from "fp-ts/lib/Either" +import { pipe } from "fp-ts/lib/function" +import * as QuickJS from "quickjs-emscripten" +import { marshalObjectToVM } from "../utils" + +let vm: QuickJS.QuickJSVm + +beforeAll(async () => { + const qjs = await QuickJS.getQuickJS() + vm = qjs.createVm() +}) + +afterAll(() => { + vm.dispose() +}) + +describe("marshalObjectToVM", () => { + + test("successfully marshals simple object into the vm", () => { + const testObj = { + a: 1 + } + + const objVMHandle: QuickJS.QuickJSHandle | null = pipe( + marshalObjectToVM(vm, testObj), + match( + (e) => null, + (result) => result + ) + ) + + expect(objVMHandle).not.toBeNull() + expect(vm.dump(objVMHandle!)).toEqual(testObj) + + objVMHandle!.dispose() + }) + + test("fails marshalling cyclic object into vm", () => { + const testObj = { + a: 1, + b: null as any + } + + testObj.b = testObj + + const objVMHandle: QuickJS.QuickJSHandle | null = pipe( + marshalObjectToVM(vm, testObj), + match( + (e) => null, + (result) => result + ) + ) + + expect(objVMHandle).toBeNull() + }) + +}) \ No newline at end of file diff --git a/packages/hoppscotch-js-sandbox/src/demo.ts b/packages/hoppscotch-js-sandbox/src/demo.ts new file mode 100644 index 000000000..8723dc608 --- /dev/null +++ b/packages/hoppscotch-js-sandbox/src/demo.ts @@ -0,0 +1,55 @@ +import { runTestScript } from "./index" +import { TestResponse } from "./test-runner" + + +const dummyResponse: TestResponse = { + status: 200, + body: "hoi", + headers: [] +}; + +// eslint-disable-next-line prettier/prettier +(async () => { + console.dir( + await runTestScript( + ` + pw.test("Arithmetic operations and toBe", () => { + const size = 500 + 500; + pw.expect(size).toBe(1000); + pw.expect(size - 500).toBe(500); + pw.expect(size * 4).toBe(4000); + pw.expect(size / 4).toBe(250); + }); + pw.test("toBeLevelxxx", () => { + pw.expect(200).toBeLevel2xx(); + pw.expect(204).toBeLevel2xx(); + pw.expect(300).not.toBeLevel2xx(); + pw.expect(300).toBeLevel3xx(); + pw.expect(304).toBeLevel3xx(); + pw.expect(204).not.toBeLevel3xx(); + pw.expect(401).toBeLevel4xx(); + pw.expect(404).toBeLevel4xx(); + pw.expect(204).not.toBeLevel4xx(); + pw.expect(501).toBeLevel5xx(); + pw.expect(504).toBeLevel5xx(); + pw.expect(204).not.toBeLevel5xx(); + }); + pw.test("toBeType", () => { + pw.expect("hello").toBeType("string"); + pw.expect(10).toBeType("number"); + pw.expect(true).toBeType("boolean"); + pw.expect("deffonotanumber").not.toBeType("number"); + }); + pw.test("toHaveLength", () => { + const arr = [1, 2, 3]; + pw.expect(arr).toHaveLength(3); + pw.expect(arr).not.toHaveLength(4); + }); + `, + dummyResponse + ), + { + depth: 100, + } + ) +})() diff --git a/packages/hoppscotch-js-sandbox/src/global.d.ts b/packages/hoppscotch-js-sandbox/src/global.d.ts new file mode 100644 index 000000000..7a83e2841 --- /dev/null +++ b/packages/hoppscotch-js-sandbox/src/global.d.ts @@ -0,0 +1 @@ +import '@relmify/jest-fp-ts'; \ No newline at end of file diff --git a/packages/hoppscotch-js-sandbox/src/index.ts b/packages/hoppscotch-js-sandbox/src/index.ts new file mode 100644 index 000000000..1150342fa --- /dev/null +++ b/packages/hoppscotch-js-sandbox/src/index.ts @@ -0,0 +1,26 @@ +import { pipe } from "fp-ts/lib/function" +import { chain, right } from "fp-ts/lib/TaskEither" +import { execPreRequestScript } from "./preRequest" +import { execTestScript, TestResponse, TestDescriptor as _TestDescriptor } from "./test-runner" + +export type TestDescriptor = _TestDescriptor +/** + * Executes a given test script on the test-runner sandbox + * @param testScript The string of the script to run + * @returns A TaskEither with an error message or a TestDescriptor with the final status + */ +export const runTestScript = ( + testScript: string, + response: TestResponse +) => pipe( + execTestScript(testScript, response), + chain((results) => right(results[0])) // execTestScript returns an array of descriptors with a single element (extract that) +) + +/** + * Executes a given pre-request script on the sandbox + * @param preRequestScript The script to run + * @param env The envirionment variables active + * @returns A TaskEither with an error message or an array of the final environments with the all the script values applied + */ +export const runPreRequestScript = execPreRequestScript \ No newline at end of file diff --git a/packages/hoppscotch-js-sandbox/src/preRequest.ts b/packages/hoppscotch-js-sandbox/src/preRequest.ts new file mode 100644 index 000000000..ade271980 --- /dev/null +++ b/packages/hoppscotch-js-sandbox/src/preRequest.ts @@ -0,0 +1,77 @@ +import { pipe } from "fp-ts/lib/function"; +import { chain, TaskEither, tryCatch, right, left } from "fp-ts/lib/TaskEither"; +import * as qjs from "quickjs-emscripten"; +import clone from "lodash/clone"; + +type EnvEntry = { + key: string; + value: string; +}; + +export const execPreRequestScript = ( + preRequestScript: string, + env: EnvEntry[] +): TaskEither => pipe( + tryCatch( + async () => await qjs.getQuickJS(), + (reason) => `QuickJS initialization failed: ${reason}` + ), + chain( + (QuickJS) => { + const finalEnv = clone(env) + + const vm = QuickJS.createVm() + + const pwHandle = vm.newObject() + + const envHandle = vm.newObject() + + const envSetFuncHandle = vm.newFunction("set", (keyHandle, valueHandle) => { + const key = vm.dump(keyHandle) + const value = vm.dump(valueHandle) + + if (typeof key !== "string") return { + error: vm.newString("Expected key to be a string") + } + + if (typeof value !== "string") return { + error: vm.newString("Expected value to be a string") + } + + const keyIndex = finalEnv.findIndex((env) => env.key === key) + + if (keyIndex === -1) { + finalEnv.push({ key, value }) + } else { + finalEnv[keyIndex] = { key, value } + } + + return { + value: vm.undefined + } + }) + + vm.setProp(envHandle, "set", envSetFuncHandle) + envSetFuncHandle.dispose() + + vm.setProp(pwHandle, "env", envHandle) + envHandle.dispose() + + vm.setProp(vm.global, "pw", pwHandle) + pwHandle.dispose() + + const evalRes = vm.evalCode(preRequestScript) + + if (evalRes.error) { + const errorData = vm.dump(evalRes.error) + evalRes.error.dispose() + + return left(`Script evaluation failed: ${errorData}`) + } + + vm.dispose() + + return right(finalEnv) + } + ) +) \ No newline at end of file diff --git a/packages/hoppscotch-js-sandbox/src/test-runner.ts b/packages/hoppscotch-js-sandbox/src/test-runner.ts new file mode 100644 index 000000000..ed3df1f5a --- /dev/null +++ b/packages/hoppscotch-js-sandbox/src/test-runner.ts @@ -0,0 +1,367 @@ +import { isLeft } from "fp-ts/lib/Either" +import { pipe } from "fp-ts/lib/function" +import { TaskEither, tryCatch, chain, right, left } from "fp-ts/lib/TaskEither" +import * as qjs from "quickjs-emscripten" +import { marshalObjectToVM } from "./utils" + +/** + * The response object structure exposed to the test script + */ +export type TestResponse = { + /** Status Code of the response */ + status: number, + /** List of headers returned */ + headers: { key: string, value: string }[], + /** + * Body of the response, this will be the JSON object if it is a JSON content type, else body string + */ + body: string | object +} + +/** + * The result of an expectation statement + */ +type ExpectResult = + | { status: "pass" | "fail" | "error", message: string } // The expectation failed (fail) or errored (error) + +/** + * An object defining the result of the execution of a + * test block + */ +export type TestDescriptor = { + /** + * The name of the test block + */ + descriptor: string + + /** + * Expectation results of the test block + */ + expectResults: ExpectResult[] + + /** + * Children test blocks (test blocks inside the test block) + */ + children: TestDescriptor[] +} + +/** + * Creates an Expectation object for use inside the sandbox + * @param vm The QuickJS sandbox VM instance + * @param expectVal The expecting value of the expectation + * @param negated Whether the expectation is negated (negative) + * @param currTestStack The current state of the test execution stack + * @returns Handle to the expectation object in VM + */ +function createExpectation( + vm: qjs.QuickJSVm, + expectVal: any, + negated: boolean, + currTestStack: TestDescriptor[] +): qjs.QuickJSHandle { + const resultHandle = vm.newObject() + + const toBeFnHandle = vm.newFunction("toBe", (expectedValHandle) => { + const expectedVal = vm.dump(expectedValHandle) + + let assertion = expectVal === expectedVal + if (negated) assertion = !assertion + + if (assertion) { + currTestStack[currTestStack.length - 1].expectResults.push({ + status: "pass", + message: `Expected '${expectVal}' to${negated ? " not" : ""} be '${expectedVal}'` + }) + } else { + currTestStack[currTestStack.length - 1].expectResults.push({ + status: "fail", + message: `Expected '${expectVal}' to${negated ? " not" : ""} be '${expectedVal}'`, + }) + } + + return { value: vm.undefined } + }) + + const toBeLevel2xxHandle = vm.newFunction("toBeLevel2xx", () => { + // Check if the expected value is a number, else it is an error + if (typeof expectVal === "number" && !Number.isNaN(expectVal)) { + let assertion = expectVal >= 200 && expectVal <= 299 + if (negated) assertion = !assertion + + if (assertion) { + currTestStack[currTestStack.length - 1].expectResults.push({ + status: "pass", + message: `Expected '${expectVal}' to${negated ? " not" : ""} be 200-level status`, + }) + } else { + currTestStack[currTestStack.length - 1].expectResults.push({ + status: "fail", + message: + `Expected '${expectVal}' to${negated ? " not" : ""} be 200-level status`, + }) + } + } else { + currTestStack[currTestStack.length - 1].expectResults.push({ + status: "error", + message: `Expected 200-level status but could not parse value '${expectVal}'`, + }) + } + + return { value: vm.undefined } + }) + + const toBeLevel3xxHandle = vm.newFunction("toBeLevel3xx", () => { + // Check if the expected value is a number, else it is an error + if (typeof expectVal === "number" && !Number.isNaN(expectVal)) { + let assertion = expectVal >= 300 && expectVal <= 399 + if (negated) assertion = !assertion + + if (assertion) { + currTestStack[currTestStack.length - 1].expectResults.push({ + status: "pass", + message: `Expected '${expectVal}' to${negated ? " not" : ""} be 300-level status`, + }) + } else { + currTestStack[currTestStack.length - 1].expectResults.push({ + status: "fail", + message: + `Expected '${expectVal}' to${negated ? " not" : ""} be 300-level status`, + }) + } + } else { + currTestStack[currTestStack.length - 1].expectResults.push({ + status: "error", + message: `Expected 300-level status but could not parse value '${expectVal}'`, + }) + } + + return { value: vm.undefined } + }) + + const toBeLevel4xxHandle = vm.newFunction("toBeLevel4xx", () => { + // Check if the expected value is a number, else it is an error + if (typeof expectVal === "number" && !Number.isNaN(expectVal)) { + let assertion = expectVal >= 400 && expectVal <= 499 + if (negated) assertion = !assertion + + if (assertion) { + currTestStack[currTestStack.length - 1].expectResults.push({ + status: "pass", + message: `Expected '${expectVal}' to${negated ? " not" : ""} be 400-level status`, + }) + } else { + currTestStack[currTestStack.length - 1].expectResults.push({ + status: "fail", + message: + `Expected '${expectVal}' to${negated ? " not" : ""} be 400-level status`, + }) + } + } else { + currTestStack[currTestStack.length - 1].expectResults.push({ + status: "error", + message: `Expected 400-level status but could not parse value '${expectVal}'`, + }) + } + + return { value: vm.undefined } + }) + + const toBeLevel5xxHandle = vm.newFunction("toBeLevel5xx", () => { + // Check if the expected value is a number, else it is an error + if (typeof expectVal === "number" && !Number.isNaN(expectVal)) { + let assertion = expectVal >= 500 && expectVal <= 599 + if (negated) assertion = !assertion + + if (assertion) { + currTestStack[currTestStack.length - 1].expectResults.push({ + status: "pass", + message: `Expected '${expectVal}' to${negated ? " not" : ""} be 500-level status`, + }) + } else { + currTestStack[currTestStack.length - 1].expectResults.push({ + status: "fail", + message: `Expected '${expectVal}' to${negated ? " not" : ""} be 500-level status` + }) + } + } else { + currTestStack[currTestStack.length - 1].expectResults.push({ + status: "error", + message: `Expected 500-level status but could not parse value '${expectVal}'`, + }) + } + + return { value: vm.undefined } + }) + + const toBeTypeHandle = vm.newFunction("toBeType", (expectedValHandle) => { + const expectedType = vm.dump(expectedValHandle) + + // Check if the expectation param is a valid type name string, else error + if (["string", "boolean", "number", "object", "undefined", "bigint", "symbol", "function"].includes(expectedType)) { + let assertion = typeof expectVal === expectedType + if (negated) assertion = !assertion + + if (assertion) { + currTestStack[currTestStack.length - 1].expectResults.push({ + status: "pass", + message: `Expected '${expectVal}' to${negated ? " not" : ""} be type '${expectedType}'` + }) + } else { + currTestStack[currTestStack.length - 1].expectResults.push({ + status: "fail", + message: `Expected '${expectVal}' to${negated ? " not" : ""} be type '${expectedType}'`, + }) + } + } else { + currTestStack[currTestStack.length - 1].expectResults.push({ + status: "error", + message: `Argument for toBeType should be "string", "boolean", "number", "object", "undefined", "bigint", "symbol" or "function"` + }) + } + + return { value: vm.undefined } + }) + + const toHaveLengthHandle = vm.newFunction( + "toHaveLength", + (expectedValHandle) => { + const expectedLength = vm.dump(expectedValHandle) + + if (!(Array.isArray(expectVal) || typeof expectVal === "string")) { + currTestStack[currTestStack.length - 1].expectResults.push({ + status: "error", + message: `Expected toHaveLength to be called for an array or string`, + }) + + return { value: vm.undefined } + } + + // Check if the parameter is a number, else error + if (typeof expectedLength === "number" && !Number.isNaN(expectedLength)) { + let assertion = (expectVal as any[]).length === expectedLength + if (negated) assertion = !assertion + + if (assertion) { + currTestStack[currTestStack.length - 1].expectResults.push({ + status: "pass", + message: `Expected the array to${negated ? " not" : ""} be of length '${expectedLength}'`, + }) + } else { + currTestStack[currTestStack.length - 1].expectResults.push({ + status: "fail", + message: `Expected the array to${negated ? " not" : ""} be of length '${expectedLength}'` + }) + } + } else { + currTestStack[currTestStack.length - 1].expectResults.push({ + status: "error", + message: `Argument for toHaveLength should be a number` + }) + } + + return { value: vm.undefined } + } + ) + + vm.setProp(resultHandle, "toBe", toBeFnHandle) + vm.setProp(resultHandle, "toBeLevel2xx", toBeLevel2xxHandle) + vm.setProp(resultHandle, "toBeLevel3xx", toBeLevel3xxHandle) + vm.setProp(resultHandle, "toBeLevel4xx", toBeLevel4xxHandle) + vm.setProp(resultHandle, "toBeLevel5xx", toBeLevel5xxHandle) + vm.setProp(resultHandle, "toBeType", toBeTypeHandle) + vm.setProp(resultHandle, "toHaveLength", toHaveLengthHandle) + + vm.defineProp(resultHandle, "not", { + get: () => { + return createExpectation(vm, expectVal, !negated, currTestStack) + }, + }) + + toBeFnHandle.dispose() + toBeLevel2xxHandle.dispose() + toBeLevel3xxHandle.dispose() + toBeLevel4xxHandle.dispose() + toBeLevel5xxHandle.dispose() + toBeTypeHandle.dispose() + toHaveLengthHandle.dispose() + + return resultHandle +} + +export const execTestScript = ( + testScript: string, + response: TestResponse +): TaskEither => pipe( + tryCatch( + async () => await qjs.getQuickJS(), + (reason) => `QuickJS initialization failed: ${reason}` + ), + chain( + // TODO: Make this more functional ? + (QuickJS) => { + const vm = QuickJS.createVm() + + const pwHandle = vm.newObject() + + const testRunStack: TestDescriptor[] = [ + { descriptor: "root", expectResults: [], children: [] }, + ] + + const testFuncHandle = vm.newFunction( + "test", + (descriptorHandle, testFuncHandle) => { + const descriptor = vm.getString(descriptorHandle) + + testRunStack.push({ + descriptor, + expectResults: [], + children: [], + }) + + const result = vm.unwrapResult(vm.callFunction(testFuncHandle, vm.null)) + result.dispose() + + const child = testRunStack.pop() as TestDescriptor + testRunStack[testRunStack.length - 1].children.push(child) + } + ) + + const expectFnHandle = vm.newFunction("expect", (expectValueHandle) => { + const expectVal = vm.dump(expectValueHandle) + + return { + value: createExpectation(vm, expectVal, false, testRunStack), + } + }) + + // Marshal response object + const responseObjHandle = marshalObjectToVM(vm, response) + if (isLeft(responseObjHandle)) return left(`Response marshalling failed: ${responseObjHandle.left}`) + + vm.setProp(pwHandle, "response", responseObjHandle.right) + responseObjHandle.right.dispose() + + vm.setProp(pwHandle, "expect", expectFnHandle) + expectFnHandle.dispose() + + vm.setProp(pwHandle, "test", testFuncHandle) + testFuncHandle.dispose() + + vm.setProp(vm.global, "pw", pwHandle) + pwHandle.dispose() + + const evalRes = vm.evalCode(testScript) + + if (evalRes.error) { + const errorData = vm.dump(evalRes.error) + evalRes.error.dispose() + + return left(`Script evaluation failed: ${errorData}`) + } + + vm.dispose() + + return right(testRunStack) + } + ) +) \ No newline at end of file diff --git a/packages/hoppscotch-js-sandbox/src/utils.ts b/packages/hoppscotch-js-sandbox/src/utils.ts new file mode 100644 index 000000000..854786e45 --- /dev/null +++ b/packages/hoppscotch-js-sandbox/src/utils.ts @@ -0,0 +1,32 @@ +import { Either, left, right } from "fp-ts/lib/Either"; +import * as QuickJS from "quickjs-emscripten"; + +export function marshalObjectToVM(vm: QuickJS.QuickJSVm, obj: object): Either { + let jsonString + + try { + jsonString = JSON.stringify(obj) + } catch (e) { + return left("Marshaling stringification failed") + } + + const vmStringHandle = vm.newString(jsonString) + + const jsonHandle = vm.getProp(vm.global, "JSON") + const parseFuncHandle = vm.getProp(jsonHandle, "parse") + + const parseResultHandle = vm.callFunction(parseFuncHandle, vm.undefined, vmStringHandle) + + if (parseResultHandle.error) { + parseResultHandle.error.dispose() + return left("Marshaling failed") + } + + const resultHandle = vm.unwrapResult(parseResultHandle) + + vmStringHandle.dispose() + parseFuncHandle.dispose() + jsonHandle.dispose() + + return right(resultHandle) +} \ No newline at end of file diff --git a/packages/hoppscotch-js-sandbox/tsconfig.json b/packages/hoppscotch-js-sandbox/tsconfig.json new file mode 100644 index 000000000..a84a776b0 --- /dev/null +++ b/packages/hoppscotch-js-sandbox/tsconfig.json @@ -0,0 +1,26 @@ +{ + "compilerOptions": { + "target": "ES6", + "module": "ESNext", + "moduleResolution": "Node", + "lib": ["ESNext", "ESNext.AsyncIterable", "DOM"], + "esModuleInterop": true, + "strict": true, + "paths": { + "~/*": ["./src/*"], + "@/*": ["./src/*"] + }, + "types": [ + "@types/node", + "@types/jest", + "@relmify/jest-fp-ts" + ], + "outDir": "./lib/", + "rootDir": "./src/", + "declaration": true, + "declarationMap": true, + "sourceMap": true + }, + "include": ["./src", "./src/global.d.ts"], + "exclude": ["node_modules", "./src/__tests__", "./src/demo.ts"] +} \ No newline at end of file From 166f9e817b56657d7b139b39513152a3f90a3296 Mon Sep 17 00:00:00 2001 From: Andrew Bastin Date: Sat, 25 Sep 2021 01:05:31 +0530 Subject: [PATCH 2/7] feat: project level testing + add fp-ts and sandbox as deps --- package.json | 3 +- packages/hoppscotch-app/package.json | 5 +- pnpm-lock.yaml | 239 +++++++++++++++++++++++++++ 3 files changed, 245 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 3517f7c8a..48689bb09 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,8 @@ "generate": "pnpm -r do-build-prod", "start": "pnpm -r do-prod-start", "lintfix": "pnpm -r do-lintfix", - "pre-commit": "pnpm -r do-lintfix" + "pre-commit": "pnpm -r do-lintfix", + "test": "pnpm -r do-test" }, "workspaces": [ "./packages/*" diff --git a/packages/hoppscotch-app/package.json b/packages/hoppscotch-app/package.json index 1edef283f..be1d3c4fa 100644 --- a/packages/hoppscotch-app/package.json +++ b/packages/hoppscotch-app/package.json @@ -22,10 +22,12 @@ "do-dev": "pnpm run dev", "do-build-prod": "pnpm run generate", "do-prod-start": "pnpm run start", - "do-lintfix": "pnpm run lint" + "do-lintfix": "pnpm run lint", + "do-test": "pnpm run test" }, "dependencies": { "@apollo/client": "^3.4.12", + "@hoppscotch/js-sandbox": "workspace:^0.3.1", "@nuxtjs/axios": "^5.13.6", "@nuxtjs/composition-api": "^0.29.0", "@nuxtjs/gtm": "^2.4.0", @@ -41,6 +43,7 @@ "core-js": "^3.17.3", "esprima": "^4.0.1", "firebase": "^9.0.2", + "fp-ts": "^2.11.3", "fuse.js": "^6.4.6", "graphql": "^15.5.3", "graphql-language-service-interface": "^2.8.4", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index dcbfb660b..9a01868e4 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -22,6 +22,7 @@ importers: '@babel/preset-env': ^7.15.6 '@commitlint/cli': ^13.1.0 '@commitlint/config-conventional': ^13.1.0 + '@hoppscotch/js-sandbox': workspace:^0.3.1 '@nuxt/types': ^2.15.8 '@nuxt/typescript-build': ^2.1.0 '@nuxtjs/axios': ^5.13.6 @@ -63,6 +64,7 @@ importers: eslint-plugin-vue: ^7.18.0 esprima: ^4.0.1 firebase: ^9.0.2 + fp-ts: ^2.11.3 fuse.js: ^6.4.6 graphql: ^15.5.3 graphql-language-service-interface: ^2.8.4 @@ -104,6 +106,7 @@ importers: yargs-parser: ^20.2.9 dependencies: '@apollo/client': 3.4.12_graphql@15.5.3 + '@hoppscotch/js-sandbox': link:../hoppscotch-js-sandbox '@nuxtjs/axios': 5.13.6 '@nuxtjs/composition-api': 0.29.0_nuxt@2.15.8 '@nuxtjs/gtm': 2.4.0 @@ -119,6 +122,7 @@ importers: core-js: 3.17.3 esprima: 4.0.1 firebase: 9.0.2 + fp-ts: 2.11.3 fuse.js: 6.4.6 graphql: 15.5.3 graphql-language-service-interface: 2.8.4_graphql@15.5.3 @@ -190,6 +194,49 @@ importers: vue-jest: 3.0.7_babel-core@7.0.0-bridge.0 worker-loader: 3.0.8 + packages/hoppscotch-js-sandbox: + specifiers: + '@digitak/esrun': ^1.2.4 + '@relmify/jest-fp-ts': ^1.1.1 + '@types/jest': ^26.0.23 + '@types/lodash': ^4.14.173 + '@types/node': ^15.12.5 + '@typescript-eslint/eslint-plugin': ^4.28.1 + '@typescript-eslint/parser': ^4.28.1 + eslint: ^7.29.0 + eslint-config-prettier: ^8.3.0 + eslint-plugin-prettier: ^3.4.0 + fp-ts: ^2.11.3 + io-ts: ^2.2.16 + jest: ^27.0.6 + lodash: ^4.17.21 + prettier: ^2.3.2 + pretty-quick: ^3.1.1 + quickjs-emscripten: ^0.13.0 + ts-jest: ^27.0.3 + typescript: ^4.3.5 + dependencies: + fp-ts: 2.11.3 + lodash: 4.17.21 + quickjs-emscripten: 0.13.0 + devDependencies: + '@digitak/esrun': 1.2.7 + '@relmify/jest-fp-ts': 1.1.1_fp-ts@2.11.3+io-ts@2.2.16 + '@types/jest': 26.0.24 + '@types/lodash': 4.14.173 + '@types/node': 15.14.9 + '@typescript-eslint/eslint-plugin': 4.31.1_e2d3c88d378335c4183365c112128ce9 + '@typescript-eslint/parser': 4.31.1_eslint@7.32.0+typescript@4.4.3 + eslint: 7.32.0 + eslint-config-prettier: 8.3.0_eslint@7.32.0 + eslint-plugin-prettier: 3.4.1_6e975bd57c7acf028c1a9ddbbf60c898 + io-ts: 2.2.16_fp-ts@2.11.3 + jest: 27.2.0 + prettier: 2.4.1 + pretty-quick: 3.1.1_prettier@2.4.1 + ts-jest: 27.0.5_bd302350a23170fc0ef61cbda3ff9f29 + typescript: 4.4.3 + packages: /@antfu/utils/0.2.4: @@ -1706,6 +1753,21 @@ packages: engines: {node: '>=4.0.0'} dev: false + /@digitak/esrun/1.2.7: + resolution: {integrity: sha512-G+4t0LvYDS684U/9uOaaOb3Qvqiju2oMK/JIk9liENtDswTxwFVnpIG5vpmszIQkuexcfGGN09VJS1mkrmmPJQ==} + engines: {node: '>=14.0'} + hasBin: true + dependencies: + '@digitak/grubber': 1.0.2 + anymatch: 3.1.2 + chokidar: 3.5.2 + esbuild: 0.12.28 + dev: true + + /@digitak/grubber/1.0.2: + resolution: {integrity: sha512-cufCjWJh9MEsdMZgg4MAyUYtLdwboyiqIYir+/zroxWQ9XFqrYLin/tj1eJuH3UyW2qoy5RWXF4TbteDZ3zd7Q==} + dev: true + /@endemolshinegroup/cosmiconfig-typescript-loader/1.0.2_4f9b016a9697d58bd127ac5ffca32a8d: resolution: {integrity: sha512-ZHkXKq2XFFmAUdmSZrmqUSIrRM4O9gtkdpxMmV+LQl7kScUnbo6pMnXu6+FTDgZ12aW6SDoZoOJfS56WD+Eu6A==} engines: {node: '>=8.0.0', yarn: '>=1.3.0'} @@ -2834,6 +2896,17 @@ packages: - supports-color dev: true + /@jest/types/26.6.2: + resolution: {integrity: sha512-fC6QCp7Sc5sX6g8Tvbmj4XUTbyrik0akgRy03yjXbQaBWWNWGE7SGtJk98m0N8nzegD/7SggrUlivxo5ax4KWQ==} + engines: {node: '>= 10.14.2'} + dependencies: + '@types/istanbul-lib-coverage': 2.0.3 + '@types/istanbul-reports': 3.0.1 + '@types/node': 16.9.2 + '@types/yargs': 15.0.14 + chalk: 4.1.2 + dev: true + /@jest/types/27.1.1: resolution: {integrity: sha512-yqJPDDseb0mXgKqmNqypCsb85C22K1aY5+LUxh7syIM9n/b0AsaltxNy+o6tt29VcfGDpYEve175bm3uOhcehA==} engines: {node: ^10.13.0 || ^12.13.0 || ^14.15.0 || >=15.0.0} @@ -3735,6 +3808,19 @@ packages: resolution: {integrity: sha1-p3c2C1s5oaLlEG+OhY8v0tBgxXA=} dev: false + /@relmify/jest-fp-ts/1.1.1_fp-ts@2.11.3+io-ts@2.2.16: + resolution: {integrity: sha512-cuYK27gjDoEZIzvUuLRvgmqp/t5BlGWMrsVj+kzGlsgYny9xYCAMMRBd38HcFciG6aVTVKrLKqsYNidvU+4vzA==} + peerDependencies: + fp-ts: 2.x + io-ts: 2.x + dependencies: + expect: 26.6.2 + fp-ts: 2.11.3 + io-ts: 2.2.16_fp-ts@2.11.3 + jest-get-type: 26.3.0 + jest-matcher-utils: 26.6.2 + dev: true + /@samverschueren/stream-to-observable/0.3.1_rxjs@6.6.7: resolution: {integrity: sha512-c/qwwcHyafOQuVQJj0IlBjf5yYgBI7YPJ77k4fOJYesb41jio65eaJODRUmfYKhTOFBrIZ66kgvGPlNbjuoRdQ==} engines: {node: '>=6'} @@ -4079,6 +4165,13 @@ packages: '@types/istanbul-lib-report': 3.0.0 dev: true + /@types/jest/26.0.24: + resolution: {integrity: sha512-E/X5Vib8BWqZNRlDxj9vYXhsDwPYbPINqKF9BsnSoon4RQ0D9moEuLD8txgyypFLH7J4+Lho9Nr/c8H0Fi+17w==} + dependencies: + jest-diff: 26.6.2 + pretty-format: 26.6.2 + dev: true + /@types/jest/27.0.1: resolution: {integrity: sha512-HTLpVXHrY69556ozYkcq47TtQJXpcWAWfkoqz+ZGz2JnmZhzlRjprCIyFnetSy8gpDWwTTGBcRVv1J1I1vBrHw==} dependencies: @@ -4175,6 +4268,10 @@ packages: resolution: {integrity: sha512-hcTWqk7DR/HrN9Xe7AlJwuCaL13Vcd9/g/T54YrJz4Q3ESM5mr33YCzW2bOfzSIc3aZMeGBvbLGvgN6mIJ0I5Q==} dev: false + /@types/node/15.14.9: + resolution: {integrity: sha512-qjd88DrCxupx/kJD5yQgZdcYKZKSIGBVDIBE1/LTGcNm3d2Np/jxojkdePDdfnBHJc5W7vSMpbJ1aB7p/Py69A==} + dev: true + /@types/node/16.9.2: resolution: {integrity: sha512-ZHty/hKoOLZvSz6BtP1g7tc7nUeJhoCf3flLjh8ZEv1vFKBWHXcnMbJMyN/pftSljNyy0kNW/UqI3DccnBnZ8w==} @@ -4372,6 +4469,12 @@ packages: resolution: {integrity: sha512-7tFImggNeNBVMsn0vLrpn1H1uPrUBdnARPTpZoitY37ZrdJREzf7I16tMrlK3hen349gr1NYh8CmZQa7CTG6Aw==} dev: true + /@types/yargs/15.0.14: + resolution: {integrity: sha512-yEJzHoxf6SyQGhBhIYGXQDSCkJjB6HohDShto7m8vaKg9Yp0Yn8+71J9eakh2bnPg6BfsH9PRMhiRTZnd4eXGQ==} + dependencies: + '@types/yargs-parser': 20.2.1 + dev: true + /@types/yargs/16.0.4: resolution: {integrity: sha512-T8Yc9wt/5LbJyCaLiHPReJa0kApcIgJ7Bn735GjItUfh08Z1pJvu8QZqb9s+mMvKV6WUQRV7K2R46YbjMXTTJw==} dependencies: @@ -7762,6 +7865,11 @@ packages: streamsearch: 0.1.2 dev: false + /diff-sequences/26.6.2: + resolution: {integrity: sha512-Mv/TDa3nZ9sbc5soK+OoA74BsS3mL37yixCvUAQkiuA4Wz6YtwP/K47n2rv2ovzHZvoiQeA5FTQOschKkEwB0Q==} + engines: {node: '>= 10.14.2'} + dev: true + /diff-sequences/27.0.6: resolution: {integrity: sha512-ag6wfpBFyNXZ0p8pcuIDS//D8H062ZQJ3fzYxjpmeKjnz8W4pekL3AI8VohmyZmsWW2PWaHgjsmqR6L13101VQ==} engines: {node: ^10.13.0 || ^12.13.0 || ^14.15.0 || >=15.0.0} @@ -8264,6 +8372,23 @@ packages: - supports-color dev: true + /eslint-plugin-prettier/3.4.1_6e975bd57c7acf028c1a9ddbbf60c898: + resolution: {integrity: sha512-htg25EUYUeIhKHXjOinK4BgCcDwtLHjqaxCDsMy5nbnUMkKFvIhMVCp+5GFUXQ4Nr8lBsPqtGAqBenbpFqAA2g==} + engines: {node: '>=6.0.0'} + peerDependencies: + eslint: '>=5.0.0' + eslint-config-prettier: '*' + prettier: '>=1.13.0' + peerDependenciesMeta: + eslint-config-prettier: + optional: true + dependencies: + eslint: 7.32.0 + eslint-config-prettier: 8.3.0_eslint@7.32.0 + prettier: 2.4.1 + prettier-linter-helpers: 1.0.0 + dev: true + /eslint-plugin-prettier/4.0.0_6e975bd57c7acf028c1a9ddbbf60c898: resolution: {integrity: sha512-98MqmCJ7vJodoQK359bqQWaxOE0CS8paAz/GgjaZLyex4TTk3g9HugoO89EqWCrFiOqn9EVvcoo7gZzONCWVwQ==} engines: {node: '>=6.0.0'} @@ -8600,6 +8725,18 @@ packages: snapdragon: 0.8.2 to-regex: 3.0.2 + /expect/26.6.2: + resolution: {integrity: sha512-9/hlOBkQl2l/PLHJx6JjoDF6xPKcJEsUlWKb23rKE7KzeDqUZKXKNMW27KIue5JMdBV9HgmoJPcc8HtO85t9IA==} + engines: {node: '>= 10.14.2'} + dependencies: + '@jest/types': 26.6.2 + ansi-styles: 4.3.0 + jest-get-type: 26.3.0 + jest-matcher-utils: 26.6.2 + jest-message-util: 26.6.2 + jest-regex-util: 26.0.0 + dev: true + /expect/27.2.0: resolution: {integrity: sha512-oOTbawMQv7AK1FZURbPTgGSzmhxkjFzoARSvDjOMnOpeWuYQx1tP6rXu9MIX5mrACmyCAM7fSNP8IJO2f1p0CQ==} engines: {node: ^10.13.0 || ^12.13.0 || ^14.15.0 || >=15.0.0} @@ -9064,6 +9201,10 @@ packages: engines: {node: '>= 0.6'} dev: false + /fp-ts/2.11.3: + resolution: {integrity: sha512-qHI5iaVSFNFmdl6yDensWfFMk32iafAINCnqx8m486DV1+Jht/bTnA9CyahL+Xm7h2y3erinviVBIAWvv5bPYw==} + dev: false + /fragment-cache/0.2.1: resolution: {integrity: sha1-QpD60n8T6Jvn8zeZxrxaCr//DRk=} engines: {node: '>=0.10.0'} @@ -10291,6 +10432,14 @@ packages: dependencies: loose-envify: 1.4.0 + /io-ts/2.2.16_fp-ts@2.11.3: + resolution: {integrity: sha512-y5TTSa6VP6le0hhmIyN0dqEXkrZeJLeC5KApJq6VLci3UEKF80lZ+KuoUs02RhBxNWlrqSNxzfI7otLX1Euv8Q==} + peerDependencies: + fp-ts: ^2.5.0 + dependencies: + fp-ts: 2.11.3 + dev: true + /ip/1.1.5: resolution: {integrity: sha1-vd7XARQpCCjAoDnnLvJfWq7ENUo=} dev: false @@ -10879,6 +11028,16 @@ packages: - utf-8-validate dev: true + /jest-diff/26.6.2: + resolution: {integrity: sha512-6m+9Z3Gv9wN0WFVasqjCL/06+EFCMTqDEUl/b87HYK2rAPTyfz4ZIuSlPhY51PIQRWx5TaxeF1qmXKe9gfN3sA==} + engines: {node: '>= 10.14.2'} + dependencies: + chalk: 4.1.2 + diff-sequences: 26.6.2 + jest-get-type: 26.3.0 + pretty-format: 26.6.2 + dev: true + /jest-diff/27.2.0: resolution: {integrity: sha512-QSO9WC6btFYWtRJ3Hac0sRrkspf7B01mGrrQEiCW6TobtViJ9RWL0EmOs/WnBsZDsI/Y2IoSHZA2x6offu0sYw==} engines: {node: ^10.13.0 || ^12.13.0 || ^14.15.0 || >=15.0.0} @@ -10937,6 +11096,11 @@ packages: jest-util: 27.2.0 dev: true + /jest-get-type/26.3.0: + resolution: {integrity: sha512-TpfaviN1R2pQWkIihlfEanwOXK0zcxrKEE4MlU6Tn7keoXdN6/3gK/xl0yEh8DOunn5pOVGKf8hB4R9gVh04ig==} + engines: {node: '>= 10.14.2'} + dev: true + /jest-get-type/27.0.6: resolution: {integrity: sha512-XTkK5exIeUbbveehcSR8w0bhH+c0yloW/Wpl+9vZrjzztCPWrxhHwkIFpZzCt71oRBsgxmuUfxEqOYoZI2macg==} engines: {node: ^10.13.0 || ^12.13.0 || ^14.15.0 || >=15.0.0} @@ -10996,6 +11160,16 @@ packages: pretty-format: 27.2.0 dev: true + /jest-matcher-utils/26.6.2: + resolution: {integrity: sha512-llnc8vQgYcNqDrqRDXWwMr9i7rS5XFiCwvh6DTP7Jqa2mqpcCBBlpCbn+trkG0KNhPu/h8rzyBkriOtBstvWhw==} + engines: {node: '>= 10.14.2'} + dependencies: + chalk: 4.1.2 + jest-diff: 26.6.2 + jest-get-type: 26.3.0 + pretty-format: 26.6.2 + dev: true + /jest-matcher-utils/27.2.0: resolution: {integrity: sha512-F+LG3iTwJ0gPjxBX6HCyrARFXq6jjiqhwBQeskkJQgSLeF1j6ui1RTV08SR7O51XTUhtc8zqpDj8iCG4RGmdKw==} engines: {node: ^10.13.0 || ^12.13.0 || ^14.15.0 || >=15.0.0} @@ -11006,6 +11180,21 @@ packages: pretty-format: 27.2.0 dev: true + /jest-message-util/26.6.2: + resolution: {integrity: sha512-rGiLePzQ3AzwUshu2+Rn+UMFk0pHN58sOG+IaJbk5Jxuqo3NYO1U2/MIR4S1sKgsoYSXSzdtSa0TgrmtUwEbmA==} + engines: {node: '>= 10.14.2'} + dependencies: + '@babel/code-frame': 7.14.5 + '@jest/types': 26.6.2 + '@types/stack-utils': 2.0.1 + chalk: 4.1.2 + graceful-fs: 4.2.8 + micromatch: 4.0.4 + pretty-format: 26.6.2 + slash: 3.0.0 + stack-utils: 2.0.5 + dev: true + /jest-message-util/27.2.0: resolution: {integrity: sha512-y+sfT/94CiP8rKXgwCOzO1mUazIEdEhrLjuiu+RKmCP+8O/TJTSne9dqQRbFIHBtlR2+q7cddJlWGir8UATu5w==} engines: {node: ^10.13.0 || ^12.13.0 || ^14.15.0 || >=15.0.0} @@ -11041,6 +11230,11 @@ packages: jest-resolve: 27.2.0 dev: true + /jest-regex-util/26.0.0: + resolution: {integrity: sha512-Gv3ZIs/nA48/Zvjrl34bf+oD76JHiGDUxNOVgUjh3j890sblXryjY4rss71fPtD/njchl6PSE2hIhvyWa1eT0A==} + engines: {node: '>= 10.14.2'} + dev: true + /jest-regex-util/27.0.6: resolution: {integrity: sha512-SUhPzBsGa1IKm8hx2F4NfTGGp+r7BXJ4CulsZ1k2kI+mGLG+lxGrs76veN2LF/aUdGosJBzKgXmNCw+BzFqBDQ==} engines: {node: ^10.13.0 || ^12.13.0 || ^14.15.0 || >=15.0.0} @@ -14071,6 +14265,16 @@ packages: renderkid: 2.0.7 dev: false + /pretty-format/26.6.2: + resolution: {integrity: sha512-7AeGuCYNGmycyQbCqd/3PWH4eOoX/OiCa0uphp57NVTeAGdJGaAliecxwBDHYQCIvrW7aDBZCYeNTP/WX69mkg==} + engines: {node: '>= 10'} + dependencies: + '@jest/types': 26.6.2 + ansi-regex: 5.0.1 + ansi-styles: 4.3.0 + react-is: 17.0.2 + dev: true + /pretty-format/27.2.0: resolution: {integrity: sha512-KyJdmgBkMscLqo8A7K77omgLx5PWPiXJswtTtFV7XgVZv2+qPk6UivpXXO+5k6ZEbWIbLoKdx1pZ6ldINzbwTA==} engines: {node: ^10.13.0 || ^12.13.0 || ^14.15.0 || >=15.0.0} @@ -14429,6 +14633,10 @@ packages: engines: {node: '>=10'} dev: true + /quickjs-emscripten/0.13.0: + resolution: {integrity: sha512-+8oz1u1Xs1I/FlQuH7p0xb9/GTPNvcLDTbfSmIgUoe4wtFrDtgxlHekmEaToXZyFJ3/rJYzjuG4djjfXTn+3lA==} + dev: false + /randombytes/2.1.0: resolution: {integrity: sha512-vYl3iOX+4CKUWuxGi9Ukhie6fsqXqS9FE2Zaic4tNFD2N2QQaXOMFbuKK4QmDHC0JO6B1Zp41J0LpT0oR68amQ==} dependencies: @@ -16455,6 +16663,37 @@ packages: yargs-parser: 20.2.9 dev: true + /ts-jest/27.0.5_bd302350a23170fc0ef61cbda3ff9f29: + resolution: {integrity: sha512-lIJApzfTaSSbtlksfFNHkWOzLJuuSm4faFAfo5kvzOiRAuoN4/eKxVJ2zEAho8aecE04qX6K1pAzfH5QHL1/8w==} + engines: {node: ^10.13.0 || ^12.13.0 || ^14.15.0 || >=15.0.0} + hasBin: true + peerDependencies: + '@babel/core': '>=7.0.0-beta.0 <8' + '@types/jest': ^27.0.0 + babel-jest: '>=27.0.0 <28' + jest: ^27.0.0 + typescript: '>=3.8 <5.0' + peerDependenciesMeta: + '@babel/core': + optional: true + '@types/jest': + optional: true + babel-jest: + optional: true + dependencies: + '@types/jest': 26.0.24 + bs-logger: 0.2.6 + fast-json-stable-stringify: 2.1.0 + jest: 27.2.0 + jest-util: 27.2.0 + json5: 2.2.0 + lodash: 4.17.21 + make-error: 1.3.6 + semver: 7.3.5 + typescript: 4.4.3 + yargs-parser: 20.2.9 + dev: true + /ts-loader/8.3.0_typescript@4.2.4: resolution: {integrity: sha512-MgGly4I6cStsJy27ViE32UoqxPTN9Xly4anxxVyaIWR+9BGxboV4EyJBGfR3RePV7Ksjj3rHmPZJeIt+7o4Vag==} engines: {node: '>=10.0.0'} From 9454d8c100bbd1fd98927dd18afa744bacf3f8d9 Mon Sep 17 00:00:00 2001 From: Andrew Bastin Date: Sat, 25 Sep 2021 01:09:48 +0530 Subject: [PATCH 3/7] feat: move testing code to js-sandbox --- .../components/http/Request.vue | 38 ++- .../hoppscotch-app/helpers/RequestRunner.ts | 187 +++------- .../helpers/postwomanTesting.ts | 321 ------------------ packages/hoppscotch-app/helpers/preRequest.ts | 51 ++- 4 files changed, 98 insertions(+), 499 deletions(-) delete mode 100644 packages/hoppscotch-app/helpers/postwomanTesting.ts diff --git a/packages/hoppscotch-app/components/http/Request.vue b/packages/hoppscotch-app/components/http/Request.vue index f4b3b14cc..31c9c6f01 100644 --- a/packages/hoppscotch-app/components/http/Request.vue +++ b/packages/hoppscotch-app/components/http/Request.vue @@ -232,6 +232,7 @@