puty 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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Yuusoft
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
10
+ is furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included
13
+ in all 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
21
+ IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,63 @@
1
+ # Puty: Pure Unit Test in Yaml
2
+
3
+ Write unit test specifications using YAML files, powered by vitest as the test runner.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install puty
9
+ ```
10
+
11
+ create a file in your project `puty.test.js`
12
+ ```js
13
+ import { setupTestSuiteFromYaml } from "./puty.js";
14
+ await setupTestSuiteFromYaml();
15
+ ```
16
+
17
+ This function will read for all `.spec.yaml` and `.test.yaml` files in the current directory and run the tests.
18
+
19
+ run
20
+
21
+ ```
22
+ vitest
23
+ ```
24
+
25
+ It should run all the tests in the file.
26
+
27
+ ## Usage
28
+
29
+ ```yaml
30
+ file: './math.js'
31
+ group: math
32
+ suites: [add, increment]
33
+ ---
34
+ ### Add
35
+ suite: add
36
+ exportName: add
37
+ ---
38
+ case: add 1 and 2
39
+ in:
40
+ - 1
41
+ - 2
42
+ out: 3
43
+ ---
44
+ case: add 2 and 2
45
+ in:
46
+ - 2
47
+ - 2
48
+ out: 4
49
+ ---
50
+ ### Increment
51
+ suite: increment
52
+ exportName: default
53
+ ---
54
+ case: increment 1
55
+ in:
56
+ - 1
57
+ out: 2
58
+ ---
59
+ case: increment 2
60
+ in:
61
+ - 2
62
+ out: 3
63
+ ```
package/package.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "name": "puty",
3
+ "version": "0.0.1",
4
+ "description": "A tooling function to test javascript functions and classes.",
5
+ "main": "src/index.js",
6
+ "type": "module",
7
+ "files": [
8
+ "src"
9
+ ],
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "git+https://github.com/yuusoft-org/puty.git"
13
+ },
14
+ "license": "MIT",
15
+ "dependencies": {
16
+ "js-yaml": "~4.1.0"
17
+ },
18
+ "scripts": {
19
+ "lint": "bunx prettier src tests -c",
20
+ "lint:fix": "bunx prettier src tests -w",
21
+ "build": "bun run esbuild.js"
22
+ }
23
+ }
package/src/index.js ADDED
@@ -0,0 +1,3 @@
1
+ import { setupTestSuiteFromYaml } from "./puty.js";
2
+
3
+ export { setupTestSuiteFromYaml };
package/src/puty.js ADDED
@@ -0,0 +1,128 @@
1
+ import path from "node:path";
2
+ import fs from "node:fs";
3
+ import yaml from "js-yaml";
4
+ import { expect, test, describe } from "vitest";
5
+
6
+ import { traverseAllFiles } from "./utils.js";
7
+
8
+ const extensions = [".test.yaml", ".test.yml", ".spec.yaml", ".spec.yml"];
9
+
10
+ /**
11
+ * Parse YAML content with document separators into structured test config
12
+ */
13
+ export const parseYamlDocuments = (yamlContent) => {
14
+ const docs = yaml.loadAll(yamlContent);
15
+ const config = {
16
+ file: null,
17
+ group: null,
18
+ suites: [],
19
+ };
20
+
21
+ let currentSuite = null;
22
+
23
+ for (const doc of docs) {
24
+ if (doc.file) {
25
+ config.file = doc.file;
26
+ config.group = doc.group || doc.name;
27
+ if (doc.suites) {
28
+ config.suiteNames = doc.suites;
29
+ }
30
+ } else if (doc.suite) {
31
+ if (currentSuite) {
32
+ config.suites.push(currentSuite);
33
+ }
34
+ currentSuite = {
35
+ name: doc.suite,
36
+ exportName: doc.exportName || doc.suite,
37
+ cases: [],
38
+ };
39
+ } else if (doc.case && currentSuite) {
40
+ currentSuite.cases.push({
41
+ name: doc.case,
42
+ in: doc.in || [],
43
+ out: doc.out,
44
+ });
45
+ }
46
+ }
47
+
48
+ if (currentSuite) {
49
+ config.suites.push(currentSuite);
50
+ }
51
+
52
+ return config;
53
+ };
54
+
55
+ /**
56
+ *
57
+ * @param {*} testConfig
58
+ */
59
+ const setupTestSuite = (testConfig) => {
60
+ const { group, suites, skip } = testConfig;
61
+ if (skip) {
62
+ return;
63
+ }
64
+ describe(group, () => {
65
+ for (const suite of suites) {
66
+ describe(suite.name, () => {
67
+ const { cases } = suite;
68
+ for (const testCase of cases) {
69
+ const {
70
+ name,
71
+ in: inArg,
72
+ out: expectedOut,
73
+ functionUnderTest,
74
+ } = testCase;
75
+ test(name, () => {
76
+ const out = functionUnderTest(...(inArg || []));
77
+ if (out instanceof Error) {
78
+ expect(out.message).toEqual(out.message);
79
+ } else {
80
+ expect(out).toEqual(expectedOut);
81
+ }
82
+ });
83
+ }
84
+ });
85
+ }
86
+ });
87
+ };
88
+
89
+ /**
90
+ *
91
+ * @param {*} module
92
+ * @param {*} originalTestConfig
93
+ * @returns
94
+ */
95
+ export const injectFunctions = (module, originalTestConfig) => {
96
+ const testConfig = structuredClone(originalTestConfig);
97
+ let functionUnderTest = module[testConfig.exportName || "default"];
98
+
99
+ for (const suite of testConfig.suites) {
100
+ if (suite.exportName) {
101
+ functionUnderTest = module[suite.exportName];
102
+ }
103
+ for (const testCase of suite.cases) {
104
+ testCase.functionUnderTest = functionUnderTest;
105
+ }
106
+ }
107
+ return testConfig;
108
+ };
109
+
110
+ /**
111
+ * Setup test suites from all yaml files in the current directory and its subdirectories
112
+ */
113
+ export const setupTestSuiteFromYaml = async (dirname) => {
114
+ const testYamlFiles = traverseAllFiles(dirname, extensions);
115
+ for (const file of testYamlFiles) {
116
+ const yamlContent = fs.readFileSync(file, "utf8");
117
+ const testConfig = parseYamlDocuments(yamlContent);
118
+ const filepathRelativeToSpecFile = path.join(
119
+ path.dirname(file),
120
+ testConfig.file,
121
+ );
122
+
123
+ // testConfig.file is relative to the spec file
124
+ const module = await import(filepathRelativeToSpecFile);
125
+ const testConfigWithInjectedFunctions = injectFunctions(module, testConfig);
126
+ setupTestSuite(testConfigWithInjectedFunctions);
127
+ }
128
+ };
package/src/utils.js ADDED
@@ -0,0 +1,35 @@
1
+ import fs from "node:fs";
2
+ import path, { join } from "node:path";
3
+
4
+ import yaml from "js-yaml";
5
+
6
+ export const loadYamlWithPath = (path) => {
7
+ const includeType = new yaml.Type("!include", {
8
+ kind: "scalar",
9
+ construct: function (filePath) {
10
+ const content = fs.readFileSync(join(path, "..", filePath), "utf8");
11
+ return yaml.load(content); // you could recurse here
12
+ },
13
+ });
14
+ const schema = yaml.DEFAULT_SCHEMA.extend([includeType]);
15
+ return yaml.load(fs.readFileSync(path, "utf8"), { schema });
16
+ };
17
+
18
+ /**
19
+ * Traverse all files in the current directory and its subdirectories
20
+ * Return an array of full path of files
21
+ */
22
+ export const traverseAllFiles = (startPath, extensions) => {
23
+ const results = [];
24
+ const files = fs.readdirSync(startPath);
25
+ for (const file of files) {
26
+ const filePath = path.join(startPath, file);
27
+ const stats = fs.statSync(filePath);
28
+ if (stats.isDirectory()) {
29
+ results.push(...traverseAllFiles(filePath, extensions));
30
+ } else if (extensions.some((ext) => file.endsWith(ext))) {
31
+ results.push(filePath);
32
+ }
33
+ }
34
+ return results;
35
+ };