jest-date-mock 1.0.5-beta.1 → 1.0.8

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.
@@ -0,0 +1,12 @@
1
+ # These are supported funding model platforms
2
+
3
+ github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
4
+ patreon: # Replace with a single Patreon username
5
+ open_collective: # Replace with a single Open Collective username
6
+ ko_fi: # Replace with a single Ko-fi username
7
+ tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
8
+ community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
9
+ liberapay: # Replace with a single Liberapay username
10
+ issuehunt: # Replace with a single IssueHunt username
11
+ otechie: # Replace with a single Otechie username
12
+ custom: ['https://paypal.me/hustcc', 'https://atool.vip']
package/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2018 hustcc
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
14
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
15
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
17
+ DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
18
+ OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
19
+ OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # jest-date-mock
2
2
 
3
- > Mock `window.Date` when run unit test cases with jest. Make tests of `Date` easier.
3
+ > Mock `Date` when run unit test cases with jest. Make tests of `Date` easier.
4
4
 
5
5
  [![Build Status](https://travis-ci.org/hustcc/jest-date-mock.svg?branch=master)](https://travis-ci.org/hustcc/jest-date-mock)
6
6
  [![Coverage Status](https://coveralls.io/repos/github/hustcc/jest-date-mock/badge.svg?branch=master)](https://coveralls.io/github/hustcc/jest-date-mock)
package/lib/date.js CHANGED
@@ -3,46 +3,58 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
+ exports.now = exports.clear = exports.advanceTo = exports.advanceBy = void 0;
7
+
6
8
  /**
7
9
  * Created by hustcc 18/05/27.
8
10
  * Contract: i@hust.cc
9
11
  */
12
+ // undefined means unmock.
13
+ var DEFAULT = undefined; // current Date of timestamp
10
14
 
11
- var DEFAULT = undefined;
12
-
13
- // current Date of timestamp
14
15
  var nowDate = DEFAULT;
15
-
16
16
  /**
17
17
  * move date by offset `ms`
18
18
  * @param ms
19
19
  */
20
- var advanceBy = exports.advanceBy = function advanceBy(ms) {
21
- return nowDate += ms || 0;
22
- };
23
20
 
21
+ var advanceBy = function advanceBy(ms) {
22
+ return nowDate = (nowDate === undefined ? +new Date() : nowDate) + (ms || 0);
23
+ };
24
24
  /**
25
25
  * reset Date
26
26
  * if no parameter, then set to 0
27
27
  * @param ms
28
28
  * @returns {*}
29
29
  */
30
- var advanceTo = exports.advanceTo = function advanceTo(ms) {
30
+
31
+
32
+ exports.advanceBy = advanceBy;
33
+
34
+ var advanceTo = function advanceTo(ms) {
31
35
  return nowDate = ms ? +new Date(ms) : 0;
32
36
  };
33
-
34
37
  /**
35
38
  * clear mock
36
39
  * @returns {undefined}
37
40
  */
38
- var clear = exports.clear = function clear() {
41
+
42
+
43
+ exports.advanceTo = advanceTo;
44
+
45
+ var clear = function clear() {
39
46
  return nowDate = DEFAULT;
40
47
  };
41
-
42
48
  /**
43
49
  * current
44
50
  * @returns {number}
45
51
  */
46
- var now = exports.now = function now() {
52
+
53
+
54
+ exports.clear = clear;
55
+
56
+ var now = function now() {
47
57
  return nowDate;
48
- };
58
+ };
59
+
60
+ exports.now = now;
package/lib/index.d.ts ADDED
@@ -0,0 +1,25 @@
1
+ /**
2
+ * Changes date by an offset. Can be negative.
3
+ *
4
+ * @param ms
5
+ * @default 0
6
+ */
7
+ export declare function advanceBy(ms?: number): void;
8
+
9
+ /**
10
+ * Sets date to a timestamp or Date.
11
+ *
12
+ * @param ms
13
+ * @default 0
14
+ */
15
+ export declare function advanceTo(ms?: number | Date | string): void;
16
+
17
+ /**
18
+ * Un-mocks the Date class.
19
+ */
20
+ export declare function clear(): void;
21
+
22
+ /**
23
+ * Package version.
24
+ */
25
+ export declare const version: string;
package/lib/index.js CHANGED
@@ -1,33 +1,36 @@
1
- 'use strict';
1
+ "use strict";
2
2
 
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.clear = exports.advanceTo = exports.advanceBy = undefined;
7
-
8
- var _date = require('./date');
9
-
10
- Object.defineProperty(exports, 'advanceBy', {
6
+ Object.defineProperty(exports, "advanceBy", {
11
7
  enumerable: true,
12
8
  get: function get() {
13
9
  return _date.advanceBy;
14
10
  }
15
11
  });
16
- Object.defineProperty(exports, 'advanceTo', {
12
+ Object.defineProperty(exports, "advanceTo", {
17
13
  enumerable: true,
18
14
  get: function get() {
19
15
  return _date.advanceTo;
20
16
  }
21
17
  });
22
- Object.defineProperty(exports, 'clear', {
18
+ Object.defineProperty(exports, "clear", {
23
19
  enumerable: true,
24
20
  get: function get() {
25
21
  return _date.clear;
26
22
  }
27
23
  });
24
+ exports.version = void 0;
28
25
 
29
- var _mockDate = require('./mockDate');
26
+ var _date = require("./date");
30
27
 
28
+ var _mockDate = require("./mockDate");
29
+
30
+ /**
31
+ * Created by hustcc 18/05/27.
32
+ * Contract: i@hust.cc
33
+ */
31
34
  // mock Date class
32
35
  var dateClass = (0, _mockDate.mockDateClass)(Date);
33
36
 
@@ -37,4 +40,7 @@ if (global.window) {
37
40
  } else {
38
41
  // node / native env
39
42
  global.Date = dateClass;
40
- }
43
+ }
44
+
45
+ var version = "1.0.8";
46
+ exports.version = version;
package/lib/mockDate.js CHANGED
@@ -1,44 +1,60 @@
1
- 'use strict';
1
+ "use strict";
2
2
 
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.mockDateClass = undefined;
6
+ exports.mockDateClass = void 0;
7
7
 
8
- var _date = require('./date');
8
+ var _date = require("./date");
9
9
 
10
- var mockDateClass = exports.mockDateClass = function mockDateClass(D) {
10
+ function isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
11
+
12
+ function _construct(Parent, args, Class) { if (isNativeReflectConstruct()) { _construct = Reflect.construct; } else { _construct = function _construct(Parent, args, Class) { var a = [null]; a.push.apply(a, args); var Constructor = Function.bind.apply(Parent, a); var instance = new Constructor(); if (Class) _setPrototypeOf(instance, Class.prototype); return instance; }; } return _construct.apply(null, arguments); }
13
+
14
+ function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
15
+
16
+ function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }
17
+
18
+ function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); }
19
+
20
+ function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); }
21
+
22
+ function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } }
23
+
24
+ var mockDateClass = function mockDateClass(D) {
25
+ // if undefined, use real date, or else mock date
11
26
  var mockNow = function mockNow() {
12
27
  return (0, _date.now)() === undefined ? D.now() : (0, _date.now)();
13
28
  };
14
29
 
15
- function MD() {
16
- for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
30
+ function Date() {
31
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
17
32
  args[_key] = arguments[_key];
18
33
  }
19
34
 
20
35
  var dateArgs = args.length === 0 ? [mockNow()] : args;
21
- var instance = new (Function.prototype.bind.apply(D, [null].concat(dateArgs)))();
36
+
37
+ var instance = _construct(D, _toConsumableArray(dateArgs));
38
+
22
39
  Object.setPrototypeOf(instance, Object.getPrototypeOf(this));
23
40
  return instance;
24
41
  }
25
42
 
26
- MD.prototype = Object.create(D.prototype);
27
- Object.setPrototypeOf(MD, D);
43
+ Date.prototype = Object.create(D.prototype);
44
+ Object.setPrototypeOf(Date, D); // undefined means do not mock date
28
45
 
29
- // undefined means do not mock date
30
- MD.now = function () {
46
+ Date.now = function () {
31
47
  return mockNow();
32
- };
33
- // original Date class
34
- MD.__OriginalDate__ = D;
35
- // current() is for test.
36
- MD.current = function () {
48
+ }; // original Date class
49
+
50
+
51
+ Date.__OriginalDate__ = D; // current() is for test.
52
+
53
+ Date.current = function () {
37
54
  return D.now();
38
55
  };
39
56
 
40
- return MD;
41
- }; /**
42
- * Created by hustcc 18/05/27.
43
- * Contract: i@hust.cc
44
- */
57
+ return Date;
58
+ };
59
+
60
+ exports.mockDateClass = mockDateClass;
package/package.json CHANGED
@@ -1,11 +1,12 @@
1
1
  {
2
2
  "name": "jest-date-mock",
3
- "version": "1.0.5-beta.1",
3
+ "version": "1.0.8",
4
4
  "description": "Mock `window.Date` when run unit test cases with jest. Make tests of `Date` easier.",
5
5
  "main": "lib/index.js",
6
+ "typings": "lib/index.d.ts",
6
7
  "scripts": {
7
- "test": "jest",
8
- "build": "babel src --out-dir lib && npm run test",
8
+ "test": "jest --no-cache",
9
+ "build": "babel src --out-dir lib --copy-files && npm run test",
9
10
  "coveralls": "cat ./coverage/lcov.info | coveralls"
10
11
  },
11
12
  "repository": {
@@ -21,17 +22,19 @@
21
22
  "timestamp"
22
23
  ],
23
24
  "author": "hustcc",
24
- "license": "ISC",
25
+ "license": "MIT",
25
26
  "bugs": {
26
27
  "url": "https://github.com/hustcc/jest-date-mock/issues"
27
28
  },
28
29
  "homepage": "https://github.com/hustcc/jest-date-mock#readme",
29
30
  "devDependencies": {
30
- "babel-cli": "^6.26.0",
31
- "babel-jest": "^22.0.4",
32
- "babel-preset-env": "^1.6.1",
31
+ "@babel/cli": "^7.7.7",
32
+ "@babel/core": "^7.7.7",
33
+ "@babel/preset-env": "^7.7.7",
34
+ "babel-jest": "^24.9.0",
35
+ "babel-plugin-version": "^0.2.2",
33
36
  "coveralls": "^3.0.0",
34
- "jest": "^21.2.1"
37
+ "jest": "^24.9.0"
35
38
  },
36
39
  "jest": {
37
40
  "collectCoverage": true,