@supertape/operator-stub 1.7.0 → 1.10.0

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/README.md CHANGED
@@ -1,11 +1,9 @@
1
- # @supertape/operator-stub [![NPM version][NPMIMGURL]][NPMURL] [![Dependency Status][DependencyStatusIMGURL]][DependencyStatusURL] [![Build Status][BuildStatusIMGURL]][BuildStatusURL] [![Coverage Status][CoverageIMGURL]][CoverageURL]
1
+ # @supertape/operator-stub [![NPM version][NPMIMGURL]][NPMURL] [![Build Status][BuildStatusIMGURL]][BuildStatusURL] [![Coverage Status][CoverageIMGURL]][CoverageURL]
2
2
 
3
3
  [NPMIMGURL]: https://img.shields.io/npm/v/supertape.svg?style=flat&longCache=true
4
4
  [BuildStatusIMGURL]: https://img.shields.io/travis/coderaiser/supertape/master.svg?style=flat&longCache=true
5
- [DependencyStatusIMGURL]: https://img.shields.io/david/coderaiser/supertape.svg?path=packages/operator-stub&style=flat&longCache=true
6
5
  [NPMURL]: https://npmjs.org/package/supertape "npm"
7
6
  [BuildStatusURL]: https://travis-ci.org/coderaiser/supertape "Build Status"
8
- [DependencyStatusURL]: https://david-dm.org/coderaiser/supertape?path=packages/operator-stub "Dependency Status"
9
7
  [CoverageURL]: https://coveralls.io/github/coderaiser/supertape?branch=master
10
8
  [CoverageIMGURL]: https://coveralls.io/repos/coderaiser/supertape/badge.svg?branch=master&service=github
11
9
 
@@ -125,6 +123,72 @@ test('function called with new', (t) => {
125
123
  });
126
124
  ```
127
125
 
126
+ ### t.calledBefore(fn1, fn2[, message])
127
+
128
+ Check that `fn1` called before `fn2`.
129
+ Do not forget to set names of stubs.
130
+
131
+ ```js
132
+ import test, {
133
+ stub,
134
+ } from 'supertape';
135
+
136
+ test('function called with new', (t) => {
137
+ const init = stub().withName('init');
138
+ const show = stub().withName('show');
139
+
140
+ init();
141
+ show();
142
+
143
+ t.calledBefore(show, init);
144
+ t.end();
145
+ });
146
+ ```
147
+
148
+ ### t.calledAfter(fn1, fn2[, message])
149
+
150
+ Check that `fn1` called after `fn2`.
151
+ Do not forget to set names of stubs.
152
+
153
+ ```js
154
+ import test, {
155
+ stub,
156
+ } from 'supertape';
157
+
158
+ test('function called with new', (t) => {
159
+ const init = stub().withName('init');
160
+ const show = stub().withName('show');
161
+
162
+ init();
163
+ show();
164
+
165
+ t.calledAfter(init, show);
166
+ t.end();
167
+ });
168
+ ```
169
+
170
+ ### t.calledInOrder([fn1, fn2, fnN][, message])
171
+
172
+ Check that array of stubs `fns` called in order;
173
+ Do not forget to set names of stubs.
174
+
175
+ ```js
176
+ import test, {
177
+ stub,
178
+ } from 'supertape';
179
+
180
+ test('function called with new', (t) => {
181
+ const init = stub().withName('init');
182
+ const show = stub().withName('show');
183
+
184
+ init();
185
+ show();
186
+
187
+ t.calledInOrder([init, show]);
188
+ t.end();
189
+ });
190
+ ```
191
+
128
192
  ## License
129
193
 
130
194
  MIT
package/lib/stub.d.ts CHANGED
@@ -2,10 +2,10 @@ import {Stub} from '@cloudcmd/stub';
2
2
 
3
3
  type Result = {
4
4
  is: boolean,
5
- expected: any,
6
- actual: any,
5
+ expected: unknown,
6
+ actual: unknown,
7
7
  message: string,
8
- }
8
+ };
9
9
  export function stub(arg?: unknown): Stub;
10
10
 
11
11
  export interface OperatorStub {
@@ -17,5 +17,8 @@ export interface OperatorStub {
17
17
  calledOnce: (fn: Stub, message?: string) => Result;
18
18
  calledTwice: (fn: Stub, message?: string) => Result;
19
19
  calledWithNew: (fn: Stub, message?: string) => Result;
20
+ calledBefore: (fn1: Stub, fn2: Stub, message?: string) => Result;
21
+ calledAfter: (fn1: Stub, fn2: Stub, message?: string) => Result;
22
+ calledInOrder: (fns: Stub[], message?: string) => Result;
20
23
  }
21
24
 
package/lib/stub.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import {isStub} from '@cloudcmd/stub';
2
2
 
3
3
  const isString = (a) => typeof a === 'string';
4
- const getExpectedStubMessage = (fn) => `Expected stub, but received: ${fn.toString()}`;
4
+ const getExpectedStubMessage = (fn) => `Expected stub, but received: ${fn?.toString()}`;
5
5
 
6
6
  const {stringify} = JSON;
7
7
  const {isArray} = Array;
@@ -17,10 +17,13 @@ export const notCalled = (operator) => (fn, message = 'should not be called') =>
17
17
  if (!isStub(fn))
18
18
  return operator.fail(getExpectedStubMessage(fn));
19
19
 
20
+ if (!isString(message))
21
+ return operator.fail(`'t.notCalled' expects message to be string, but received: '${stringify(message)}', looks like you need 't.notCalledWith'`);
22
+
20
23
  return operator.notOk(fn.called, message);
21
24
  };
22
25
 
23
- export const calledWith = (operator) => (fn, args, message = 'should be called with arguments') => {
26
+ export const calledWith = (operator) => (fn, args, message = `should be called with 'args'`) => {
24
27
  if (!isStub(fn))
25
28
  return operator.fail(getExpectedStubMessage(fn));
26
29
 
@@ -28,10 +31,10 @@ export const calledWith = (operator) => (fn, args, message = 'should be called w
28
31
  return operator.fail(`Expected function to be called with arguments, but not called at all`);
29
32
 
30
33
  if (!args)
31
- return operator.fail(`You haven't provided 'arguments', looks like you need 't.calledWithNoArgs()'`);
34
+ return operator.fail(`You haven't provided 'args', looks like you need 't.calledWithNoArgs()'`);
32
35
 
33
36
  if (!isArray(args))
34
- return operator.fail(`Expected 'arguments' to be 'array' but received: ${stringify(args)}`);
37
+ return operator.fail(`Expected 'args' to be 'array' but received: ${stringify(args)}`);
35
38
 
36
39
  const {length} = fn.args;
37
40
  const calledArgs = fn.args[length - 1];
@@ -62,13 +65,9 @@ export const calledCount = (operator) => (fn, count, message = 'should be called
62
65
  return operator.equal(fn.callCount, count, message);
63
66
  };
64
67
 
65
- export const calledOnce = (operator) => (fn, message = 'should be called once') => {
66
- return calledCount(operator)(fn, 1, message);
67
- };
68
+ export const calledOnce = (operator) => (fn, message = 'should be called once') => calledCount(operator)(fn, 1, message);
68
69
 
69
- export const calledTwice = (operator) => (fn, count, message = 'should be called twice') => {
70
- return calledCount(operator)(fn, 2, message);
71
- };
70
+ export const calledTwice = (operator) => (fn, count, message = 'should be called twice') => calledCount(operator)(fn, 2, message);
72
71
 
73
72
  export const calledWithNew = (operator) => (fn, message = 'should be called with new') => {
74
73
  if (!isStub(fn))
@@ -77,3 +76,76 @@ export const calledWithNew = (operator) => (fn, message = 'should be called with
77
76
  return operator.ok(fn.calledWithNew(), message);
78
77
  };
79
78
 
79
+ const noName = ({name}) => name === 'anonymous';
80
+
81
+ export const calledBefore = (operator) => (fn1, fn2, message) => {
82
+ message = message || `should call '${fn1.name}' before '${fn2.name}'`;
83
+
84
+ if (!isStub(fn1))
85
+ return operator.fail(getExpectedStubMessage(fn1));
86
+
87
+ if (!isStub(fn2))
88
+ return operator.fail(getExpectedStubMessage(fn2));
89
+
90
+ if (noName(fn1) || noName(fn2))
91
+ return operator.fail(`Looks like you forget to define name of a stub, use: stub().withName('functionName')`);
92
+
93
+ if (!fn1.called)
94
+ return operator.fail(`Expected function '${fn1.name}' to be called before '${fn2.name}' but '${fn1.name}' not called at all`);
95
+
96
+ if (!fn2.called)
97
+ return operator.fail(`Expected function '${fn1.name}' to be called before '${fn2.name}' but '${fn2.name}' not called at all`);
98
+
99
+ return operator.ok(fn1.calledBefore(fn2), message);
100
+ };
101
+
102
+ export const calledAfter = (operator) => (fn1, fn2, message) => {
103
+ message = message || `should call '${fn1.name}' after '${fn2.name}'`;
104
+
105
+ if (!isStub(fn1))
106
+ return operator.fail(getExpectedStubMessage(fn1));
107
+
108
+ if (!isStub(fn2))
109
+ return operator.fail(getExpectedStubMessage(fn2));
110
+
111
+ if (noName(fn1) || noName(fn2))
112
+ return operator.fail(`Looks like you forget to define name of a stub, use: stub().withName('functionName')`);
113
+
114
+ if (!fn1.called)
115
+ return operator.fail(`Expected function '${fn1.name}' to be called after '${fn2.name}' but '${fn1.name}' not called at all`);
116
+
117
+ if (!fn2.called)
118
+ return operator.fail(`Expected function '${fn1.name}' to be called after '${fn2.name}' but '${fn2.name}' not called at all`);
119
+
120
+ return operator.ok(fn1.calledAfter(fn2), message);
121
+ };
122
+
123
+ export const calledInOrder = (operator) => (fns, message = 'should call in order') => {
124
+ if (!isArray(fns))
125
+ return operator.fail(`Expected 'fns' to be 'array' but received: ${fns}`);
126
+
127
+ let failMessage = '';
128
+ const fail = (message) => failMessage = message;
129
+ const ok = (result, message) => {
130
+ if (!result)
131
+ failMessage = message;
132
+ };
133
+
134
+ const check = calledBefore({
135
+ fail,
136
+ ok,
137
+ });
138
+
139
+ for (let i = 0; i < fns.length - 1; i++) {
140
+ const fn1 = fns[i];
141
+ const fn2 = fns[i + 1];
142
+
143
+ check(fn1, fn2);
144
+
145
+ if (failMessage)
146
+ return operator.fail(failMessage);
147
+ }
148
+
149
+ return operator.pass(message);
150
+ };
151
+
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@supertape/operator-stub",
3
- "version": "1.7.0",
3
+ "version": "1.10.0",
4
4
  "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
5
5
  "description": "supertape stub operator",
6
6
  "homepage": "http://github.com/coderaiser/supertape",
@@ -26,7 +26,7 @@
26
26
  "wisdom": "madrun wisdom"
27
27
  },
28
28
  "dependencies": {
29
- "@cloudcmd/stub": "^3.6.0"
29
+ "@cloudcmd/stub": "^3.7.0"
30
30
  },
31
31
  "keywords": [
32
32
  "operator",
@@ -38,17 +38,17 @@
38
38
  ],
39
39
  "devDependencies": {
40
40
  "@babel/core": "^7.12.9",
41
- "@babel/eslint-parser": "^7.12.1",
42
41
  "c8": "^7.3.5",
43
- "check-dts": "^0.5.5",
42
+ "check-dts": "^0.6.5",
44
43
  "eslint": "^8.0.0-beta.0",
45
44
  "eslint-plugin-node": "^11.1.0",
46
- "eslint-plugin-putout": "^10.0.1",
45
+ "eslint-plugin-putout": "^13.0.1",
47
46
  "madrun": "^8.0.0",
48
47
  "mock-require": "^3.0.2",
49
48
  "nodemon": "^2.0.2",
50
- "putout": "^20.0.1",
51
- "supertape": "^6.0.1"
49
+ "putout": "^24.1.0",
50
+ "supertape": "^6.0.1",
51
+ "typescript": "^4.4.4"
52
52
  },
53
53
  "license": "MIT",
54
54
  "engines": {