@strictly/vitest 0.0.0-stage → 0.0.41

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,3 +1,19 @@
1
- # Temporary Holding Version
1
+ # @strictly/vitest
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Vitest assertions that narrow types, so tests can carry on without casts or non-null assertions.
4
+
5
+ ```ts
6
+ import { expectDefined } from '@strictly/vitest'
7
+
8
+ const user = users.find(({ id }) => id === 1)
9
+ expectDefined(user)
10
+ expect(user.name).toBe('Ada')
11
+ ```
12
+
13
+ - `expectDefined(v)` and `expectDefinedAndReturn(v)` narrow away `null` and `undefined`
14
+ - `expectEquals(v1, v2)` narrows `v1` to the type of `v2`
15
+ - `expectInstanceOf(v, Class)` narrows `v` to an instance of `Class`
16
+ - `expectTruthy(b)` narrows `b` to `true`
17
+ - `expectPendingPromise(p)` asserts that `p` has not settled within a timeout
18
+
19
+ The same assertions, backed by `storybook/test`, are exported from `@strictly/storybook` for use in story play functions.
package/dist/index.cjs ADDED
@@ -0,0 +1,35 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ let vitest = require("vitest");
3
+ //#region src/expect.ts
4
+ function expectEquals(v1, v2) {
5
+ (0, vitest.expect)(v1).toEqual(v2);
6
+ }
7
+ function expectTruthy(b) {
8
+ (0, vitest.expect)(b).toBeTruthy();
9
+ }
10
+ function expectDefined(v) {
11
+ (0, vitest.expect)(v).toBeDefined();
12
+ }
13
+ function expectInstanceOf(instance, constructor) {
14
+ (0, vitest.expect)(instance).toBeInstanceOf(constructor);
15
+ }
16
+ function expectDefinedAndReturn(v) {
17
+ expectDefined(v);
18
+ return v;
19
+ }
20
+ function pendingAfter(milliseconds) {
21
+ return new Promise((resolve) => {
22
+ setTimeout(() => resolve("pending"), milliseconds);
23
+ });
24
+ }
25
+ async function expectPendingPromise(p, timeoutMilliseconds = 500) {
26
+ const state = await Promise.race([p.then(() => "fulfilled", () => "rejected"), pendingAfter(timeoutMilliseconds)]);
27
+ (0, vitest.expect)(state).toBe("pending");
28
+ }
29
+ //#endregion
30
+ exports.expectDefined = expectDefined;
31
+ exports.expectDefinedAndReturn = expectDefinedAndReturn;
32
+ exports.expectEquals = expectEquals;
33
+ exports.expectInstanceOf = expectInstanceOf;
34
+ exports.expectPendingPromise = expectPendingPromise;
35
+ exports.expectTruthy = expectTruthy;
@@ -0,0 +1,13 @@
1
+ export declare function expectDefined<V>(v: V): asserts v is NonNullable<V>;
2
+
3
+ export declare function expectDefinedAndReturn<V>(v: V): NonNullable<V>;
4
+
5
+ export declare function expectEquals<V1, V2 extends V1>(v1: V1, v2: V2): asserts v1 is V2;
6
+
7
+ export declare function expectInstanceOf<V1, V2 extends V1>(instance: V1, constructor: abstract new (...args: never[]) => V2): asserts instance is V2;
8
+
9
+ export declare function expectPendingPromise<V>(p: Promise<V>, timeoutMilliseconds?: number): Promise<void>;
10
+
11
+ export declare function expectTruthy(b: boolean): asserts b is true;
12
+
13
+ export { }
@@ -0,0 +1,13 @@
1
+ export declare function expectDefined<V>(v: V): asserts v is NonNullable<V>;
2
+
3
+ export declare function expectDefinedAndReturn<V>(v: V): NonNullable<V>;
4
+
5
+ export declare function expectEquals<V1, V2 extends V1>(v1: V1, v2: V2): asserts v1 is V2;
6
+
7
+ export declare function expectInstanceOf<V1, V2 extends V1>(instance: V1, constructor: abstract new (...args: never[]) => V2): asserts instance is V2;
8
+
9
+ export declare function expectPendingPromise<V>(p: Promise<V>, timeoutMilliseconds?: number): Promise<void>;
10
+
11
+ export declare function expectTruthy(b: boolean): asserts b is true;
12
+
13
+ export { }
package/dist/index.js ADDED
@@ -0,0 +1,29 @@
1
+ import { expect } from "vitest";
2
+ //#region src/expect.ts
3
+ function expectEquals(v1, v2) {
4
+ expect(v1).toEqual(v2);
5
+ }
6
+ function expectTruthy(b) {
7
+ expect(b).toBeTruthy();
8
+ }
9
+ function expectDefined(v) {
10
+ expect(v).toBeDefined();
11
+ }
12
+ function expectInstanceOf(instance, constructor) {
13
+ expect(instance).toBeInstanceOf(constructor);
14
+ }
15
+ function expectDefinedAndReturn(v) {
16
+ expectDefined(v);
17
+ return v;
18
+ }
19
+ function pendingAfter(milliseconds) {
20
+ return new Promise((resolve) => {
21
+ setTimeout(() => resolve("pending"), milliseconds);
22
+ });
23
+ }
24
+ async function expectPendingPromise(p, timeoutMilliseconds = 500) {
25
+ const state = await Promise.race([p.then(() => "fulfilled", () => "rejected"), pendingAfter(timeoutMilliseconds)]);
26
+ expect(state).toBe("pending");
27
+ }
28
+ //#endregion
29
+ export { expectDefined, expectDefinedAndReturn, expectEquals, expectInstanceOf, expectPendingPromise, expectTruthy };
package/package.json CHANGED
@@ -1,6 +1,51 @@
1
1
  {
2
+ "author": "Chris <chris.glover@gmail.com> (@madmaw)",
3
+ "description": "Vitest assertion helpers that narrow types",
4
+ "devDependencies": {
5
+ "vite-plus": "1.0.0",
6
+ "vitest": "5.0.1",
7
+ "@strictly/typescript": "0.0.41",
8
+ "@strictly/vite": "0.0.41"
9
+ },
10
+ "files": [
11
+ "dist"
12
+ ],
13
+ "homepage": "https://github.com/madmaw/strictly/tree/main/support/vitest",
14
+ "keywords": [
15
+ "vitest"
16
+ ],
17
+ "license": "MIT",
18
+ "main": "./dist/index.js",
2
19
  "name": "@strictly/vitest",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
20
+ "peerDependencies": {
21
+ "vitest": "^5.0.1"
22
+ },
23
+ "publishConfig": {
24
+ "access": "public"
25
+ },
26
+ "repository": {
27
+ "directory": "support/vitest",
28
+ "type": "git",
29
+ "url": "git+https://github.com/madmaw/strictly.git"
30
+ },
31
+ "type": "module",
32
+ "version": "0.0.41",
33
+ "scripts": {
34
+ "build": "vp build --configLoader runner",
35
+ "check-types": "tsc -b",
36
+ "clean": "del-cli dist .out"
37
+ },
38
+ "exports": {
39
+ ".": {
40
+ "import": {
41
+ "types": "./dist/index.d.ts",
42
+ "default": "./dist/index.js"
43
+ },
44
+ "require": {
45
+ "types": "./dist/index.d.cts",
46
+ "default": "./dist/index.cjs"
47
+ }
48
+ }
49
+ },
50
+ "types": "./dist/index.d.ts"
6
51
  }