hamjest 4.0.1 → 4.1.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.
package/index.js CHANGED
@@ -61,11 +61,13 @@ module.exports.closeTo = require('./lib/matchers/IsCloseTo').closeTo;
61
61
  module.exports.allOf = require('./lib/matchers/AllOf').allOf;
62
62
  module.exports.anyOf = require('./lib/matchers/AnyOf').anyOf;
63
63
  module.exports.everyItem = require('./lib/matchers/Every').everyItem;
64
+ module.exports.startsWithItems = require('./lib/matchers/IsArrayStartingWith').startsWithItems;
65
+ module.exports.endsWithItems = require('./lib/matchers/IsArrayEndingWith').endsWithItems;
66
+ module.exports.contains = require('./lib/matchers/IsArrayContaining').contains;
67
+ module.exports.containsInAnyOrder = require('./lib/matchers/IsArrayContainingInAnyOrder').containsInAnyOrder;
64
68
  module.exports.hasItem = require('./lib/matchers/IsArrayWithItem').hasItem;
65
69
  module.exports.hasItems = require('./lib/matchers/IsArrayWithItems').hasItems;
66
70
  module.exports.hasExactlyOneItem = require('./lib/matchers/hasExactlyOneItem');
67
- module.exports.contains = require('./lib/matchers/IsArrayContaining').contains;
68
- module.exports.containsInAnyOrder = require('./lib/matchers/IsArrayContainingInAnyOrder').containsInAnyOrder;
69
71
  module.exports.orderedBy = require('./lib/matchers/IsArrayOrderedBy').orderedBy;
70
72
  module.exports.hasSize = require('./lib/matchers/hasSize');
71
73
  module.exports.isEmpty = require('./lib/matchers/isEmpty');
@@ -103,7 +103,11 @@ function Description() {
103
103
  this.useJsonForObjects = oldJsonFlag;
104
104
  }
105
105
  } else {
106
- this.append(value);
106
+ try {
107
+ this.append(value);
108
+ } catch (e) {
109
+ this.append('<' + e.stack + '>');
110
+ }
107
111
  }
108
112
  return this;
109
113
  },
package/lib/assertThat.js CHANGED
@@ -41,7 +41,7 @@ const assertThat = (...args) => {
41
41
  } else {
42
42
  if (global && global.expect) {
43
43
  const expectation = global.expect();
44
- if (expectation && expectation.nothing) {
44
+ if (expectation && 'nothing' in expectation) {
45
45
  expectation.nothing();
46
46
  }
47
47
  }
@@ -0,0 +1,70 @@
1
+ 'use strict';
2
+
3
+ const _create = require('lodash/create');
4
+ const _every = require('lodash/every');
5
+ const _map = require('lodash/map');
6
+ const IsArray = require('./IsArray');
7
+ const asMatcher = require('../utils/asMatcher');
8
+ const promiseAgnostic = require('./promiseAgnostic');
9
+
10
+ const IsArrayEndingWith = function IsArrayEndingWith(itemsOrMatchers) {
11
+ const matchers = _map(itemsOrMatchers, asMatcher);
12
+ return _create(new IsArray(), {
13
+ matchesSafely: function (actual) {
14
+ if (actual.length < matchers.length) {
15
+ return false;
16
+ }
17
+
18
+ const results = _map(matchers, (matcher, index) => {
19
+ return matcher.matches(actual[actual.length - matchers.length + index]);
20
+ });
21
+
22
+ return promiseAgnostic.matchesAggregate(results, _every);
23
+ },
24
+ describeTo: function (description) {
25
+ description.appendList('[…, ', ', ', ']', matchers);
26
+ },
27
+ describeMismatchSafely: function (actual, description) {
28
+ const results = _map(matchers, (matcher, index) => {
29
+ const actualIndex = actual.length - matchers.length + index;
30
+ if (actualIndex >= 0) {
31
+ return matcher.matches(actual[actualIndex]);
32
+ }
33
+ });
34
+
35
+ let first = true;
36
+ return promiseAgnostic.describeMismatchAggregate(results, (result, index) => {
37
+ const actualIndex = actual.length - matchers.length + index;
38
+ if (result || actualIndex < 0) {
39
+ return;
40
+ }
41
+
42
+ if (!first) {
43
+ description.append('\n');
44
+ }
45
+ first = false;
46
+
47
+ description
48
+ .append('item ')
49
+ .append(actualIndex)
50
+ .append(': ');
51
+ return matchers[index].describeMismatch(actual[actualIndex], description);
52
+ }, () => {
53
+ if (!first) {
54
+ description.append('\n');
55
+ }
56
+
57
+ if (actual.length < matchers.length) {
58
+ description.indented(() => description.appendList('missing:\n', ',\n', '', matchers.slice(0, matchers.length - actual.length)));
59
+ }
60
+ });
61
+ }
62
+ });
63
+ };
64
+
65
+ IsArrayEndingWith.endsWithItems = function () {
66
+ return new IsArrayEndingWith(arguments);
67
+ };
68
+
69
+ module.exports = IsArrayEndingWith;
70
+
@@ -0,0 +1,67 @@
1
+ 'use strict';
2
+
3
+ const _create = require('lodash/create');
4
+ const _every = require('lodash/every');
5
+ const _map = require('lodash/map');
6
+ const IsArray = require('./IsArray');
7
+ const asMatcher = require('../utils/asMatcher');
8
+ const promiseAgnostic = require('./promiseAgnostic');
9
+
10
+ const IsArrayStartingWith = function IsArrayStartingWith(itemsOrMatchers) {
11
+ const matchers = _map(itemsOrMatchers, asMatcher);
12
+ return _create(new IsArray(), {
13
+ matchesSafely: function (actual) {
14
+ if (actual.length < matchers.length) {
15
+ return false;
16
+ }
17
+
18
+ const results = _map(matchers, (matcher, index) => {
19
+ return matcher.matches(actual[index]);
20
+ });
21
+
22
+ return promiseAgnostic.matchesAggregate(results, _every);
23
+ },
24
+ describeTo: function (description) {
25
+ description.appendList('[', ', ', ', …]', matchers);
26
+ },
27
+ describeMismatchSafely: function (actual, description) {
28
+ const results = _map(matchers, (matcher, index) => {
29
+ if (index < actual.length) {
30
+ return matcher.matches(actual[index]);
31
+ }
32
+ });
33
+
34
+ let first = true;
35
+ return promiseAgnostic.describeMismatchAggregate(results, (result, index) => {
36
+ if (result || index >= actual.length) {
37
+ return;
38
+ }
39
+
40
+ if (!first) {
41
+ description.append('\n');
42
+ }
43
+ first = false;
44
+
45
+ description
46
+ .append('item ')
47
+ .append(index)
48
+ .append(': ');
49
+ return matchers[index].describeMismatch(actual[index], description);
50
+ }, () => {
51
+ if (!first) {
52
+ description.append('\n');
53
+ }
54
+
55
+ if (actual.length < matchers.length) {
56
+ description.indented(() => description.appendList('missing:\n', ',\n', '', matchers.slice(actual.length)));
57
+ }
58
+ });
59
+ }
60
+ });
61
+ };
62
+
63
+ IsArrayStartingWith.startsWithItems = function () {
64
+ return new IsArrayStartingWith(arguments);
65
+ };
66
+
67
+ module.exports = IsArrayStartingWith;
@@ -27,17 +27,14 @@ function IsArrayWithItem(valueOrMatcher) {
27
27
  description.append('was empty');
28
28
  return;
29
29
  }
30
- const results = _map(actual, (value) => {
31
- return matcher.matches(value);
32
- });
33
30
 
34
- return promiseAgnostic.describeMismatchAggregate(results, (__result, index) => {
31
+ return promiseAgnostic.describeMismatchAggregate(actual, (actualItem, index) => {
35
32
  description.append('\n');
36
33
  description
37
34
  .append('item ')
38
35
  .append(index)
39
36
  .append(': ');
40
- return description.indented(() => matcher.describeMismatch(actual[index], description));
37
+ return description.indented(() => matcher.describeMismatch(actualItem, description));
41
38
  });
42
39
  }
43
40
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hamjest",
3
- "version": "4.0.1",
3
+ "version": "4.1.1",
4
4
  "main": "index.js",
5
5
  "description": "A library of composable matchers for defining meaningful and readable assertions in JavaScript.",
6
6
  "homepage": "https://github.com/rluba/hamjest",
@@ -49,6 +49,7 @@
49
49
  "@babel/core": "^7.0.0",
50
50
  "@babel/preset-env": "^7.4.2",
51
51
  "@typescript-eslint/eslint-plugin": "^2.30.0",
52
+ "@types/node": "16.11.6",
52
53
  "@typescript-eslint/parser": "^2.30.0",
53
54
  "babelify": "^10.0.0",
54
55
  "browserify": "^16.3.0",
@@ -114,4 +114,18 @@ describe('assertThat', () => {
114
114
 
115
115
  assert.equal(expectNothingWasCalled, true);
116
116
  });
117
+
118
+ it('should not throw when nothing is not available on expect (fix "Invalid Chai property" in vitest)', () => {
119
+ const chaiFake = {irrelevant: 'property'};
120
+
121
+ global.expect = () => new Proxy(chaiFake, {
122
+ get(target, property) {
123
+ if (!Object.keys(target).includes(property)) {
124
+ throw Error(`Invalid Chai property: ${property}`);
125
+ }
126
+ }
127
+ });
128
+
129
+ __.assertThat(true, __.is(__.equalTo(true)));
130
+ });
117
131
  });