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+ } ) ;
0 commit comments