Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add automated smoke tests
  • Loading branch information
sebastianquek committed Mar 16, 2023
commit e83736f5220797f09a3b01437e76f61faa3d2302
81 changes: 79 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on: push

jobs:
build:
name: "Deno tests and build npm files"
runs-on: ubuntu-22.04
steps:
- name: Checkout repo
Expand All @@ -28,8 +29,84 @@ jobs:
- name: Setup Node
uses: actions/setup-node@v3
with:
node-version: '18.x'
node-version: '18.x' # Build files using a fixed node version
registry-url: 'https://registry.npmjs.org'

- name: Test building of npm files
- name: Build npm files
run: deno task npm

- name: Zip build files
run: zip npm.zip ./npm -r

- name: Upload build files for smoke tests
uses: actions/upload-artifact@v3
with:
name: npm
path: npm.zip
retention-days: 1

smoke-tests-commonjs:
name: "Smoke tests (CommonJS)"
needs: build
runs-on: ubuntu-22.04
strategy:
matrix:
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]
steps:
- name: Checkout repo
uses: actions/checkout@v3

- name: Setup Node
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
registry-url: 'https://registry.npmjs.org'

- name: Download build files
uses: actions/download-artifact@v3
with:
name: npm

- name: Unzip build files
run: unzip npm.zip

- name: Run smoke tests
env:
SERPAPI_TEST_KEY: ${{ secrets.SERPAPI_TEST_KEY }}
run: |
cd tests/smoke/commonjs
npm i
npm test

smoke-tests-esm:
name: "Smoke tests (ESM)"
needs: build
runs-on: ubuntu-22.04
strategy:
matrix:
node-version: [14.x, 15.x, 16.x, 17.x, 18.x, 19.x]
steps:
- name: Checkout repo
uses: actions/checkout@v3

- name: Setup Node
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
registry-url: 'https://registry.npmjs.org'

- name: Download build files
uses: actions/download-artifact@v3
with:
name: npm

- name: Unzip build files
run: unzip npm.zip

- name: Run smoke tests
env:
SERPAPI_TEST_KEY: ${{ secrets.SERPAPI_TEST_KEY }}
run: |
cd tests/smoke/esm
npm i
npm test
17 changes: 16 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ If you use VSCode, use the following settings (`.vscode/settings.json`):
"mod.ts",
"version.ts",
"src",
"tests",
"tests/*.ts",
"tests/engines/",
"scripts",
"examples/deno"
],
Expand Down Expand Up @@ -115,6 +116,20 @@ To run [examples](./examples/) on your local source files, follow these steps.
}
```

## Run smoke tests

These test key functionality on different Node.js versions. They are ran on
GitHub Actions, see the [build workflow](.github/workflows/build.yml) for more
details.

To run these locally, follow these steps.

1. Run `deno task npm` to build the files.
2. Change directory to either the `commonjs` or `esm` folder.
3. Setup the intended Node.js version. For example, if you're using `nvm`, you
can run `nvm use 14` to run Node.js 14 for the current shell.
4. Run `npm i`, then `npm test`.

## Update documentation

- Every exposed function must have associated JSDoc comments.
Expand Down
10 changes: 8 additions & 2 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,18 @@
},
"fmt": {
"files": {
"exclude": ["npm/", "examples/node"]
"exclude": ["npm/", "examples/node", "tests/smoke/"]
}
},
"lint": {
"files": {
"exclude": ["npm/", "examples/node"]
"exclude": ["npm/", "examples/node", "tests/smoke/"]
}
},
"test": {
"files": {
"include": ["tests/"],
"exclude": ["tests/smoke/"]
}
},
"compilerOptions": {
Expand Down
3 changes: 3 additions & 0 deletions tests/smoke/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Smoke tests run on different node versions which generate different
# package-lock.json files.
package-lock.json
1 change: 1 addition & 0 deletions tests/smoke/commonjs/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SERPAPI_TEST_KEY=YOUR_API_KEY
139 changes: 139 additions & 0 deletions tests/smoke/commonjs/commonjs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/**
* Smoke test works for Node.js 7 and newer.
*/

const Dotenv = require("dotenv");
const { config, getJson, getHtml, getJsonBySearchId, getHtmlBySearchId, getAccount, getLocations } = require("serpapi");

Dotenv.config();
const apiKey = process.env.SERPAPI_TEST_KEY;

const run = async () => {
console.log("running", process.versions.node);

const params = {
q: "Coffee",
api_key: apiKey,
};

let searchId;

{
console.log("getJson async await");
const page1 = await getJson("google", params);
searchId = page1["search_metadata"]["id"];
if (!page1["organic_results"]) throw new Error("No organic results");
if (page1.next) {
const page2 = await page1.next();
if (!page2["organic_results"]) throw new Error("No organic results");
}
}

{
console.log("getJson callback");
getJson("google", params, (page1) => {
if (!page1["organic_results"]) throw new Error("No organic results");
if (page1.next) {
page1.next((page2) => {
if (!page2["organic_results"]) throw new Error("No organic results");
});
}
});
}

{
console.log("getJson using global config");
config.api_key = apiKey;
const page1 = await getJson("google", { q: "Coffee" });
if (!page1["organic_results"]) throw new Error("No organic results");
if (page1.next) {
const page2 = await page1.next();
if (!page2["organic_results"]) throw new Error("No organic results");
}
}

{
console.log("getJson pagination loop (async/await)");
const links = [];
let page = await getJson("google", params);
while (page) {
links.push(...page.organic_results.map(r => r.link));
if (links.length >= 30) break;
page = page.next ? await page.next(): undefined;
}
if (links.length < 30) throw new Error("Incorrect number of links");
}

{
console.log("getJson pagination loop (callback)");
const links = [];
getJson("google", params, (page) => {
links.push(...page.organic_results.map(r => r.link));
if (links.length < 30 && page.next) {
page.next();
} else {
if (links.length < 30) throw new Error("Incorrect number of links");
}
});
}

{
console.log("getHtml");
const html = await getHtml("google", params);
if (html.length < 1000) throw new Error("Incorrect HTML");

getHtml("google", params, html => {
if (html.length < 1000) throw new Error("Incorrect HTML");
});
}

{
console.log("getJsonBySearchId");
config.api_key = apiKey;
const json = await getJsonBySearchId(searchId);
if (!json["organic_results"]) throw new Error("No organic results");

getJsonBySearchId(searchId, {}, (json) => {
if (!json["organic_results"]) throw new Error("No organic results");
});
}

{
console.log("getHtmlBySearchId");
config.api_key = apiKey;
const html = await getHtmlBySearchId(searchId);
if (html.length < 1000) throw new Error("Incorrect HTML");

getHtmlBySearchId(searchId, {}, (html) => {
if (html.length < 1000) throw new Error("Incorrect HTML");
});
}

{
console.log("getAccount");
config.api_key = apiKey;
const info = await getAccount();
if (!info["account_email"]) throw new Error("Incorrect account info");

getAccount({}, (info) => {
if (!info["account_email"]) throw new Error("Incorrect account info");
});
}

{
console.log("getLocations");
const locations = await getLocations({ limit: 3 });
if (locations.length !== 3) throw new Error("Incorrect locations length");

getLocations({ limit: 3 }, (locations) => {
if (locations.length !== 3) throw new Error("Incorrect locations length");
});
}

console.log("success", process.versions.node);
};

run().catch((e) => {
console.error(e);
process.exitCode = 1;
});
9 changes: 9 additions & 0 deletions tests/smoke/commonjs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"dependencies": {
"dotenv": "*",
"serpapi": "../../../npm"
},
"scripts": {
"test": "node commonjs.js"
}
}
1 change: 1 addition & 0 deletions tests/smoke/esm/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SERPAPI_TEST_KEY=YOUR_API_KEY
Loading