vitest-gwt 0.0.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/LICENSE +21 -0
- package/README.md +132 -0
- package/lib/index.d.ts +5 -0
- package/lib/index.js +11 -0
- package/lib/index.js.map +1 -0
- package/lib/withAspect.d.ts +4 -0
- package/lib/withAspect.js +34 -0
- package/lib/withAspect.js.map +1 -0
- package/package.json +84 -0
- package/src/index.ts +12 -0
- package/src/withAspect.ts +31 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020 Eric Siebeneich
|
|
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,132 @@
|
|
|
1
|
+
# vitest-gwt
|
|
2
|
+
A small library to help Vitest support given-when-then style testing without a
|
|
3
|
+
bunch of overhead
|
|
4
|
+
|
|
5
|
+
## Compatibility Chart
|
|
6
|
+
|
|
7
|
+
vitest-gwt stays in lockstep with vitest's **MAJOR VERSION**. If you're using
|
|
8
|
+
vitest@2.x.x, use vitest-gwt@2.x.x
|
|
9
|
+
|
|
10
|
+
## Usage
|
|
11
|
+
|
|
12
|
+
1. Install the package
|
|
13
|
+
```bash
|
|
14
|
+
npm i --save-dev vitest-gwt
|
|
15
|
+
```
|
|
16
|
+
2. In your test files, import `test`
|
|
17
|
+
```js
|
|
18
|
+
import test from 'vitest-gwt';
|
|
19
|
+
```
|
|
20
|
+
3. Write a test!
|
|
21
|
+
```js
|
|
22
|
+
describe('test context', () => {
|
|
23
|
+
test('has no expected errors', {
|
|
24
|
+
given: {
|
|
25
|
+
mock_vitest_test_function,
|
|
26
|
+
GOOD_test_case,
|
|
27
|
+
},
|
|
28
|
+
when: {
|
|
29
|
+
executing_test_case,
|
|
30
|
+
},
|
|
31
|
+
then: {
|
|
32
|
+
all_GIVENS_called,
|
|
33
|
+
all_WHENS_called,
|
|
34
|
+
all_THENS_called,
|
|
35
|
+
},
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## [Scenario Test](https://github.com/devzeebo/gwt-runner/blob/main/README.md#scenario-definition)
|
|
41
|
+
|
|
42
|
+
Sometimes a GWT flow doesn't make sense. You might be writing integration tests.
|
|
43
|
+
Or something that needs to assert something, then do another thing, then assert
|
|
44
|
+
something else.
|
|
45
|
+
|
|
46
|
+
In these cases, you can use the scenario definition style which allows chaining
|
|
47
|
+
`when` and `then`, followed by `then_when` and `then` blocks.
|
|
48
|
+
|
|
49
|
+
```ts
|
|
50
|
+
{
|
|
51
|
+
given: {
|
|
52
|
+
mock_vitest_test_function,
|
|
53
|
+
GOOD_test_case,
|
|
54
|
+
},
|
|
55
|
+
scenario: [{
|
|
56
|
+
when: {
|
|
57
|
+
executing_test_case,
|
|
58
|
+
},
|
|
59
|
+
then: {
|
|
60
|
+
assert_something,
|
|
61
|
+
},
|
|
62
|
+
}, {
|
|
63
|
+
then_when: {
|
|
64
|
+
user_submits_form,
|
|
65
|
+
},
|
|
66
|
+
then: {
|
|
67
|
+
something_else_happens,
|
|
68
|
+
yet_another_thing_is_true,
|
|
69
|
+
},
|
|
70
|
+
}, {
|
|
71
|
+
then_when: {
|
|
72
|
+
something_happens,
|
|
73
|
+
},
|
|
74
|
+
then: {
|
|
75
|
+
expect_error: some_check,
|
|
76
|
+
and: {
|
|
77
|
+
something_is_still_true,
|
|
78
|
+
},
|
|
79
|
+
}
|
|
80
|
+
}]
|
|
81
|
+
}
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
## Disabling a test
|
|
85
|
+
Sometimes you want a test to be disabled in code. Vitest offers this functionality with
|
|
86
|
+
the `xtest` method, and we've duplicated this logic, but with strong typing so you can
|
|
87
|
+
disable your gwt style tests.
|
|
88
|
+
|
|
89
|
+
```js
|
|
90
|
+
import test, { xtest } from 'vitest-gwt';
|
|
91
|
+
|
|
92
|
+
describe('test context', () => {
|
|
93
|
+
test('this test will run', {
|
|
94
|
+
then: {
|
|
95
|
+
CANARY,
|
|
96
|
+
},
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
xtest('this test will NOT run', {
|
|
100
|
+
when: {
|
|
101
|
+
oops_we_broke_this,
|
|
102
|
+
},
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
## withAspect
|
|
108
|
+
|
|
109
|
+
`withAspect` wraps up vitest's `beforeEach` and `afterEach` to allow preparing and
|
|
110
|
+
cleaning up the context before running tests.
|
|
111
|
+
|
|
112
|
+
```js
|
|
113
|
+
withAspect(
|
|
114
|
+
// this is the beforeEach. Do your prep work here
|
|
115
|
+
function(this: Context) {
|
|
116
|
+
},
|
|
117
|
+
// this is the afterEach. It is OPTIONAL. If you need to do clean up of
|
|
118
|
+
// external resources you allocated in the beforeEach, do it here
|
|
119
|
+
function(this: Context) {
|
|
120
|
+
}
|
|
121
|
+
)
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
The `afterEach` has access to whatever values you put on the Context in the
|
|
125
|
+
`beforeEach`. It does NOT have access to the values put on the Context during
|
|
126
|
+
the specific test.
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
## Detailed Usage
|
|
130
|
+
|
|
131
|
+
Please refer to [gwt-runner](https://github.com/devzeebo/gwt-runner) for
|
|
132
|
+
detailed usage on how to write tests and clauses.
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { TestContext } from 'gwt-runner';
|
|
2
|
+
declare const _default: <TContext>(name: string, gwtDefinition: import("gwt-runner").GwtDefinition<TContext>) => void;
|
|
3
|
+
export default _default;
|
|
4
|
+
export { TestContext };
|
|
5
|
+
export declare const withAspect: <T>(before: (this: T) => any, after?: (this: T) => any) => void;
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.withAspect = exports.TestContext = void 0;
|
|
4
|
+
var tslib_1 = require("tslib");
|
|
5
|
+
var vitest_1 = require("vitest");
|
|
6
|
+
var gwt_runner_1 = tslib_1.__importStar(require("gwt-runner"));
|
|
7
|
+
Object.defineProperty(exports, "TestContext", { enumerable: true, get: function () { return gwt_runner_1.TestContext; } });
|
|
8
|
+
var withAspect_1 = tslib_1.__importDefault(require("./withAspect"));
|
|
9
|
+
exports.default = (0, gwt_runner_1.default)(vitest_1.test);
|
|
10
|
+
exports.withAspect = (0, withAspect_1.default)(vitest_1.beforeEach, vitest_1.afterEach);
|
|
11
|
+
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;AAAA,iCAIgB;AAChB,+DAAoD;AAI3C,4FAJW,wBAAW,OAIX;AAHpB,oEAA6C;AAE7C,kBAAe,IAAA,oBAAS,EAAC,aAAM,CAAC,CAAC;AAGpB,QAAA,UAAU,GAAG,IAAA,oBAAiB,EAAC,mBAAgB,EAAE,kBAAe,CAAC,CAAC"}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { beforeEach as vitestBeforeEach, afterEach as vitestAfterEach } from 'vitest';
|
|
2
|
+
type Callback<T> = (this: T) => any;
|
|
3
|
+
declare const _default: (beforeEach: typeof vitestBeforeEach, afterEach: typeof vitestAfterEach) => <T>(before: Callback<T>, after?: Callback<T>) => void;
|
|
4
|
+
export default _default;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
var tslib_1 = require("tslib");
|
|
4
|
+
var gwt_runner_1 = require("gwt-runner");
|
|
5
|
+
exports.default = (function (beforeEach, afterEach) { return (function (before, after) {
|
|
6
|
+
beforeEach(function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
|
|
7
|
+
return tslib_1.__generator(this, function (_a) {
|
|
8
|
+
switch (_a.label) {
|
|
9
|
+
case 0:
|
|
10
|
+
gwt_runner_1.TestContext.createContext();
|
|
11
|
+
return [4 /*yield*/, before.bind(gwt_runner_1.TestContext.context)()];
|
|
12
|
+
case 1:
|
|
13
|
+
_a.sent();
|
|
14
|
+
return [2 /*return*/];
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
}); });
|
|
18
|
+
afterEach(function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
|
|
19
|
+
return tslib_1.__generator(this, function (_a) {
|
|
20
|
+
switch (_a.label) {
|
|
21
|
+
case 0:
|
|
22
|
+
if (!after) return [3 /*break*/, 2];
|
|
23
|
+
return [4 /*yield*/, after.bind(gwt_runner_1.TestContext.context)()];
|
|
24
|
+
case 1:
|
|
25
|
+
_a.sent();
|
|
26
|
+
_a.label = 2;
|
|
27
|
+
case 2:
|
|
28
|
+
gwt_runner_1.TestContext.releaseContext();
|
|
29
|
+
return [2 /*return*/];
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
}); });
|
|
33
|
+
}); });
|
|
34
|
+
//# sourceMappingURL=withAspect.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"withAspect.js","sourceRoot":"","sources":["../src/withAspect.ts"],"names":[],"mappings":";;;AAIA,yCAAyC;AAIzC,mBAAe,UACb,UAAmC,EACnC,SAAiC,IAC9B,OAAA,CACH,UACE,MAAmB,EACnB,KAAmB;IAEnB,UAAU,CAAC;;;;oBACT,wBAAW,CAAC,aAAa,EAAE,CAAC;oBAE5B,qBAAO,MAAM,CAAC,IAAI,CAAC,wBAAW,CAAC,OAAY,CAAS,EAAE,EAAA;;oBAAtD,SAAsD,CAAC;;;;SACxD,CAAC,CAAC;IAEH,SAAS,CAAC;;;;yBACJ,KAAK,EAAL,wBAAK;oBACP,qBAAO,KAAK,CAAC,IAAI,CAAC,wBAAW,CAAC,OAAY,CAAS,EAAE,EAAA;;oBAArD,SAAqD,CAAC;;;oBAGxD,wBAAW,CAAC,cAAc,EAAE,CAAC;;;;SAC9B,CAAC,CAAC;AACL,CAAC,CACF,EAnBI,CAmBJ,EAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "vitest-gwt",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "A small library to help Vitest support given-when-then style testing without a bunch of overhead",
|
|
5
|
+
"main": "lib/index.js",
|
|
6
|
+
"types": "lib/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"lib",
|
|
9
|
+
"src",
|
|
10
|
+
"!**/*.spec.ts"
|
|
11
|
+
],
|
|
12
|
+
"scripts": {
|
|
13
|
+
"build": "wireit",
|
|
14
|
+
"test": "wireit",
|
|
15
|
+
"lint": "wireit",
|
|
16
|
+
"test:coverage": "wireit",
|
|
17
|
+
"prepublishOnly": "wireit"
|
|
18
|
+
},
|
|
19
|
+
"wireit": {
|
|
20
|
+
"build": {
|
|
21
|
+
"command": "tsc --project ./tsconfig.build.json",
|
|
22
|
+
"files": [
|
|
23
|
+
"src/**/*.ts",
|
|
24
|
+
"tsconfig.*",
|
|
25
|
+
"!src/**/*.spec.ts"
|
|
26
|
+
],
|
|
27
|
+
"output": [
|
|
28
|
+
"lib/**"
|
|
29
|
+
]
|
|
30
|
+
},
|
|
31
|
+
"lint": {
|
|
32
|
+
"command": "eslint ./src"
|
|
33
|
+
},
|
|
34
|
+
"test": {
|
|
35
|
+
"command": "vitest run"
|
|
36
|
+
},
|
|
37
|
+
"test:coverage": {
|
|
38
|
+
"command": "vitest run --coverage"
|
|
39
|
+
},
|
|
40
|
+
"prepublishOnly": {
|
|
41
|
+
"dependencies": [
|
|
42
|
+
"lint",
|
|
43
|
+
"test:coverage",
|
|
44
|
+
"build"
|
|
45
|
+
]
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
"repository": {
|
|
49
|
+
"type": "git",
|
|
50
|
+
"url": "git+https://github.com/devzeebo/vitest-gwt.git"
|
|
51
|
+
},
|
|
52
|
+
"peerDependencies": {
|
|
53
|
+
"vitest": ">2.0.0"
|
|
54
|
+
},
|
|
55
|
+
"keywords": [
|
|
56
|
+
"vitest",
|
|
57
|
+
"gwt",
|
|
58
|
+
"unit",
|
|
59
|
+
"testing",
|
|
60
|
+
"given",
|
|
61
|
+
"when",
|
|
62
|
+
"then"
|
|
63
|
+
],
|
|
64
|
+
"author": "Eric Siebeneich",
|
|
65
|
+
"license": "MIT",
|
|
66
|
+
"bugs": {
|
|
67
|
+
"url": "https://github.com/devzeebo/vitest-gwt/issues"
|
|
68
|
+
},
|
|
69
|
+
"homepage": "https://github.com/devzeebo/vitest-gwt#readme",
|
|
70
|
+
"devDependencies": {
|
|
71
|
+
"@react-ddd/eslint-config": "^0.3.1",
|
|
72
|
+
"@types/lodash": "^4.14.198",
|
|
73
|
+
"@types/node": "^20.6.0",
|
|
74
|
+
"@vitest/coverage-v8": "^2.0.5",
|
|
75
|
+
"axios": "^1.5.0",
|
|
76
|
+
"lodash": "^4.17.21",
|
|
77
|
+
"typescript": "^5.2.2",
|
|
78
|
+
"vitest": ">2.0.0",
|
|
79
|
+
"wireit": "^0.13.0"
|
|
80
|
+
},
|
|
81
|
+
"dependencies": {
|
|
82
|
+
"gwt-runner": "^2.4.0"
|
|
83
|
+
}
|
|
84
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import {
|
|
2
|
+
test as vitest,
|
|
3
|
+
beforeEach as vitestBeforeEach,
|
|
4
|
+
afterEach as vitestAfterEach,
|
|
5
|
+
} from 'vitest';
|
|
6
|
+
import gwtRunner, { TestContext } from 'gwt-runner';
|
|
7
|
+
import withAspectBuilder from './withAspect';
|
|
8
|
+
|
|
9
|
+
export default gwtRunner(vitest);
|
|
10
|
+
export { TestContext };
|
|
11
|
+
|
|
12
|
+
export const withAspect = withAspectBuilder(vitestBeforeEach, vitestAfterEach);
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
beforeEach as vitestBeforeEach,
|
|
3
|
+
afterEach as vitestAfterEach,
|
|
4
|
+
} from 'vitest';
|
|
5
|
+
import { TestContext } from 'gwt-runner';
|
|
6
|
+
|
|
7
|
+
type Callback<T> = (this: T) => any;
|
|
8
|
+
|
|
9
|
+
export default (
|
|
10
|
+
beforeEach: typeof vitestBeforeEach,
|
|
11
|
+
afterEach: typeof vitestAfterEach,
|
|
12
|
+
) => (
|
|
13
|
+
<T>(
|
|
14
|
+
before: Callback<T>,
|
|
15
|
+
after?: Callback<T>,
|
|
16
|
+
) => {
|
|
17
|
+
beforeEach(async () => {
|
|
18
|
+
TestContext.createContext();
|
|
19
|
+
|
|
20
|
+
await (before.bind(TestContext.context as T) as any)();
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
afterEach(async () => {
|
|
24
|
+
if (after) {
|
|
25
|
+
await (after.bind(TestContext.context as T) as any)();
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
TestContext.releaseContext();
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
);
|