@@ -37,6 +37,113 @@ describe('Module Tests', function() {
37
37
assert . notEqual ( lighthouseTraceCategories . length , 0 ) ;
38
38
} ) ;
39
39
40
+ describe ( 'lighthouse' , ( ) => {
41
+ it ( 'should throw an error when the first parameter is not defined' , async ( ) => {
42
+ const resultPromise = lighthouse ( ) ;
43
+ await expect ( resultPromise ) . rejects . toThrow ( ) ;
44
+ } ) ;
45
+
46
+ it ( 'should throw an error when the first parameter is an empty string' , async ( ) => {
47
+ const resultPromise = lighthouse ( '' ) ;
48
+ await expect ( resultPromise ) . rejects . toThrow ( ) ;
49
+ } ) ;
50
+
51
+ it ( 'should throw an error when the first parameter is not a string' , async ( ) => {
52
+ const resultPromise = lighthouse ( { } ) ;
53
+ await expect ( resultPromise ) . rejects . toThrow ( ) ;
54
+ } ) ;
55
+
56
+ it ( 'should throw an error when the second parameter is not an object' , async ( ) => {
57
+ const resultPromise = lighthouse ( 'chrome://version' , 'flags' ) ;
58
+ await expect ( resultPromise ) . rejects . toThrow ( ) ;
59
+ } ) ;
60
+
61
+ it ( 'should throw an error when the config is invalid' , async ( ) => {
62
+ const resultPromise = lighthouse ( 'chrome://version' , { } , { } ) ;
63
+ await expect ( resultPromise ) . rejects . toThrow ( ) ;
64
+ } ) ;
65
+
66
+ it ( 'should throw an error when the config contains incorrect audits' , async ( ) => {
67
+ const resultPromise = lighthouse ( 'chrome://version' , { } , {
68
+ passes : [ {
69
+ gatherers : [
70
+ 'script-elements' ,
71
+ ] ,
72
+ } ] ,
73
+ audits : [
74
+ 'fluff' ,
75
+ ] ,
76
+ } ) ;
77
+ await expect ( resultPromise ) . rejects . toThrow ( ) ;
78
+ } ) ;
79
+
80
+ it ( 'should throw an error when the url is invalid' , async ( ) => {
81
+ const resultPromise = lighthouse ( 'i-am-not-valid' , { } , { } ) ;
82
+ await expect ( resultPromise ) . rejects . toThrow ( 'INVALID_URL' ) ;
83
+ } ) ;
84
+
85
+ it ( 'should throw an error when the url is invalid protocol (file:///)' , async ( ) => {
86
+ const resultPromise = lighthouse ( 'file:///a/fake/index.html' , { } , { } ) ;
87
+ await expect ( resultPromise ) . rejects . toThrow ( 'INVALID_URL' ) ;
88
+ } ) ;
89
+
90
+ it ( 'should return formatted LHR when given no categories' , async ( ) => {
91
+ const exampleUrl = 'https://www.reddit.com/r/nba' ;
92
+ const result = await lighthouse ( exampleUrl , {
93
+ output : 'html' ,
94
+ } , {
95
+ settings : {
96
+ auditMode : TEST_DIR + '/fixtures/artifacts/perflog/' ,
97
+ formFactor : 'mobile' ,
98
+ } ,
99
+ artifacts : [
100
+ { id : 'MetaElements' , gatherer : 'meta-elements' } ,
101
+ ] ,
102
+ audits : [
103
+ 'viewport' ,
104
+ ] ,
105
+ } ) ;
106
+
107
+ assert . ok ( / < h t m l / . test ( result . report ) , 'did not create html report' ) ;
108
+ assert . ok ( result . artifacts . ViewportDimensions , 'did not set artifacts' ) ;
109
+ assert . ok ( result . lhr . lighthouseVersion ) ;
110
+ assert . ok ( result . lhr . fetchTime ) ;
111
+ assert . equal ( result . lhr . finalUrl , exampleUrl ) ;
112
+ assert . equal ( result . lhr . requestedUrl , exampleUrl ) ;
113
+ assert . equal ( Object . values ( result . lhr . categories ) . length , 0 ) ;
114
+ assert . ok ( result . lhr . audits . viewport ) ;
115
+ assert . strictEqual ( result . lhr . audits . viewport . score , 0 ) ;
116
+ assert . ok ( result . lhr . audits . viewport . explanation ) ;
117
+ assert . ok ( result . lhr . timing ) ;
118
+ assert . ok ( result . lhr . timing . entries . length > 3 , 'timing entries not populated' ) ;
119
+ } ) ;
120
+
121
+ it ( 'should specify the channel as node by default' , async ( ) => {
122
+ const exampleUrl = 'https://www.reddit.com/r/nba' ;
123
+ const result = await lighthouse ( exampleUrl , { } , {
124
+ settings : {
125
+ auditMode : TEST_DIR + '/fixtures/artifacts/perflog/' ,
126
+ formFactor : 'mobile' ,
127
+ } ,
128
+ audits : [ ] ,
129
+ } ) ;
130
+ assert . equal ( result . lhr . configSettings . channel , 'node' ) ;
131
+ } ) ;
132
+
133
+ it ( 'lets consumers pass in a custom channel' , async ( ) => {
134
+ const exampleUrl = 'https://www.reddit.com/r/nba' ;
135
+ const result = await lighthouse ( exampleUrl , { } , {
136
+ settings : {
137
+ auditMode : TEST_DIR + '/fixtures/artifacts/perflog/' ,
138
+ formFactor : 'mobile' ,
139
+ channel : 'custom' ,
140
+ } ,
141
+ audits : [ ] ,
142
+ } ) ;
143
+ assert . equal ( result . lhr . configSettings . channel , 'custom' ) ;
144
+ } ) ;
145
+ } ) ;
146
+
40
147
describe ( 'legacyNavigation' , ( ) => {
41
148
it ( 'should throw an error when the first parameter is not defined' , function ( ) {
42
149
return legacyNavigation ( )
0 commit comments