Skip to content

Commit 6ad7d5e

Browse files
committed
Smoke tests
1 parent 4b94bb7 commit 6ad7d5e

8 files changed

Lines changed: 323 additions & 4 deletions

File tree

.github/workflows/build.yml

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ on: push
44

55
jobs:
66
build:
7+
name: "Deno tests and npm build"
78
runs-on: ubuntu-22.04
89
steps:
910
- name: Checkout repo
@@ -28,8 +29,43 @@ jobs:
2829
- name: Setup Node
2930
uses: actions/setup-node@v3
3031
with:
31-
node-version: '18.x'
32+
node-version: '18.x' # Build files using a fixed node version
3233
registry-url: 'https://registry.npmjs.org'
3334

3435
- name: Test building of npm files
3536
run: deno task npm
37+
38+
- name: Upload build files for smoke tests
39+
uses: actions/upload-artifact@v3
40+
with:
41+
name: npm
42+
path: npm/
43+
44+
smoke-tests:
45+
name: "Smoke tests for different Node.js versions"
46+
needs: build
47+
runs-on: ubuntu-22.04
48+
strategy:
49+
matrix:
50+
node-version: [18.x]
51+
steps:
52+
- name: Checkout repo
53+
uses: actions/checkout@v3
54+
55+
- name: Setup Node
56+
uses: actions/setup-node@v3
57+
with:
58+
node-version: ${{ matrix.node-version }}
59+
registry-url: 'https://registry.npmjs.org'
60+
61+
- name: Download build files
62+
uses: actions/download-artifact@v3
63+
with:
64+
name: npm
65+
66+
- name: Run smoke tests
67+
run: |
68+
echo ${{ matrix.node-version }}
69+
cd smoke
70+
npm i
71+
npm run esm

CONTRIBUTING.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ If you use VSCode, use the following settings (`.vscode/settings.json`):
2626
"mod.ts",
2727
"version.ts",
2828
"src",
29-
"tests",
29+
"tests/*.ts",
30+
"tests/engines/",
3031
"scripts",
3132
"examples/deno"
3233
],

deno.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,18 @@
99
},
1010
"fmt": {
1111
"files": {
12-
"exclude": ["npm/", "examples/node"]
12+
"exclude": ["npm/", "examples/node", "tests/smoke/"]
1313
}
1414
},
1515
"lint": {
1616
"files": {
17-
"exclude": ["npm/", "examples/node"]
17+
"exclude": ["npm/", "examples/node", "tests/smoke/"]
18+
}
19+
},
20+
"test": {
21+
"files": {
22+
"include": ["tests/"],
23+
"exclude": ["tests/smoke/"]
1824
}
1925
},
2026
"compilerOptions": {

tests/smoke/.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
API_KEY=YOUR_API_KEY

tests/smoke/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Smoke tests run on different node versions which generate different
2+
# package-lock.json files.
3+
package-lock.json

tests/smoke/cjs.js

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/**
2+
* Smoke test works for Node.js 7 and newer.
3+
*/
4+
5+
const Dotenv = require("dotenv");
6+
const { config, getJson, getHtml, getJsonBySearchId, getHtmlBySearchId, getAccount, getLocations } = require("serpapi");
7+
8+
Dotenv.config();
9+
const apiKey = process.env.API_KEY;
10+
11+
const run = async () => {
12+
console.log("running", process.versions.node);
13+
14+
const params = {
15+
q: "Coffee",
16+
api_key: apiKey,
17+
};
18+
19+
let searchId;
20+
21+
// getJson async await
22+
{
23+
const page1 = await getJson("google", params);
24+
searchId = page1["search_metadata"]["id"];
25+
if (!page1["organic_results"]) throw new Error("No organic results");
26+
if (page1.next) {
27+
const page2 = await page1.next();
28+
if (!page2["organic_results"]) throw new Error("No organic results");
29+
}
30+
}
31+
32+
// getJson callback
33+
{
34+
getJson("google", params, (page1) => {
35+
if (!page1["organic_results"]) throw new Error("No organic results");
36+
if (page1.next) {
37+
page1.next((page2) => {
38+
if (!page2["organic_results"]) throw new Error("No organic results");
39+
});
40+
}
41+
});
42+
}
43+
44+
// getJson using global config
45+
{
46+
config.api_key = apiKey;
47+
const page1 = await getJson("google", { q: "Coffee" });
48+
if (!page1["organic_results"]) throw new Error("No organic results");
49+
if (page1.next) {
50+
const page2 = await page1.next();
51+
if (!page2["organic_results"]) throw new Error("No organic results");
52+
}
53+
}
54+
55+
// getJson pagination loop (async/await)
56+
{
57+
const links = [];
58+
let page = await getJson("google", params);
59+
while (page) {
60+
links.push(...page.organic_results.map(r => r.link));
61+
if (links.length >= 30) break;
62+
page = page.next ? await page.next(): undefined;
63+
}
64+
if (links.length < 30) throw new Error("Incorrect number of links");
65+
}
66+
67+
// getJson pagination loop (callback)
68+
{
69+
const links = [];
70+
getJson("google", params, (page) => {
71+
links.push(...page.organic_results.map(r => r.link));
72+
if (links.length < 30 && page.next) {
73+
page.next();
74+
} else {
75+
if (links.length < 30) throw new Error("Incorrect number of links");
76+
}
77+
});
78+
}
79+
80+
// getHtml
81+
{
82+
const html = await getHtml("google", params);
83+
if (html.length < 1000) throw new Error("Incorrect HTML");
84+
85+
getHtml("google", params, html => {
86+
if (html.length < 1000) throw new Error("Incorrect HTML");
87+
});
88+
}
89+
90+
// getJsonBySearchId
91+
{
92+
config.api_key = apiKey;
93+
const json = await getJsonBySearchId(searchId);
94+
if (!json["organic_results"]) throw new Error("No organic results");
95+
96+
getJsonBySearchId(searchId, {}, (json) => {
97+
if (!json["organic_results"]) throw new Error("No organic results");
98+
});
99+
}
100+
101+
// getHtmlBySearchId
102+
{
103+
config.api_key = apiKey;
104+
const html = await getHtmlBySearchId(searchId);
105+
if (html.length < 1000) throw new Error("Incorrect HTML");
106+
107+
getHtmlBySearchId(searchId, {}, (html) => {
108+
if (html.length < 1000) throw new Error("Incorrect HTML");
109+
});
110+
}
111+
112+
// getAccount
113+
{
114+
config.api_key = apiKey;
115+
const info = await getAccount();
116+
if (!info["account_email"]) throw new Error("Incorrect account info");
117+
118+
getAccount({}, (info) => {
119+
if (!info["account_email"]) throw new Error("Incorrect account info");
120+
});
121+
}
122+
123+
// getLocations
124+
{
125+
const locations = await getLocations({ limit: 3 });
126+
if (locations.length !== 3) throw new Error("Incorrect locations length");
127+
128+
getLocations({ limit: 3 }, (locations) => {
129+
if (locations.length !== 3) throw new Error("Incorrect locations length");
130+
});
131+
}
132+
133+
console.log("success", process.versions.node);
134+
};
135+
136+
run();

tests/smoke/esm.js

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/**
2+
* Smoke test works for Node.js 14 and newer.
3+
*/
4+
5+
import Dotenv from "dotenv";
6+
import { config, getJson, getHtml, getJsonBySearchId, getHtmlBySearchId, getAccount, getLocations } from "serpapi";
7+
8+
Dotenv.config();
9+
const apiKey = process.env.API_KEY;
10+
11+
console.log("running", process.versions.node);
12+
13+
const params = {
14+
q: "Coffee",
15+
api_key: apiKey,
16+
};
17+
18+
let searchId;
19+
20+
// getJson async await
21+
{
22+
const page1 = await getJson("google", params);
23+
searchId = page1["search_metadata"]["id"];
24+
if (!page1["organic_results"]) throw new Error("No organic results");
25+
const page2 = await page1.next?.();
26+
if (!page2["organic_results"]) throw new Error("No organic results");
27+
}
28+
29+
// getJson callback
30+
{
31+
getJson("google", params, (page1) => {
32+
if (!page1["organic_results"]) throw new Error("No organic results");
33+
page1.next?.((page2) => {
34+
if (!page2["organic_results"]) throw new Error("No organic results");
35+
});
36+
});
37+
}
38+
39+
// getJson using global config
40+
{
41+
config.api_key = apiKey;
42+
const page1 = await getJson("google", { q: "Coffee" });
43+
if (!page1["organic_results"]) throw new Error("No organic results");
44+
const page2 = await page1.next?.();
45+
if (!page2["organic_results"]) throw new Error("No organic results");
46+
}
47+
48+
// getJson pagination loop (async/await)
49+
{
50+
const links = [];
51+
let page = await getJson("google", params);
52+
while (page) {
53+
links.push(...page.organic_results.map(r => r.link));
54+
if (links.length >= 30) break;
55+
page = await page.next?.();
56+
}
57+
if (links.length < 30) throw new Error("Incorrect number of links");
58+
}
59+
60+
// getJson pagination loop (callback)
61+
{
62+
const links = [];
63+
getJson("google", params, (page) => {
64+
links.push(...page.organic_results.map(r => r.link));
65+
if (links.length < 30 && page.next) {
66+
page.next();
67+
} else {
68+
if (links.length < 30) throw new Error("Incorrect number of links");
69+
}
70+
});
71+
}
72+
73+
// getHtml
74+
{
75+
const html = await getHtml("google", params);
76+
if (html.length < 1000) throw new Error("Incorrect HTML");
77+
78+
getHtml("google", params, html => {
79+
if (html.length < 1000) throw new Error("Incorrect HTML");
80+
});
81+
}
82+
83+
// getJsonBySearchId
84+
{
85+
config.api_key = apiKey;
86+
const json = await getJsonBySearchId(searchId);
87+
if (!json["organic_results"]) throw new Error("No organic results");
88+
89+
getJsonBySearchId(searchId, {}, (json) => {
90+
if (!json["organic_results"]) throw new Error("No organic results");
91+
});
92+
}
93+
94+
// getHtmlBySearchId
95+
{
96+
config.api_key = apiKey;
97+
const html = await getHtmlBySearchId(searchId);
98+
if (html.length < 1000) throw new Error("Incorrect HTML");
99+
100+
getHtmlBySearchId(searchId, {}, (html) => {
101+
if (html.length < 1000) throw new Error("Incorrect HTML");
102+
});
103+
}
104+
105+
// getAccount
106+
{
107+
config.api_key = apiKey;
108+
const info = await getAccount();
109+
if (!info["account_email"]) throw new Error("Incorrect account info");
110+
111+
getAccount({}, (info) => {
112+
if (!info["account_email"]) throw new Error("Incorrect account info");
113+
});
114+
}
115+
116+
// getLocations
117+
{
118+
const locations = await getLocations({ limit: 3 });
119+
if (locations.length !== 3) throw new Error("Incorrect locations length");
120+
121+
getLocations({ limit: 3 }, (locations) => {
122+
if (locations.length !== 3) throw new Error("Incorrect locations length");
123+
});
124+
}
125+
126+
console.log("success", process.versions.node);

tests/smoke/package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"dependencies": {
3+
"dotenv": "*",
4+
"serpapi": "../../npm"
5+
},
6+
"scripts": {
7+
"cjs": "node cjs.js",
8+
"esm": "node esm.js"
9+
}
10+
}

0 commit comments

Comments
 (0)