tinyexpect 0.4.1 → 0.6.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2022 Mak Drakos
3
+ Copyright (c) 2024 crafter999
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -1,3 +1,37 @@
1
1
  # tinyexpect
2
2
 
3
- `expect` with 0 dependencies for testing using the native Node.js test runner `node --test`.
3
+ `expect` is a lightweight testing utility with 0 dependencies that provides a Jest-like assertion API for TypeScript/JavaScript projects that use the native Node.js test runner `node --test`.
4
+
5
+ ## API Reference
6
+
7
+ ### Matchers
8
+
9
+ - `toBe(value)`: Checks strict equality (including deep equality for objects)
10
+ - `toNotBe(value)`: Checks strict inequality
11
+ - `eitherOr(values[])`: Checks if value is included in an array of possible values
12
+ - `toBeNumber()`: Verifies value is a number
13
+ - `toBeNumberOrParsed()`: Verifies value is either a number or can be parsed as one
14
+ - `toBeNumberOr(values[])`: Verifies value is either a number or one of the specified strings
15
+ - `toBeFalsy()`: Checks if value is falsy
16
+ - `toBeTruthy()`: Checks if value is truthy
17
+ - `toBeDefined()`: Verifies value is not undefined
18
+ - `toContainString(str)`: Checks if string contains a substring
19
+ - `toNotContainString(str)`: Checks if string does not contain a substring
20
+ - `toBeSameObject(value)`: Check objects, return true if different property order
21
+
22
+ ### Example
23
+ ```typescript
24
+ import { expect } from "tinyexpect"
25
+ expect("test").toBe("test")
26
+ ```
27
+
28
+ ## Error Handling
29
+
30
+ All matchers throw descriptive errors when assertions fail, including:
31
+ - Detailed error messages for object comparisons
32
+ - Type mismatch notifications
33
+ - Clear expected vs actual value reporting
34
+
35
+ ## License
36
+
37
+ MIT
package/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.expect = void 0;
3
+ exports.expect = expect;
4
4
  function expect(v) {
5
5
  return {
6
6
  toBe: (a) => {
@@ -14,6 +14,16 @@ function expect(v) {
14
14
  throw new Error(`Expected ${v} to be ${a}`);
15
15
  }
16
16
  },
17
+ toNotBe: (a) => {
18
+ if (typeof v === "object") {
19
+ if (JSON.stringify(v) === JSON.stringify(a)) {
20
+ throw new Error(`Expected ${JSON.stringify(v, null, 2)} to not be ${JSON.stringify(a, null, 2)}`);
21
+ }
22
+ }
23
+ else if (v === a) {
24
+ throw new Error(`Expected ${v} to not be ${a}`);
25
+ }
26
+ },
17
27
  eitherOr: (values) => {
18
28
  if (!values.includes(v)) {
19
29
  throw new Error(`Expected ${v} to be either of the following values:` +
@@ -62,14 +72,18 @@ function expect(v) {
62
72
  throw new Error(`${v} doesn't contain ${str}`);
63
73
  }
64
74
  },
65
- toNotContainString(str) {
66
- if (typeof v !== "string") {
67
- throw new Error(`${v} is not a string`);
75
+ toBeSameObject(obj) {
76
+ if (typeof v !== "object" || typeof obj !== "object") {
77
+ throw new Error(`Both values must be objects`);
68
78
  }
69
- if (v.includes(str)) {
70
- throw new Error(`${v} contains ${str}`);
79
+ const vKeys = Object.keys(v).sort();
80
+ const objKeys = Object.keys(obj).sort();
81
+ if (vKeys.length !== objKeys.length || vKeys.every((key, i) => key === objKeys[i])) {
82
+ throw new Error(`Object keys do not match`);
83
+ }
84
+ if (!vKeys.every(key => JSON.stringify(v[key]) === JSON.stringify(obj[key]))) {
85
+ throw new Error(`Object values do not match`);
71
86
  }
72
87
  }
73
88
  };
74
89
  }
75
- exports.expect = expect;
package/index.ts CHANGED
@@ -10,6 +10,15 @@ export function expect(v: any) {
10
10
  throw new Error(`Expected ${v} to be ${a}`);
11
11
  }
12
12
  },
13
+ toNotBe: (a: any) => {
14
+ if (typeof v === "object"){
15
+ if (JSON.stringify(v) === JSON.stringify(a)) {
16
+ throw new Error(`Expected ${JSON.stringify(v,null,2)} to not be ${JSON.stringify(a,null,2)}`);
17
+ }
18
+ } else if (v === a) {
19
+ throw new Error(`Expected ${v} to not be ${a}`);
20
+ }
21
+ },
13
22
  eitherOr: (values: any[]) => {
14
23
  if (!values.includes(v)) {
15
24
  throw new Error(
@@ -61,14 +70,20 @@ export function expect(v: any) {
61
70
  throw new Error(`${v} doesn't contain ${str}`)
62
71
  }
63
72
  },
64
- toNotContainString(str:string){
65
- if (typeof v !== "string"){
66
- throw new Error(`${v} is not a string`)
67
- }
73
+ toBeSameObject(obj:any){
74
+ if (typeof v !== "object" || typeof obj !== "object") {
75
+ throw new Error(`Both values must be objects`);
76
+ }
77
+
78
+ const vKeys = Object.keys(v).sort();
79
+ const objKeys = Object.keys(obj).sort();
68
80
 
69
- if ((v as string).includes(str)){
70
- throw new Error(`${v} contains ${str}`)
71
- }
72
- }
81
+ if (vKeys.length !== objKeys.length || vKeys.every((key, i) => key === objKeys[i])) {
82
+ throw new Error(`Object keys do not match`);
83
+ }
84
+ if (!vKeys.every(key => JSON.stringify(v[key]) === JSON.stringify(obj[key]))) {
85
+ throw new Error(`Object values do not match`);
86
+ }
87
+ }
73
88
  }
74
89
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tinyexpect",
3
- "version": "0.4.1",
3
+ "version": "0.6.1",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {