Skip to content

Commit 1c0398b

Browse files
committed
chore: improve the JS test suite
1 parent 12ca738 commit 1c0398b

File tree

2 files changed

+45
-13
lines changed

2 files changed

+45
-13
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,6 @@ codegen/*/go.sum
2929
.github/styles/Microsoft
3030
.github/styles/write-good
3131
sources/academy/**/exercises/storage
32+
sources/academy/**/exercises/node_modules
33+
sources/academy/**/exercises/package*.json
3234
sources/academy/**/exercises/dataset.json
Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,98 +1,116 @@
1-
setup() {
1+
setup_file() {
22
cd "$BATS_TEST_DIRNAME"
33
export npm_config_yes=true
44
}
55

6-
retry_run() {
7-
for attempt in 1 2 3; do
8-
run "$@"
9-
(( status == 0 )) && return 0
10-
sleep 1
11-
done
12-
return "$status"
6+
teardown_file() {
7+
rm -rf storage node_modules package.json package-lock.json dataset.json
138
}
149

10+
# retry_run() {
11+
# for attempt in 1 2 3; do
12+
# run "$@"
13+
# (( status == 0 )) && return 0
14+
# sleep 1
15+
# done
16+
# return "$status"
17+
# }
18+
1519
@test "outputs the HTML with Star Wars products" {
1620
run npx node lego.mjs
21+
1722
[[ "$output" == *"Millennium Falcon"* ]]
1823
}
1924

2025
@test "counts the number of F1 Academy teams" {
2126
run npx --package=cheerio node f1academy_teams.mjs
27+
2228
[[ "$output" == "6" ]]
2329
}
2430

2531
@test "counts the number of F1 Academy drivers" {
2632
run npx --package=cheerio node f1academy_drivers.mjs
33+
2734
[[ "$output" == "18" ]]
2835
}
2936

3037
@test "lists African countries" {
3138
run npx --package=cheerio node wikipedia_countries.mjs
39+
3240
[[ "$output" == *$'Comoros\nDemocratic Republic of the Congo\n'* ]]
3341
[[ $(echo "$output" | wc -l) -gt 5 ]]
3442
}
3543

3644
@test "lists African countries with a single selector" {
3745
run npx --package=cheerio node wikipedia_countries_single_selector.mjs
46+
3847
[[ "$output" == *$'Comoros\nDemocratic Republic of the Congo\n'* ]]
3948
[[ $(echo "$output" | wc -l) -gt 5 ]]
4049
}
4150

4251
@test "lists Guardian F1 article titles" {
4352
run npx --package=cheerio node guardian_f1_titles.mjs
53+
4454
[[ "$output" == *' F1 '* ]]
4555
[[ $(echo "$output" | wc -l) -gt 5 ]]
4656
}
4757

4858
@test "prints warehouse stock counts" {
4959
run npx --package=cheerio node warehouse_units.mjs
60+
5061
[[ "$output" == *$'JBL Flip 4 Waterproof Portable Bluetooth Speaker | 672\n'* ]]
5162
[[ "$output" == *$'Sony XBR-950G BRAVIA 4K HDR Ultra HD TV | 77\n'* ]]
5263
[[ $(echo "$output" | wc -l) -gt 5 ]]
5364
}
5465

5566
@test "prints warehouse stock counts using regex" {
5667
run npx --package=cheerio node warehouse_units_regex.mjs
68+
5769
[[ "$output" == *$'JBL Flip 4 Waterproof Portable Bluetooth Speaker | 672\n'* ]]
5870
[[ "$output" == *$'Sony XBR-950G BRAVIA 4K HDR Ultra HD TV | 77\n'* ]]
5971
[[ $(echo "$output" | wc -l) -gt 5 ]]
6072
}
6173

6274
@test "prints Guardian F1 titles with publish dates" {
6375
run npx --package=cheerio node guardian_publish_dates.mjs
76+
6477
[[ "$output" == *' F1 '* ]]
65-
[[ "$output" == *' | Sun '* ]] # has info about date (articles published on Sunday are very likely)
78+
[[ "$output" == *' | Sun '* ]] # has info about date, Sundays are very likely
6679
[[ $(echo "$output" | wc -l) -gt 5 ]]
6780
}
6881

6982
@test "filters products from JSON" {
7083
run npx node process_products_json.mjs
84+
7185
[[ "$output" == "{ title: 'Premium Speakers', minPrice: 75000, price: 75000 }" ]]
7286
}
7387

7488
@test "lists Wikipedia country links" {
7589
run npx --package=cheerio node wikipedia_country_links.mjs
90+
7691
[[ "$output" == *$'https://en.wikipedia.org/wiki/Algeria\nhttps://en.wikipedia.org/wiki/Angola\n'* ]]
7792
[[ "$output" == *$'https://en.wikipedia.org/wiki/R%C3%A9union\n'* ]]
7893
[[ $(echo "$output" | wc -l) -gt 5 ]]
7994
}
8095

8196
@test "lists Guardian F1 article links" {
8297
run npx --package=cheerio node guardian_f1_links.mjs
98+
8399
[[ "$output" == *'https://www.theguardian.com/sport/'* ]]
84100
[[ $(echo "$output" | wc -l) -gt 5 ]]
85101
}
86102

87103
@test "prints Wikipedia calling codes" {
88104
run npx --package=cheerio node wikipedia_calling_codes.mjs
105+
89106
[[ "$output" == *$'https://en.wikipedia.org/wiki/Comoros +269\n'* ]]
90107
[[ "$output" == *$'https://en.wikipedia.org/wiki/Sahrawi_Arab_Democratic_Republic null\n'* ]]
91108
[[ $(echo "$output" | wc -l) -gt 5 ]]
92109
}
93110

94111
@test "lists Guardian F1 authors" {
95112
run npx --package=cheerio node guardian_f1_authors.mjs
113+
96114
[[ "$output" == *' F1 '* ]]
97115
[[ "$output" == *'Giles Richards: '* ]] # writes most of them (we'll have to change this if they fire'him)
98116
[[ "$output" == *'Guardian sport: '* || "$output" == *'PM Media: '* ]]
@@ -101,25 +119,37 @@ retry_run() {
101119

102120
@test "lists npm LLM packages" {
103121
run npx --package=cheerio node npm_llm_packages.mjs
122+
104123
(( status == 0 ))
105124
[[ -n "$output" ]]
106125
}
107126

108127
@test "finds the shortest CNN sports article" {
109128
run npx --package=cheerio node cnn_sports_shortest_article.mjs
129+
110130
[[ "$output" == 'https://edition.cnn.com/'* ]]
111131
}
112132

113133
@test "scrapes F1 Academy driver details with Crawlee" {
114-
run npx --package=crawlee node crawlee_f1_drivers.mjs
134+
npm init --yes
135+
npm install crawlee
136+
run node crawlee_f1_drivers.mjs
137+
115138
(( status == 0 ))
116139
[[ -n "$output" || -f dataset.json ]]
117-
rm -f dataset.json
140+
[[ $(cat dataset.json | jq '. | length') == "18" ]]
141+
[[ $(cat dataset.json | jq -c '.[0] | keys') == '["dob","instagram_url","name","nationality","team","url"]' ]]
142+
[[ $(cat dataset.json | jq '.[].url') == *"https://www.f1academy.com/Racing-Series/Drivers/"* ]]
118143
}
119144

120145
@test "scrapes Netflix ratings with Crawlee" {
121-
run npx --package=crawlee node crawlee_netflix_ratings.mjs
146+
npm init --yes
147+
npm install crawlee
148+
run node crawlee_netflix_ratings.mjs
149+
122150
(( status == 0 ))
123151
[[ -n "$output" || -f dataset.json ]]
124-
rm -f dataset.json
152+
[[ $(cat dataset.json | jq '. | length') == "10" ]]
153+
[[ $(cat dataset.json | jq -c '.[0] | keys') == '["url","title","rating"]' ]]
154+
[[ $(cat dataset.json | jq '.[].url') == *"https://www.imdb.com/title/"* ]]
125155
}

0 commit comments

Comments
 (0)