@robert.tools/testing 1.0.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/LICENSE +21 -0
- package/README.md +45 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +32 -0
- package/dist/index.js.map +1 -0
- package/dist/utils/utils.d.ts +8 -0
- package/dist/utils/utils.js +37 -0
- package/dist/utils/utils.js.map +1 -0
- package/package.json +70 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Robert Willemelis
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# @robert.tools/testing
|
|
2
|
+
|
|
3
|
+
A set of helper for testing
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @robert.tools/testing
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## 📜 Usage
|
|
12
|
+
|
|
13
|
+
### 🟢 Installation
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm install @robert.tools/testing
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
### 📝 Sample usage
|
|
20
|
+
|
|
21
|
+
```typescript
|
|
22
|
+
import { spyOnCommand } from '@robert.tools/testing';
|
|
23
|
+
|
|
24
|
+
const spy = spyOnCommand('hello');
|
|
25
|
+
command('curl someurl') // 'result: hello'
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## 🗃️ commands
|
|
29
|
+
|
|
30
|
+
After an npm install with `npm i` the following commands are available:
|
|
31
|
+
|
|
32
|
+
* spy on command and return result: `spyOnCommand(result)`
|
|
33
|
+
* spy on URLs and return result: `spyOnURLs(results)`
|
|
34
|
+
|
|
35
|
+
## ⚖️ Notes
|
|
36
|
+
|
|
37
|
+
This software is hand-crafted, test-driven and assisted by AI tools. I know each
|
|
38
|
+
line of my code. ✌️
|
|
39
|
+
|
|
40
|
+
| Tool | Comment |
|
|
41
|
+
| --- | --- |
|
|
42
|
+
|  | Test-driven development with Jest |
|
|
43
|
+
|  | Part of the robert.tools ecosystem |
|
|
44
|
+
|  | Code completion |
|
|
45
|
+
|  | chatGPT research |
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { URL_ITEMS } from './index.d';
|
|
2
|
+
/**
|
|
3
|
+
* 🎯 Creates a spy on the `cmd.command` function and mocks its implementation to return the specified result.
|
|
4
|
+
* @param result ➡️ The mock value to return. (optional)
|
|
5
|
+
* @returns {jest.SpyInstance} 📤 The spy instance.
|
|
6
|
+
*/
|
|
7
|
+
export declare const spyOnCommand: (result?: string) => jest.SpyInstance<string, [command: string, doLog?: boolean | undefined, showError?: boolean | undefined], any>;
|
|
8
|
+
/**
|
|
9
|
+
* 🎯 Creates a spy to mock the `cmd.command` function for specific URL requests.
|
|
10
|
+
* @param {URL_ITEMS} results ➡️ The URL items to mock.
|
|
11
|
+
* @returns {jest.SpyInstance} 📤 The spy instance.
|
|
12
|
+
*/
|
|
13
|
+
export declare const spyOnURLs: (results: URL_ITEMS) => jest.SpyInstance<string, [command: string, doLog?: boolean | undefined, showError?: boolean | undefined], any>;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.spyOnURLs = exports.spyOnCommand = void 0;
|
|
4
|
+
// 📦 external dependencies
|
|
5
|
+
const cmd = require("@robert.tools/cmd");
|
|
6
|
+
// 📦 internal dependencies
|
|
7
|
+
const utils_1 = require("./utils/utils");
|
|
8
|
+
/**
|
|
9
|
+
* 🎯 Creates a spy on the `cmd.command` function and mocks its implementation to return the specified result.
|
|
10
|
+
* @param result ➡️ The mock value to return. (optional)
|
|
11
|
+
* @returns {jest.SpyInstance} 📤 The spy instance.
|
|
12
|
+
*/
|
|
13
|
+
const spyOnCommand = (result = '') => {
|
|
14
|
+
return jest.spyOn(cmd, 'command').mockImplementation(() => {
|
|
15
|
+
return result;
|
|
16
|
+
});
|
|
17
|
+
};
|
|
18
|
+
exports.spyOnCommand = spyOnCommand;
|
|
19
|
+
/**
|
|
20
|
+
* 🎯 Creates a spy to mock the `cmd.command` function for specific URL requests.
|
|
21
|
+
* @param {URL_ITEMS} results ➡️ The URL items to mock.
|
|
22
|
+
* @returns {jest.SpyInstance} 📤 The spy instance.
|
|
23
|
+
*/
|
|
24
|
+
const spyOnURLs = (results) => {
|
|
25
|
+
return jest
|
|
26
|
+
.spyOn(cmd, 'command')
|
|
27
|
+
.mockImplementation((request) => {
|
|
28
|
+
return (0, utils_1.mockRequest)(request, results);
|
|
29
|
+
});
|
|
30
|
+
};
|
|
31
|
+
exports.spyOnURLs = spyOnURLs;
|
|
32
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,2BAA2B;AAC3B,yCAAyC;AAEzC,2BAA2B;AAC3B,yCAA4C;AAK5C;;;;GAIG;AACI,MAAM,YAAY,GAAG,CAAC,SAAiB,EAAE,EAAE,EAAE;IAChD,OAAO,IAAI,CAAC,KAAK,CAAC,GAAG,EAAE,SAAS,CAAC,CAAC,kBAAkB,CAAC,GAAW,EAAE;QAC9D,OAAO,MAAM,CAAC;IAClB,CAAC,CAAC,CAAC;AACP,CAAC,CAAC;AAJW,QAAA,YAAY,gBAIvB;AAEF;;;;GAIG;AACI,MAAM,SAAS,GAAG,CAAC,OAAkB,EAAE,EAAE;IAC5C,OAAO,IAAI;SACN,KAAK,CAAC,GAAG,EAAE,SAAS,CAAC;SACrB,kBAAkB,CAAC,CAAC,OAAe,EAAU,EAAE;QAC5C,OAAO,IAAA,mBAAW,EAAC,OAAO,EAAE,OAAO,CAAC,CAAC;IACzC,CAAC,CAAC,CAAC;AACX,CAAC,CAAC;AANW,QAAA,SAAS,aAMpB"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { RAW, URL_ITEMS } from '../index.d';
|
|
2
|
+
/**
|
|
3
|
+
* 🎯 Mocks a request based on the provided mocked results.
|
|
4
|
+
* @param {string} request ➡️ The request string, typically a curl command.
|
|
5
|
+
* @param {URL_ITEMS} results ➡️ object containing mocked results.
|
|
6
|
+
* @returns {RAW} 📤 mocked result for the given request.
|
|
7
|
+
*/
|
|
8
|
+
export declare const mockRequest: (request: string, results: URL_ITEMS) => RAW;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.mockRequest = void 0;
|
|
4
|
+
// 📦 external dependencies
|
|
5
|
+
const uri_1 = require("@robert.tools/uri");
|
|
6
|
+
const curl_1 = require("@robert.tools/curl");
|
|
7
|
+
const log_1 = require("@robert.tools/log");
|
|
8
|
+
const utils_1 = require("@robert.tools/utils");
|
|
9
|
+
/**
|
|
10
|
+
* 🎯 Mocks a request based on the provided mocked results.
|
|
11
|
+
* @param {string} request ➡️ The request string, typically a curl command.
|
|
12
|
+
* @param {URL_ITEMS} results ➡️ object containing mocked results.
|
|
13
|
+
* @returns {RAW} 📤 mocked result for the given request.
|
|
14
|
+
*/
|
|
15
|
+
const mockRequest = (request, results) => {
|
|
16
|
+
const forwards = results?.forwards || {};
|
|
17
|
+
const urlID = (0, uri_1.getUrlID)(request);
|
|
18
|
+
const domainID = (0, uri_1.getHostname)(urlID);
|
|
19
|
+
const orders = results?.orders || {};
|
|
20
|
+
const order = orders?.[domainID] || [];
|
|
21
|
+
const data = (0, curl_1.getCurlData)(request);
|
|
22
|
+
let ID = urlID;
|
|
23
|
+
if (data.data?.forwarding) {
|
|
24
|
+
const lastOrder = order[order.length - 1];
|
|
25
|
+
if (lastOrder && forwards[lastOrder]) {
|
|
26
|
+
ID = lastOrder;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
const result = forwards?.[ID];
|
|
30
|
+
if (!result) {
|
|
31
|
+
log_1.LOG.FAIL(`No mock result for URL ID: ${ID}`);
|
|
32
|
+
return forwards?.['fallback'] || '<invalid>';
|
|
33
|
+
}
|
|
34
|
+
return (0, utils_1.toType)(result);
|
|
35
|
+
};
|
|
36
|
+
exports.mockRequest = mockRequest;
|
|
37
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/utils/utils.ts"],"names":[],"mappings":";;;AAAA,2BAA2B;AAC3B,2CAA0D;AAC1D,6CAAiD;AACjD,2CAAwC;AACxC,+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"./dist/index.js",
|
|
6
|
+
"types": "./dist/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./dist/index.js"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"dist"
|
|
12
|
+
],
|
|
13
|
+
"scripts": {
|
|
14
|
+
"init": "robi init",
|
|
15
|
+
"release": "robi release",
|
|
16
|
+
"status": "robi status",
|
|
17
|
+
"version": "robi version",
|
|
18
|
+
"verbose": "robi verbose",
|
|
19
|
+
"pub": "npm whoami && npm publish --access public",
|
|
20
|
+
"build": "rm -rf dist && tsc -p tsconfig.build.json",
|
|
21
|
+
"test": "jest --coverage --runInBand --verbose",
|
|
22
|
+
"lint": "eslint .",
|
|
23
|
+
"check": "npm run lint && npm test && npm run build",
|
|
24
|
+
"prepublishOnly": "npm run check"
|
|
25
|
+
},
|
|
26
|
+
"repository": {
|
|
27
|
+
"type": "git",
|
|
28
|
+
"url": "git+https://github.com/robert-tools/testing.git"
|
|
29
|
+
},
|
|
30
|
+
"bugs": {
|
|
31
|
+
"url": "https://github.com/robert-tools/testing/issues"
|
|
32
|
+
},
|
|
33
|
+
"homepage": "https://github.com/robert-tools/testing#readme",
|
|
34
|
+
"keywords": [
|
|
35
|
+
"typescript",
|
|
36
|
+
"node",
|
|
37
|
+
"testing",
|
|
38
|
+
"robert.tools"
|
|
39
|
+
],
|
|
40
|
+
"author": "Robert Willemelis",
|
|
41
|
+
"license": "MIT",
|
|
42
|
+
"engines": {
|
|
43
|
+
"node": ">=22"
|
|
44
|
+
},
|
|
45
|
+
"publishConfig": {
|
|
46
|
+
"access": "public",
|
|
47
|
+
"registry": "https://registry.npmjs.org/"
|
|
48
|
+
},
|
|
49
|
+
"devDependencies": {
|
|
50
|
+
"@robert.tools/setup": "^1.2.0",
|
|
51
|
+
"@types/jest": "^29.5.14",
|
|
52
|
+
"@types/node": "^24.0.0",
|
|
53
|
+
"@typescript-eslint/eslint-plugin": "^8.0.0",
|
|
54
|
+
"@typescript-eslint/parser": "^8.0.0",
|
|
55
|
+
"eslint": "^9.0.0",
|
|
56
|
+
"eslint-config-prettier": "^10.1.8",
|
|
57
|
+
"eslint-plugin-prettier": "^5.5.6",
|
|
58
|
+
"jest": "^29.7.0",
|
|
59
|
+
"jest-environment-jsdom": "^29.6.2",
|
|
60
|
+
"ts-jest": "^29.4.0",
|
|
61
|
+
"typescript": "^5.9.0"
|
|
62
|
+
},
|
|
63
|
+
"dependencies": {
|
|
64
|
+
"@robert.tools/cmd": "^1.0.0",
|
|
65
|
+
"@robert.tools/curl": "^1.0.1",
|
|
66
|
+
"@robert.tools/log": "^1.0.0",
|
|
67
|
+
"@robert.tools/uri": "^1.0.0",
|
|
68
|
+
"@robert.tools/utils": "^1.2.0"
|
|
69
|
+
}
|
|
70
|
+
}
|