html-snapshots 1.2.0 → 1.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -27,6 +27,7 @@ const defaults = Object.freeze({
27
27
  useJQuery: false,
28
28
  verbose: false,
29
29
  phantomjsOptions: "",
30
+ puppeteerLaunchOptions: {},
30
31
  debug: {
31
32
  flag: "false",
32
33
  slowMo: 500
@@ -86,6 +87,23 @@ function supplyMissingDefault (options, name) {
86
87
  }
87
88
  }
88
89
 
90
+ /**
91
+ * Prepare single Object options so that it is always used for all pages (globally).
92
+ *
93
+ * @param {String} name - The property name.
94
+ * @param {Object} options - Input generator options.
95
+ */
96
+ function prepSingleObjectOptions (name, options) {
97
+ const objectOpt = options[name];
98
+ const toString = Object.prototype.toString;
99
+ const isObj = toString.call(objectOpt) === "[object Object]";
100
+ if (isObj) {
101
+ options[name] = {
102
+ __default: objectOpt
103
+ };
104
+ }
105
+ }
106
+
89
107
  /**
90
108
  * Prepare options for use by an input generator.
91
109
  *
@@ -96,11 +114,20 @@ function prepOptions (options, listener) {
96
114
  // Ensure defaults are represented
97
115
  common.ensure(options, defaults);
98
116
 
117
+ // Prep single, global object options for normalization.
118
+ [
119
+ "puppeteerLaunchOptions"
120
+ ]
121
+ .forEach((optionName) => {
122
+ prepSingleObjectOptions(optionName, options);
123
+ });
124
+
99
125
  // Normalize certain arguments if they are not functions.
100
126
  // The certain arguments are per-page options.
101
127
  // However, outputPath is a special case
102
128
  [
103
- "selector", "timeout", "useJQuery", "verbose", "phantomjsOptions"
129
+ "selector", "timeout", "useJQuery", "verbose", "phantomjsOptions",
130
+ "puppeteerLaunchOptions"
104
131
  ]
105
132
  .forEach((perPageOption) => {
106
133
  options[perPageOption] = normalize(options[perPageOption]);
@@ -228,7 +255,8 @@ module.exports = {
228
255
  * @param {Function} options.userJQuery - Produces a jquery bool given a page url.
229
256
  * @param {Function} options.verbose - Produces a verbose bool given a page url.
230
257
  * @param {Function} options.phantomjsOptions - Produces phantomjs options given a page url.
231
- * @param {Function} options.debug - Produces debug options given a page url.
258
+ * @param {Function} options.puppeteerLaunchOptions - Produces puppeteer launch options given a page url.
259
+ * @param {Object} options.debug - Produces debug options given a page url.
232
260
  * @param {Function} options._inputEmitter - The EventEmitter to generate input.
233
261
  * @param {String} page - The url to the page resource.
234
262
  * @returns {String} The full output file path.
@@ -262,7 +290,9 @@ module.exports = {
262
290
  verbose: options.verbose(page),
263
291
  // map the input page to phantomJS options
264
292
  phantomjsOptions: options.phantomjsOptions(page),
265
- // useful for testing, debugging
293
+ // map the input page to puppeteer launch options
294
+ puppeteerLaunchOptions: options.puppeteerLaunchOptions(page),
295
+ // useful for puppeteer testing, debugging
266
296
  debug: options.debug,
267
297
  __page: page
268
298
  });
@@ -12,6 +12,7 @@
12
12
  * [6] - filter
13
13
  * [7] - debug
14
14
  * [8] - slowMo
15
+ * [9] - arbitrary launchOptions
15
16
  */
16
17
  const puppeteer = require('puppeteer');
17
18
  const path = require('path');
@@ -31,13 +32,17 @@ const options = {
31
32
  timeout: process.argv[5] ? parseInt(process.argv[5], 10) : 10000,
32
33
  filter: process.argv[6] && process.argv[6] !== 'false' ? process.argv[6] : null,
33
34
  debug: process.argv[7] && process.argv[7].toLowerCase() === 'true' ? true : false,
34
- slowMo: process.argv[8] ? parseInt(process.argv[8], 10) : 500
35
+ slowMo: process.argv[8] ? parseInt(process.argv[8], 10) : 500,
36
+ launchOpts: process.argv[9] ? JSON.parse(process.argv[9]) : null
37
+ };
38
+ const launchOptions = {
39
+ ...(options.debug ? {
40
+ headless: false,
41
+ slowMo: options.slowMo,
42
+ devtools: true
43
+ } : {}),
44
+ ...(options.launchOpts ?? {})
35
45
  };
36
- const launchOptions = options.debug ? {
37
- headless: false,
38
- slowMo: options.slowMo,
39
- devtools: true
40
- } : {};
41
46
 
42
47
  /**
43
48
  * Run the snapshot script.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "html-snapshots",
3
- "version": "1.2.0",
3
+ "version": "1.3.1",
4
4
  "author": {
5
5
  "name": "Alex Grant",
6
6
  "email": "alex@localnerve.com",
@@ -57,14 +57,14 @@
57
57
  "lodash": "4.17.21",
58
58
  "mkdirp": "1.0.4",
59
59
  "phantomjs-prebuilt": "2.1.16",
60
- "puppeteer": "^19.0.0",
60
+ "puppeteer": "^19.2.0",
61
61
  "rimraf": "3.0.2",
62
62
  "xml2js": "0.4.23"
63
63
  },
64
64
  "devDependencies": {
65
- "eslint": "8.25.0",
65
+ "eslint": "8.26.0",
66
66
  "express": "4.18.2",
67
- "mocha": "10.0.0",
67
+ "mocha": "10.1.0",
68
68
  "c8": "7.12.0",
69
69
  "precommit-hook": "3.0.0",
70
70
  "server-destroy": "1.0.1",
@@ -43,7 +43,7 @@ function puppeteerTests () {
43
43
  });
44
44
 
45
45
  it("should succeed with minimal args", function (done) {
46
- this.timeout(5000);
46
+ this.timeout(4000);
47
47
 
48
48
  rimraf(outputDir);
49
49
 
@@ -54,6 +54,28 @@ function puppeteerTests () {
54
54
  ], done);
55
55
  });
56
56
 
57
+ it("should succeed with stringified puppeteerLaunchOptions", function (done) {
58
+ this.timeout(4000);
59
+ rimraf(outputDir);
60
+
61
+ // override the debug options to prove stringified launch opts worked.
62
+ const puppeteerLaunchOptions = {
63
+ headless: true,
64
+ devTools: false
65
+ };
66
+
67
+ spawnPuppeteer([
68
+ outputFile,
69
+ `http://localhost:${port}/`,
70
+ "body",
71
+ 4000,
72
+ "false",
73
+ "true",
74
+ 10,
75
+ JSON.stringify(puppeteerLaunchOptions)
76
+ ], done);
77
+ });
78
+
57
79
  it("should fail if insufficient input args", function (done) {
58
80
  rimraf(outputDir);
59
81
 
@@ -98,6 +120,7 @@ function puppeteerTests () {
98
120
  10
99
121
  ], done);
100
122
  });
123
+
101
124
  };
102
125
  }
103
126
 
@@ -921,6 +921,96 @@ describe("input-generator", function () {
921
921
 
922
922
  });
923
923
 
924
+ describe("puppeteerLaunchOptions option", function () {
925
+ it("should accept a single object and apply globally", function (done) {
926
+ let count = 0;
927
+ const puppeteerLaunchOptions = {
928
+ one: 'one',
929
+ two: 2,
930
+ three: ['three', 'four', 'five'],
931
+ six: {
932
+ seven: 'seven'
933
+ }
934
+ };
935
+
936
+ const result = gen.run(options.decorate({
937
+ source,
938
+ puppeteerLaunchOptions,
939
+ outputDir: thisOutputDir
940
+ }), function (input) {
941
+ const classString = Object.prototype.toString;
942
+
943
+ assert.equal(classString.call(input.puppeteerLaunchOptions), classString.call(puppeteerLaunchOptions),
944
+ `${input.__page}:\ninput.puppeteerLaunchOptions should be an object:\n${
945
+ require("util").inspect(input.puppeteerLaunchOptions)
946
+ }`
947
+ );
948
+
949
+ assert.deepEqual(input.puppeteerLaunchOptions, puppeteerLaunchOptions,
950
+ `${input.__page}:\ninput.puppeteerLaunchOptions should be equal to the original input`);
951
+
952
+ count++;
953
+ if (count === urls) {
954
+ done();
955
+ }
956
+ });
957
+
958
+ result
959
+ .then(function () {
960
+ assert.ok(true);
961
+ })
962
+ .catch(function (err) {
963
+ assert.fail(`Run should not fail: ${err.toString()}`);
964
+ });
965
+ });
966
+
967
+ it("should accept a function and apply per url", function (done) {
968
+ const plo1 = { one: "one" },
969
+ plo2 = { two: "two" },
970
+ plo3 = { three: "three" };
971
+
972
+ const globalOptions = {
973
+ "/": plo1,
974
+ "/contact": plo2,
975
+ "/services/carpets": plo3
976
+ };
977
+ const localOptions = {
978
+ "http://northstar.local/": plo1,
979
+ "http://northstar.local/contact": plo2,
980
+ "http://northstar.local/services/carpets": plo3
981
+ };
982
+ const opts = globalUrl ? globalOptions : localOptions;
983
+ let count = 0;
984
+ const ploFn = function (url) {
985
+ return opts[url];
986
+ };
987
+
988
+ const result = gen.run(options.decorate({
989
+ source: source,
990
+ outputDir: thisOutputDir,
991
+ puppeteerLaunchOptions: ploFn
992
+ }), function (input) {
993
+ const testOption = opts[input.__page] ? opts[input.__page] : base.defaults({}).puppeteerLaunchOptions;
994
+
995
+ assert.deepEqual(input.puppeteerLaunchOptions, testOption,
996
+ `${input.__page}:\ninput.puppeteerLaunchOptions: ${input.puppeteerLaunchOptions} != testPuppeteerLaunchOption: ${testOption}`);
997
+
998
+ count++;
999
+ if (count === urls) {
1000
+ done();
1001
+ }
1002
+ });
1003
+
1004
+ result
1005
+ .then(function () {
1006
+ assert.ok(true);
1007
+ })
1008
+ .catch(function (err) {
1009
+ assert.fail(`Run should not fail: ${err.toString()}`);
1010
+ });
1011
+ });
1012
+ });
1013
+
924
1014
  describe("url behavior", function () {
925
1015
 
926
1016
  it("should replace the default hostname in results", function (done) {