Skip to content

Commit 75afedc

Browse files
committed
chore(internal): add scripts/test and scripts/mock (#801)
1 parent c6629c3 commit 75afedc

File tree

4 files changed

+65
-1
lines changed

4 files changed

+65
-1
lines changed

jest.config.ts

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const config: JestConfigWithTsJest = {
1717
'<rootDir>/deno/',
1818
'<rootDir>/deno_tests/',
1919
],
20+
testPathIgnorePatterns: ['scripts'],
2021
};
2122

2223
export default config;

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
],
1515
"private": false,
1616
"scripts": {
17-
"test": "bin/check-test-server && yarn jest",
17+
"test": "./scripts/test",
1818
"build": "bash ./build",
1919
"prepack": "echo 'to pack, run yarn build && (cd dist; yarn pack)' && exit 1",
2020
"prepublishOnly": "echo 'to publish, run yarn build && (cd dist; yarn publish)' && exit 1",

scripts/mock

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/bin/env bash
2+
3+
if [ -z "$1" ]; then
4+
URL="$1"
5+
shift
6+
else
7+
URL="$(grep 'openapi_spec_url' .stats.yml | cut -d' ' -f2)"
8+
fi
9+
10+
# Check if the URL is empty
11+
if [ -z "$URL" ]; then
12+
echo "Error: No OpenAPI spec path/url provided or found in .stats.yml"
13+
exit 1
14+
fi
15+
16+
# Run prism mock on the given spec
17+
if [ "$1" == "--daemon" ]; then
18+
npm exec prism mock "$URL" &> .prism.log &
19+
20+
# Wait for server to come online
21+
while ! grep -q "✖ fatal\|Prism is listening" ".prism.log" ; do
22+
echo -n "."
23+
sleep 0.1
24+
done
25+
26+
if grep -q "✖ fatal" ".prism.log"; then
27+
cat .prism.log
28+
exit 1
29+
fi
30+
31+
echo
32+
else
33+
npm exec prism mock "$URL"
34+
fi

scripts/test

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env bash
2+
3+
function prism_is_running() {
4+
curl --silent "http://localhost:4010" >/dev/null 2>&1
5+
}
6+
7+
kill_server_on_port() {
8+
pids=$(lsof -t -i tcp:"$1" || echo "")
9+
if [ "$pids" != "" ]; then
10+
kill "$pids"
11+
echo "Stopped $pids."
12+
fi
13+
}
14+
15+
if ! prism_is_running; then
16+
# When we exit this script, make sure to kill the background mock server process
17+
trap 'kill_server_on_port 4010' EXIT
18+
19+
# Start the dev server
20+
./scripts/mock --daemon
21+
22+
# Sanity check and print a nice error message
23+
if ! ./bin/check-test-server; then
24+
exit
25+
fi
26+
fi
27+
28+
# Run tests
29+
./node_modules/.bin/jest

0 commit comments

Comments
 (0)