node-test-bootstrap 1.2.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/README.md +96 -0
- package/index.js +56 -0
- package/package.json +22 -0
package/README.md
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
# Node testing bootstrap
|
|
2
|
+
|
|
3
|
+
Node [has a test runner on board](#node-test-runner) whose capabilities depend on the Node version used, esp. "mock.property" requiring at least Node 22.20, and "mock" and other objects have to be imported identically to every test file.
|
|
4
|
+
|
|
5
|
+
The following acts as a tiny middleware to check the running Node version before importing the required Node functionality in single stroke.
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
## Bun
|
|
9
|
+
|
|
10
|
+
Bun implents Node's "assert" [fully](#bun-node-compatibility-assert) and its test runner library [partly](#bun-node-compatibility-test), so the bootstrap here will also accept to be run in Bun, details may depend on exact tests.
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
Dev dependencies like this should be installed explicitly as such to make it easier for humans and tooling to separate it from production critical / less exchangeable things, so do not dismiss the "-D" switch ("-d" for Bun) just by regarding it as old-school.
|
|
16
|
+
|
|
17
|
+
Pick for your preferred package manager:
|
|
18
|
+
|
|
19
|
+
```shell
|
|
20
|
+
npm i -D node-test-bootstrap
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
```shell
|
|
24
|
+
pnpm i -D node-test-bootstrap
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
```shell
|
|
28
|
+
// see note on Bun in README
|
|
29
|
+
bun i -D node-test-bootstrap
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
```shell
|
|
33
|
+
# For Yarn you should double check docs for your and / or
|
|
34
|
+
# current Yarn version, newer versions do not treat `i package_name`
|
|
35
|
+
# as an alias for `add ...` and exclude global installations
|
|
36
|
+
yarn add -D node-test-bootstrap
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
## Usage
|
|
41
|
+
|
|
42
|
+
* Bootstrapping
|
|
43
|
+
```js
|
|
44
|
+
import { assert, mock, suite, test } from 'node-test-bootstrap';
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
* Your tests like
|
|
48
|
+
```js
|
|
49
|
+
import { /* your functions to check */ } from /* your module to check */;
|
|
50
|
+
|
|
51
|
+
suite( /* your detail test */, () => {
|
|
52
|
+
/* your test setup */
|
|
53
|
+
test( /* display name */, /* options */ , () => {
|
|
54
|
+
/* your details */
|
|
55
|
+
assert( /* your check */ )
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
## Details
|
|
62
|
+
|
|
63
|
+
### Node version testing
|
|
64
|
+
|
|
65
|
+
Tests if given Node version is not older than that stated for "engines: node" in [the current library's package.json](package.json) (***not*** *the package.json of the project where the current library is imported to*). Literal [range operators](#npm-semver-ranges) (formally required in package.json) would be stripped off since ">=" is implicit here. Note that unfortunately npm regards "engines" settings [just as "advisory"](#npm-engines-advisory-only) and therefore will not prevent installing into not matching Node versions.
|
|
66
|
+
|
|
67
|
+
### TypeScript complain "Could not find a declaration file for module 'node-test-bootstrap'" (ts(7016))
|
|
68
|
+
|
|
69
|
+
VS Code may mark a [Quick Fix](#vscode-quick-fixes) "Could not find a declaration file for module 'node-test-bootstrap'" (with some non working suggestsions for fixing), while at the same time Type checking and IntelliSense work well. The background here is that the functions to type come from general Node libraries and the type definitions for them are acquired automatically by VS Code - trying to provide them by the way the Quick Fix for ts(7016) knows about would superfluously and massively blow up the size of the current package and its update cycles.
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
## References
|
|
73
|
+
|
|
74
|
+
###### bun-node-compatibility-assert
|
|
75
|
+
* [Bun: Node.js Compatibility node:assert](https://bun.com/docs/runtime/nodejs-compat#nodeassert)
|
|
76
|
+
|
|
77
|
+
###### bun-node-compatibility-test
|
|
78
|
+
* [Bun: Node.js Compatibility node:test](https://bun.com/docs/runtime/nodejs-compat#nodetest)
|
|
79
|
+
|
|
80
|
+
###### node-test-describe-suite
|
|
81
|
+
* [Node.js: Test runner: describe / suite](https://nodejs.org/api/test.html#describe-and-it-aliases)
|
|
82
|
+
|
|
83
|
+
###### node-test-runner
|
|
84
|
+
* [Node.js: Test runner](https://nodejs.org/api/test.html#test-runner)
|
|
85
|
+
|
|
86
|
+
###### npm-engines-advisory-only
|
|
87
|
+
* [npm: package.json: "engines ... advisory only"](https://docs.npmjs.com/cli/v7/configuring-npm/package-json#engines#:~:text=Unless,advisory%20only)
|
|
88
|
+
|
|
89
|
+
###### npm-semver-ranges
|
|
90
|
+
* [npm/node-semver: The semver parser for node: Ranges](https://github.com/npm/node-semver#ranges)
|
|
91
|
+
|
|
92
|
+
###### vscode-quick-fixes
|
|
93
|
+
* [Visual Studio Code: Quick Fixes](https://code.visualstudio.com/docs/typescript/typescript-refactoring#_quick-fixes)
|
|
94
|
+
|
|
95
|
+
###### vscode-automatic-type-acquisition
|
|
96
|
+
* [Visual Studio Code: Typings and Automatic Type Acquisition](https://code.visualstudio.com/docs/nodejs/working-with-javascript#_typings-and-automatic-type-acquisition)
|
package/index.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
3
|
+
import package_data from './package.json' with { type: 'json' };
|
|
4
|
+
import { release as runtimeHost, version as runtimVersion } from 'node:process' ;
|
|
5
|
+
|
|
6
|
+
if( ! runtimeHost || ! [ 'node', 'bun' ].includes( runtimeHost.name ) ) throw Error( 'Must run in Node.js or Bun' );
|
|
7
|
+
if( runtimeHost.sourceUrl && runtimeHost.sourceUrl.includes( '/bun/') ) console.warn( 'Note: Running in Bun instead of Node may be experimental' );
|
|
8
|
+
|
|
9
|
+
/** Exit with error
|
|
10
|
+
* @example _exit_err( 'engines', 'not defined' )
|
|
11
|
+
* @param cat - Category for templating
|
|
12
|
+
* @param detail - Filling for template
|
|
13
|
+
* @type { ( cat: 'engines'|'version', detail?: string ) => void }
|
|
14
|
+
*/
|
|
15
|
+
const _exit_err = function( cat, detail = '' ){
|
|
16
|
+
let msg;
|
|
17
|
+
if( cat === 'engines' ) {
|
|
18
|
+
msg = `"engines" ${detail} in package.json for ${package_data.name} - maybe package is corrupted`;
|
|
19
|
+
}
|
|
20
|
+
if( cat === 'version' ) {
|
|
21
|
+
msg = `Node.js version not appropiate for Node.js Test runner - given: ${_is.join('.')}, required min.: ${_min.join('.')}` ;
|
|
22
|
+
}
|
|
23
|
+
throw Error( msg ) ;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if( ! Object.hasOwn( package_data, 'engines' ) ) _exit_err( 'engines', 'not defined' );
|
|
27
|
+
if( ! Object.hasOwn( package_data.engines, 'node' ) ) _exit_err( 'engines', 'has no "node" entry' );
|
|
28
|
+
if( package_data.engines.node === '' ) _exit_err( 'engines', '"node" has no value' );
|
|
29
|
+
|
|
30
|
+
/** Extracts Semver as array of segments as numbers
|
|
31
|
+
* - Strips off anything that is not a number or a dot = allowing
|
|
32
|
+
* npm version ranges etc. as input
|
|
33
|
+
* @example _to_ver_num_arr( '20.13.0' ) // => [ 20, 13, 0 ]
|
|
34
|
+
* @param ver_string - String containing Semver
|
|
35
|
+
* @returns Array with Semver segments as numbers
|
|
36
|
+
* @type { ( ver_string: string ) => number[] }
|
|
37
|
+
*/
|
|
38
|
+
const _to_ver_num_arr = function( ver_string ){
|
|
39
|
+
const my_arr = ver_string
|
|
40
|
+
.replaceAll( /[^0-9\.]/g, '' )
|
|
41
|
+
.split( '.' )
|
|
42
|
+
.map( value => parseInt( value ) ) ;
|
|
43
|
+
return my_arr;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const _is = _to_ver_num_arr( runtimVersion ) ;
|
|
47
|
+
const _min = _to_ver_num_arr( package_data.engines.node ) ;
|
|
48
|
+
let _approved = undefined ;
|
|
49
|
+
if( ! _approved && _is[ 0 ] < _min[ 0 ] ) _exit_err( 'version' ) ;
|
|
50
|
+
if( ! _approved && _is[ 0 ] > _min[ 0 ] ) _approved = true ;
|
|
51
|
+
if( ! _approved && _is[ 1 ] < _min[ 1 ] ) _exit_err( 'version' ) ;
|
|
52
|
+
if( ! _approved && _is[ 1 ] > _min[ 1 ] ) _approved = true ;
|
|
53
|
+
if( ! _approved && _is[ 2 ] < _min[ 2 ] ) _exit_err( 'version' ) ;
|
|
54
|
+
|
|
55
|
+
export { strict as assert } from 'node:assert';
|
|
56
|
+
export { mock, suite, test } from 'node:test';
|
package/package.json
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "node-test-bootstrap",
|
|
3
|
+
"description": "Bootstrapping for Node test runner files",
|
|
4
|
+
"keywords": [],
|
|
5
|
+
"author": {
|
|
6
|
+
"name": "hh lohmann",
|
|
7
|
+
"email": "hh.lohmann@gmail.com"
|
|
8
|
+
},
|
|
9
|
+
"version": "1.2.1",
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git+https://github.com/hh-lohmann/node-test-bootstrap.git"
|
|
14
|
+
},
|
|
15
|
+
"bugs": "https://github.com/hh-lohmann/node-test-bootstrap/issues",
|
|
16
|
+
"homepage": "https://hh-lohmann.github.io/node-test-bootstrap",
|
|
17
|
+
"engines": { "node": ">=22.20.0" },
|
|
18
|
+
"type": "module",
|
|
19
|
+
"exports": {
|
|
20
|
+
"default": "./index.js"
|
|
21
|
+
}
|
|
22
|
+
}
|