Skip to content

Commit

Permalink
chore(internal): add scripts/test and scripts/mock (#801)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-bot committed Apr 26, 2024
1 parent c6629c3 commit 6656105
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 1 deletion.
1 change: 1 addition & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const config: JestConfigWithTsJest = {
'<rootDir>/deno/',
'<rootDir>/deno_tests/',
],
testPathIgnorePatterns: ['scripts'],
};

export default config;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
34 changes: 34 additions & 0 deletions scripts/mock
Original file line number Diff line number Diff line change
@@ -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
29 changes: 29 additions & 0 deletions scripts/test
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 6656105

Please sign in to comment.