varsnap 1.6.10 → 1.8.2

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.
package/CHANGELOG.md CHANGED
@@ -1,6 +1,38 @@
1
1
  CHANGELOG
2
2
  =========
3
3
 
4
+ 1.8.2 (2022-01-29)
5
+ ------------------
6
+
7
+ - Remove dependency on `remove-query-string`
8
+ - Return branch from `setGitBranchEnv`
9
+ - Update dependencies
10
+
11
+
12
+ 1.8.1 (2022-01-15)
13
+ ------------------
14
+
15
+ - Fix varsnap import config
16
+ - Update dependencies
17
+
18
+
19
+ 1.8.0 (2022-01-14)
20
+ ------------------
21
+
22
+ - Add `gitbranchenv` to make getting git branch names easier
23
+ - Add documentation of git branch usage
24
+ - Cleanup
25
+ - Update depencencies
26
+
27
+
28
+ 1.7.0 (2022-01-03)
29
+ ------------------
30
+
31
+ - Send git branch when generating trial groups
32
+ - Update dependencies
33
+ - Refactors
34
+
35
+
4
36
  1.6.10 (2022-01-01)
5
37
  -------------------
6
38
 
package/README.md CHANGED
@@ -17,6 +17,7 @@ The varsnap decorator needs to be configured with a `config` object with these v
17
17
 
18
18
  - `varsnap` - Should be either `true` or `false`. Varsnap will be disabled if the variable is anything other than `true`.
19
19
  - `env` - If set to `development`, the client will receive events from production. If set to `production`, the client will emit events.
20
+ - `branch` - Used for labeling test runs by (git) branch. Test runs on the `master` branch will update your varsnap badge
20
21
  - `producerToken` - Only clients with this token may emit production snapshots. Copied from https://www.varsnap.com/user/
21
22
  - `consumerToken` - Only clients with this token may emit development snapshots. Copied from https://www.varsnap.com/user/
22
23
 
@@ -38,6 +39,7 @@ var varsnap = window.varsnap;
38
39
  varsnap.updateConfig({
39
40
  varsnap: true,
40
41
  env: 'production',
42
+ branch: 'master',
41
43
  producerToken: 'producer-abcd'
42
44
  });
43
45
 
@@ -112,6 +114,17 @@ const options = {
112
114
  minify(code, options);
113
115
  ```
114
116
 
117
+ Git Branch
118
+ ----------
119
+
120
+ Providing a git branch to the client allows Varsnap to categorize test runs.
121
+ Tests on the master branch will also be reflected in your Varsnap badge image.
122
+
123
+ For builds in environments that cannot access git branch names, such as builds
124
+ in headless browsers, `varsnap/gitbranchenv.ts` will read git branch names and
125
+ set them into a `GIT_BRANCH` environment variable. This can then be copied
126
+ into the varsnap config when testing.
127
+
115
128
  Client Development
116
129
  ------------------
117
130
 
@@ -122,6 +135,5 @@ Client Development
122
135
  3. Update package.json version and run `npm install`
123
136
  4. Update `.uglify.json` and `.uglify.min.json`
124
137
  5. Run `npm run package`
125
- 6. Git commit and tag
126
- 7. `npm publish`
138
+ 6. Git commit, tag, and push
127
139
  8. Upload `dist/*` to https://github.com/albertyw/varsnap.js/releases
@@ -1,16 +1,15 @@
1
- /// <reference types="node" />
2
- import querystring = require('querystring');
3
- export declare const version = "1.6.10";
1
+ export declare const version = "1.8.2";
4
2
  export declare const clientName: string;
5
3
  export interface Config {
6
4
  varsnap: boolean;
7
5
  env: string;
6
+ branch: string;
8
7
  producerToken?: string;
9
8
  consumerToken?: string;
10
9
  logger?: Logger;
11
10
  }
12
11
  declare type ConfigKeys = keyof Config;
13
- declare type rawJSONObject = ConsumeSnapResponse | ProduceTrialResponse | TrialGroupResponse | querystring.ParsedUrlQueryInput;
12
+ declare type rawJSONObject = ConsumeSnapResponse | ProduceTrialResponse | TrialGroupRequest | TrialGroupResponse | Record<string, string>;
14
13
  export interface Logger {
15
14
  debug(s: string): void;
16
15
  log(s: string): void;
@@ -29,6 +28,8 @@ export declare class Util {
29
28
  static getProducers(): Producer[];
30
29
  static getConsumers(): Consumer[];
31
30
  static limitString(x: TargetFunctionInput[] | TargetFunctionOutput): string;
31
+ static isNode(): boolean;
32
+ static getBranch(config: Config): Promise<string>;
32
33
  }
33
34
  export declare class Producer {
34
35
  targetFunc: TargetFunction;
@@ -84,6 +85,11 @@ export declare namespace varsnap {
84
85
  var version: string;
85
86
  var runTests: typeof import("./core").runTests;
86
87
  }
88
+ interface TrialGroupRequest {
89
+ branch: string;
90
+ consumer_token: string;
91
+ client: string;
92
+ }
87
93
  interface TrialGroupResponse {
88
94
  status: string;
89
95
  project_id: string;
@@ -2,16 +2,17 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.runTests = exports.varsnap = exports.Consumer = exports.Producer = exports.Util = exports.clientName = exports.version = void 0;
4
4
  const axios_1 = require("axios");
5
- const querystring = require("querystring");
5
+ const child_process_1 = require("child_process");
6
6
  const produceURL = 'https://www.varsnap.com/api/snap/produce/';
7
7
  const consumeURL = 'https://www.varsnap.com/api/snap/consume/';
8
8
  const produceTrialURL = 'https://www.varsnap.com/api/trial/produce/';
9
9
  const trialGroupURL = 'https://www.varsnap.com/api/trial_group/';
10
- exports.version = '1.6.10';
10
+ exports.version = '1.8.2';
11
11
  exports.clientName = 'javascript.' + exports.version;
12
12
  const defaultConfig = {
13
13
  varsnap: true,
14
14
  env: 'development',
15
+ branch: '',
15
16
  producerToken: '',
16
17
  consumerToken: '',
17
18
  logger: console, // eslint-disable-line no-console
@@ -37,7 +38,7 @@ class Util {
37
38
  }
38
39
  config[configKey] = configValue;
39
40
  }
40
- if (configKey === 'env' || configKey === 'producerToken' || configKey === 'consumerToken') {
41
+ if (configKey === 'env' || configKey === 'producerToken' || configKey === 'consumerToken' || configKey === 'branch') {
41
42
  config[configKey] = configValue;
42
43
  }
43
44
  if (configKey === 'logger') {
@@ -61,20 +62,24 @@ class Util {
61
62
  return 'javascript.' + exports.version + '.' + signature;
62
63
  }
63
64
  static ajax(url, requestData) {
65
+ const requestParams = new URLSearchParams(requestData);
66
+ const data = requestParams.toString();
64
67
  const options = {
65
68
  method: 'POST',
66
69
  url: url,
67
70
  headers: {
68
71
  'Content-Type': 'application/x-www-form-urlencoded',
69
72
  },
70
- data: querystring.stringify(requestData),
73
+ data: data,
71
74
  responseType: 'json',
72
75
  };
73
- if (typeof process === 'object' && typeof window === 'undefined' && options.headers) {
76
+ if (Util.isNode() && options.headers) {
74
77
  options.headers['User-Agent'] = 'varsnap.js';
75
78
  }
76
79
  const ajaxPromise = (0, axios_1.default)(options).then((response) => {
77
80
  return response.data;
81
+ }).catch((error) => {
82
+ return error;
78
83
  });
79
84
  return ajaxPromise;
80
85
  }
@@ -114,6 +119,36 @@ class Util {
114
119
  }
115
120
  return limitedString.substring(0, 30 - ellipsis.length) + ellipsis;
116
121
  }
122
+ static isNode() {
123
+ if (typeof process !== 'object') {
124
+ return false;
125
+ }
126
+ if (typeof window !== 'undefined') {
127
+ return false;
128
+ }
129
+ return true;
130
+ }
131
+ static getBranch(config) {
132
+ const configBranch = Util.getConfig(config, 'branch');
133
+ if (configBranch) {
134
+ const configPromise = new Promise((resolve) => {
135
+ return resolve(configBranch);
136
+ });
137
+ return configPromise;
138
+ }
139
+ if (!Util.isNode()) {
140
+ const defaultPromise = new Promise((resolve) => {
141
+ return resolve('');
142
+ });
143
+ return defaultPromise;
144
+ }
145
+ const command = 'git rev-parse --abbrev-ref HEAD';
146
+ return new Promise((resolve) => {
147
+ (0, child_process_1.exec)(command, (err, stdout) => {
148
+ return resolve(stdout.trim());
149
+ });
150
+ });
151
+ }
117
152
  }
118
153
  exports.Util = Util;
119
154
  class Producer {
@@ -266,7 +301,7 @@ class Consumer {
266
301
  'consumer_token': Util.getConfig(this.config, 'consumerToken'),
267
302
  'snap_id': snap.id,
268
303
  'test_outputs': Producer.serialize(localOutputs),
269
- 'matches': matches,
304
+ 'matches': matches.toString(),
270
305
  'snap_outputs_formatted': Producer.serialize_formatted(prodOutputs),
271
306
  'test_outputs_formatted': Producer.serialize_formatted(localOutputs),
272
307
  };
@@ -346,11 +381,19 @@ varsnap.reset = function reset() {
346
381
  };
347
382
  varsnap.version = exports.version;
348
383
  function getTrialGroup(consumerConfig) {
349
- const data = {
350
- 'consumer_token': Util.getConfig(consumerConfig, 'consumerToken'),
351
- 'client': exports.clientName,
352
- };
353
- const ajax = Util.ajax(trialGroupURL, data);
384
+ const getBranch = Util.getBranch(consumerConfig);
385
+ const getRequestConfig = getBranch.then((branch) => {
386
+ const data = {
387
+ 'branch': branch,
388
+ 'consumer_token': Util.getConfig(consumerConfig, 'consumerToken'),
389
+ 'client': exports.clientName,
390
+ };
391
+ return data;
392
+ });
393
+ const ajax = getRequestConfig.then((data) => {
394
+ const raw = data;
395
+ return Util.ajax(trialGroupURL, raw);
396
+ });
354
397
  const trialGroup = ajax.then((rawResponse) => {
355
398
  const response = rawResponse;
356
399
  return response;
@@ -0,0 +1 @@
1
+ export declare function setGitBranchEnv(): Promise<string>;
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.setGitBranchEnv = void 0;
4
+ const core_1 = require("./core");
5
+ /* Read git branch information from git and save it into an environment variable
6
+ * Useful when combined with envify (or loose-envify) when packaging browser apps
7
+ */
8
+ function setGitBranchEnv() {
9
+ const config = {
10
+ varsnap: true,
11
+ env: 'none',
12
+ branch: '',
13
+ };
14
+ return core_1.Util.getBranch(config)
15
+ .then((branch) => {
16
+ process.env.GIT_BRANCH = branch;
17
+ return branch;
18
+ });
19
+ }
20
+ exports.setGitBranchEnv = setGitBranchEnv;
21
+ setGitBranchEnv();