Skip to content

Commit 87b813b

Browse files
committed
chore: fix the JS test suite not to rely on npx --package
1 parent ddb9a1b commit 87b813b

File tree

2 files changed

+27
-44
lines changed

2 files changed

+27
-44
lines changed

sources/academy/webscraping/scraping_basics_javascript/exercises/products.json

Lines changed: 0 additions & 12 deletions
This file was deleted.

sources/academy/webscraping/scraping_basics_javascript/exercises/test.bats

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,115 +1,114 @@
11
setup_file() {
22
cd "$BATS_TEST_DIRNAME"
3-
export npm_config_yes=true
3+
npm init --yes
4+
npm install cheerio crawlee
5+
}
6+
7+
teardown() {
8+
rm -rf products.json storage dataset.json
49
}
510

611
teardown_file() {
7-
rm -rf storage node_modules package.json package-lock.json dataset.json
12+
rm -rf node_modules package.json package-lock.json
813
}
914

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-
# }
1815

1916
@test "outputs the HTML with Star Wars products" {
20-
run npx node lego.mjs
17+
run node lego.mjs
2118

2219
[[ "$output" == *"Millennium Falcon"* ]]
2320
}
2421

2522
@test "counts the number of F1 Academy teams" {
26-
run npx --package=cheerio node f1academy_teams.mjs
23+
run node f1academy_teams.mjs
2724

2825
[[ "$output" == "6" ]]
2926
}
3027

3128
@test "counts the number of F1 Academy drivers" {
32-
run npx --package=cheerio node f1academy_drivers.mjs
29+
run node f1academy_drivers.mjs
3330

3431
[[ "$output" == "18" ]]
3532
}
3633

3734
@test "lists African countries" {
38-
run npx --package=cheerio node wikipedia_countries.mjs
35+
run node wikipedia_countries.mjs
3936

4037
[[ "$output" == *$'Comoros\nDemocratic Republic of the Congo\n'* ]]
4138
[[ $(echo "$output" | wc -l) -gt 5 ]]
4239
}
4340

4441
@test "lists African countries with a single selector" {
45-
run npx --package=cheerio node wikipedia_countries_single_selector.mjs
42+
run node wikipedia_countries_single_selector.mjs
4643

4744
[[ "$output" == *$'Comoros\nDemocratic Republic of the Congo\n'* ]]
4845
[[ $(echo "$output" | wc -l) -gt 5 ]]
4946
}
5047

5148
@test "lists Guardian F1 article titles" {
52-
run npx --package=cheerio node guardian_f1_titles.mjs
49+
run node guardian_f1_titles.mjs
5350

5451
[[ "$output" == *' F1 '* ]]
5552
[[ $(echo "$output" | wc -l) -gt 5 ]]
5653
}
5754

5855
@test "prints warehouse stock counts" {
59-
run npx --package=cheerio node warehouse_units.mjs
56+
run node warehouse_units.mjs
6057

6158
[[ "$output" == *$'JBL Flip 4 Waterproof Portable Bluetooth Speaker | 672\n'* ]]
6259
[[ "$output" == *$'Sony XBR-950G BRAVIA 4K HDR Ultra HD TV | 77\n'* ]]
6360
[[ $(echo "$output" | wc -l) -gt 5 ]]
6461
}
6562

6663
@test "prints warehouse stock counts using regex" {
67-
run npx --package=cheerio node warehouse_units_regex.mjs
64+
run node warehouse_units_regex.mjs
6865

6966
[[ "$output" == *$'JBL Flip 4 Waterproof Portable Bluetooth Speaker | 672\n'* ]]
7067
[[ "$output" == *$'Sony XBR-950G BRAVIA 4K HDR Ultra HD TV | 77\n'* ]]
7168
[[ $(echo "$output" | wc -l) -gt 5 ]]
7269
}
7370

7471
@test "prints Guardian F1 titles with publish dates" {
75-
run npx --package=cheerio node guardian_publish_dates.mjs
72+
run node guardian_publish_dates.mjs
7673

7774
[[ "$output" == *' F1 '* ]]
7875
[[ "$output" == *' | Sun '* ]] # has info about date, Sundays are very likely
7976
[[ $(echo "$output" | wc -l) -gt 5 ]]
8077
}
8178

8279
@test "filters products from JSON" {
83-
run npx node process_products_json.mjs
80+
echo '[{"title":"Premium Speakers","minPrice":75000,"price":75000},{"title":"Budget Headphones","minPrice":25000,"price":25000}]' > products.json
81+
82+
run node process_products_json.mjs
8483

8584
[[ "$output" == "{ title: 'Premium Speakers', minPrice: 75000, price: 75000 }" ]]
8685
}
8786

8887
@test "lists Wikipedia country links" {
89-
run npx --package=cheerio node wikipedia_country_links.mjs
88+
run node wikipedia_country_links.mjs
9089

9190
[[ "$output" == *$'https://en.wikipedia.org/wiki/Algeria\nhttps://en.wikipedia.org/wiki/Angola\n'* ]]
9291
[[ "$output" == *$'https://en.wikipedia.org/wiki/R%C3%A9union\n'* ]]
9392
[[ $(echo "$output" | wc -l) -gt 5 ]]
9493
}
9594

9695
@test "lists Guardian F1 article links" {
97-
run npx --package=cheerio node guardian_f1_links.mjs
96+
run node guardian_f1_links.mjs
9897

9998
[[ "$output" == *'https://www.theguardian.com/sport/'* ]]
10099
[[ $(echo "$output" | wc -l) -gt 5 ]]
101100
}
102101

103102
@test "prints Wikipedia calling codes" {
104-
run npx --package=cheerio node wikipedia_calling_codes.mjs
103+
run node wikipedia_calling_codes.mjs
105104

106105
[[ "$output" == *$'https://en.wikipedia.org/wiki/Comoros +269\n'* ]]
107106
[[ "$output" == *$'https://en.wikipedia.org/wiki/Sahrawi_Arab_Democratic_Republic null\n'* ]]
108107
[[ $(echo "$output" | wc -l) -gt 5 ]]
109108
}
110109

111110
@test "lists Guardian F1 authors" {
112-
run npx --package=cheerio node guardian_f1_authors.mjs
111+
run node guardian_f1_authors.mjs
113112

114113
[[ "$output" == *' F1 '* ]]
115114
[[ "$output" == *'Giles Richards: '* ]] # writes most of them (we'll have to change this if they fire'him)
@@ -118,37 +117,33 @@ teardown_file() {
118117
}
119118

120119
@test "lists npm LLM packages" {
121-
run npx --package=cheerio node npm_llm_packages.mjs
120+
run node npm_llm_packages.mjs
122121

123122
(( status == 0 ))
124123
[[ -n "$output" ]]
125124
}
126125

127126
@test "finds the shortest CNN sports article" {
128-
run npx --package=cheerio node cnn_sports_shortest_article.mjs
127+
run node cnn_sports_shortest_article.mjs
129128

130129
[[ "$output" == 'https://edition.cnn.com/'* ]]
131130
}
132131

133132
@test "scrapes F1 Academy driver details with Crawlee" {
134-
npm init --yes
135-
npm install crawlee
136133
run node crawlee_f1_drivers.mjs
137134

138135
(( status == 0 ))
139-
[[ -n "$output" || -f dataset.json ]]
136+
[[ -f dataset.json ]]
140137
[[ $(cat dataset.json | jq '. | length') == "18" ]]
141138
[[ $(cat dataset.json | jq -c '.[0] | keys') == '["dob","instagram_url","name","nationality","team","url"]' ]]
142139
[[ $(cat dataset.json | jq '.[].url') == *"https://www.f1academy.com/Racing-Series/Drivers/"* ]]
143140
}
144141

145142
@test "scrapes Netflix ratings with Crawlee" {
146-
npm init --yes
147-
npm install crawlee
148143
run node crawlee_netflix_ratings.mjs
149144

150145
(( status == 0 ))
151-
[[ -n "$output" || -f dataset.json ]]
146+
[[ -f dataset.json ]]
152147
[[ $(cat dataset.json | jq '. | length') == "10" ]]
153148
[[ $(cat dataset.json | jq -c '.[0] | keys') == '["url","title","rating"]' ]]
154149
[[ $(cat dataset.json | jq '.[].url') == *"https://www.imdb.com/title/"* ]]

0 commit comments

Comments
 (0)