From 298104ce24e2d159704eda6bdcda549c46f3dd1a Mon Sep 17 00:00:00 2001 From: Stainless Bot Date: Fri, 26 Apr 2024 20:29:58 +0000 Subject: [PATCH] chore(internal): add scripts/test and scripts/mock --- jest.config.ts | 1 + package.json | 2 +- scripts/mock | 34 ++++++++++++++++++++++++++++++++++ scripts/test | 29 +++++++++++++++++++++++++++++ 4 files changed, 65 insertions(+), 1 deletion(-) create mode 100755 scripts/mock create mode 100755 scripts/test diff --git a/jest.config.ts b/jest.config.ts index 445a87301..56d824cdc 100644 --- a/jest.config.ts +++ b/jest.config.ts @@ -17,6 +17,7 @@ const config: JestConfigWithTsJest = { '/deno/', '/deno_tests/', ], + testPathIgnorePatterns: ['scripts'], }; export default config; diff --git a/package.json b/package.json index c67a2fb77..f94e0590b 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,7 @@ ], "private": false, "scripts": { - "test": "bin/check-test-server && yarn jest", + "test": "./scripts/test", "build": "bash ./build", "prepack": "echo 'to pack, run yarn build && (cd dist; yarn pack)' && exit 1", "prepublishOnly": "echo 'to publish, run yarn build && (cd dist; yarn publish)' && exit 1", diff --git a/scripts/mock b/scripts/mock new file mode 100755 index 000000000..61c6988a3 --- /dev/null +++ b/scripts/mock @@ -0,0 +1,34 @@ +#!/usr/bin/env bash + +if [ -z "$1" ]; then + URL="$1" + shift +else + URL="$(grep 'openapi_spec_url' .stats.yml | cut -d' ' -f2)" +fi + +# Check if the URL is empty +if [ -z "$URL" ]; then + echo "Error: No OpenAPI spec path/url provided or found in .stats.yml" + exit 1 +fi + +# Run prism mock on the given spec +if [ "$1" == "--daemon" ]; then + npm exec prism mock "$URL" &> .prism.log & + + # Wait for server to come online + while ! grep -q "✖ fatal\|Prism is listening" ".prism.log" ; do + echo -n "." + sleep 0.1 + done + + if grep -q "✖ fatal" ".prism.log"; then + cat .prism.log + exit 1 + fi + + echo +else + npm exec prism mock "$URL" +fi diff --git a/scripts/test b/scripts/test new file mode 100755 index 000000000..f01384e68 --- /dev/null +++ b/scripts/test @@ -0,0 +1,29 @@ +#!/usr/bin/env bash + +function prism_is_running() { + curl --silent "http://localhost:4010" >/dev/null 2>&1 +} + +kill_server_on_port() { + pids=$(lsof -t -i tcp:"$1" || echo "") + if [ "$pids" != "" ]; then + kill "$pids" + echo "Stopped $pids." + fi +} + +if ! prism_is_running; then + # When we exit this script, make sure to kill the background mock server process + trap 'kill_server_on_port 4010' EXIT + + # Start the dev server + ./scripts/mock --daemon + + # Sanity check and print a nice error message + if ! ./bin/check-test-server; then + exit + fi +fi + +# Run tests +./node_modules/.bin/jest