Skip to content

Commit e83736f

Browse files
committed
Add automated smoke tests
1 parent 79cad31 commit e83736f

10 files changed

Lines changed: 396 additions & 5 deletions

File tree

.github/workflows/build.yml

Lines changed: 79 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ on: push
44

55
jobs:
66
build:
7+
name: "Deno tests and build npm files"
78
runs-on: ubuntu-22.04
89
steps:
910
- name: Checkout repo
@@ -28,8 +29,84 @@ 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

34-
- name: Test building of npm files
35+
- name: Build npm files
3536
run: deno task npm
37+
38+
- name: Zip build files
39+
run: zip npm.zip ./npm -r
40+
41+
- name: Upload build files for smoke tests
42+
uses: actions/upload-artifact@v3
43+
with:
44+
name: npm
45+
path: npm.zip
46+
retention-days: 1
47+
48+
smoke-tests-commonjs:
49+
name: "Smoke tests (CommonJS)"
50+
needs: build
51+
runs-on: ubuntu-22.04
52+
strategy:
53+
matrix:
54+
node-version: [7.x, 8.x, 9.x, 10.x, 11.x, 12.x, 13.x, 14.x, 15.x, 16.x, 17.x, 18.x, 19.x]
55+
steps:
56+
- name: Checkout repo
57+
uses: actions/checkout@v3
58+
59+
- name: Setup Node
60+
uses: actions/setup-node@v3
61+
with:
62+
node-version: ${{ matrix.node-version }}
63+
registry-url: 'https://registry.npmjs.org'
64+
65+
- name: Download build files
66+
uses: actions/download-artifact@v3
67+
with:
68+
name: npm
69+
70+
- name: Unzip build files
71+
run: unzip npm.zip
72+
73+
- name: Run smoke tests
74+
env:
75+
SERPAPI_TEST_KEY: ${{ secrets.SERPAPI_TEST_KEY }}
76+
run: |
77+
cd tests/smoke/commonjs
78+
npm i
79+
npm test
80+
81+
smoke-tests-esm:
82+
name: "Smoke tests (ESM)"
83+
needs: build
84+
runs-on: ubuntu-22.04
85+
strategy:
86+
matrix:
87+
node-version: [14.x, 15.x, 16.x, 17.x, 18.x, 19.x]
88+
steps:
89+
- name: Checkout repo
90+
uses: actions/checkout@v3
91+
92+
- name: Setup Node
93+
uses: actions/setup-node@v3
94+
with:
95+
node-version: ${{ matrix.node-version }}
96+
registry-url: 'https://registry.npmjs.org'
97+
98+
- name: Download build files
99+
uses: actions/download-artifact@v3
100+
with:
101+
name: npm
102+
103+
- name: Unzip build files
104+
run: unzip npm.zip
105+
106+
- name: Run smoke tests
107+
env:
108+
SERPAPI_TEST_KEY: ${{ secrets.SERPAPI_TEST_KEY }}
109+
run: |
110+
cd tests/smoke/esm
111+
npm i
112+
npm test

CONTRIBUTING.md

Lines changed: 16 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
],
@@ -115,6 +116,20 @@ To run [examples](./examples/) on your local source files, follow these steps.
115116
}
116117
```
117118

119+
## Run smoke tests
120+
121+
These test key functionality on different Node.js versions. They are ran on
122+
GitHub Actions, see the [build workflow](.github/workflows/build.yml) for more
123+
details.
124+
125+
To run these locally, follow these steps.
126+
127+
1. Run `deno task npm` to build the files.
128+
2. Change directory to either the `commonjs` or `esm` folder.
129+
3. Setup the intended Node.js version. For example, if you're using `nvm`, you
130+
can run `nvm use 14` to run Node.js 14 for the current shell.
131+
4. Run `npm i`, then `npm test`.
132+
118133
## Update documentation
119134

120135
- Every exposed function must have associated JSDoc comments.

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/.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/commonjs/.env.example

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

tests/smoke/commonjs/commonjs.js

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
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.SERPAPI_TEST_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+
{
22+
console.log("getJson async await");
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+
{
33+
console.log("getJson callback");
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+
{
45+
console.log("getJson using global config");
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+
{
56+
console.log("getJson pagination loop (async/await)");
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+
{
68+
console.log("getJson pagination loop (callback)");
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+
{
81+
console.log("getHtml");
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+
{
91+
console.log("getJsonBySearchId");
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+
{
102+
console.log("getHtmlBySearchId");
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+
{
113+
console.log("getAccount");
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+
{
124+
console.log("getLocations");
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().catch((e) => {
137+
console.error(e);
138+
process.exitCode = 1;
139+
});

tests/smoke/commonjs/package.json

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

tests/smoke/esm/.env.example

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

0 commit comments

Comments
 (0)