Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
abhishiv committed Aug 21, 2024
0 parents commit f2da45f
Show file tree
Hide file tree
Showing 13 changed files with 281 additions and 0 deletions.
37 changes: 37 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: CI
on:
push:
branches:
- master
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout 🛎️
uses: actions/[email protected]
with:
persist-credentials: false
- uses: actions/setup-node@v2
with:
node-version: "18.x"
cache: "npm"

- name: Install
run: env NODE_ENV=development npm install --prefer-offline --no-audit
- name: Test
run: npm run typecheck && npm run test

- name: Build
run: env NODE_ENV=production npm run build
- run: jq -s 'add' package.json package-publish.json > package-final.json && rm -rf package.json && mv package-final.json package.json

- id: Publish
uses: JS-DevTools/npm-publish@v1
with:
registry: "https://registry.npmjs.org/"
token: ${{ secrets.NPM_TOKEN }}
access: "public"
check-version: true
- if: steps.publish.outputs.type != 'none'
run: |
echo "Version changed: ${{ steps.publish.outputs.old-version }} => ${{ steps.publish.outputs.version }}"
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
.vscode
.DS_Store
dist
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

.SHELL := /usr/bin/bash

.PHONY: build
build:
npx tsc --resolveJsonModule -p ./tsconfig.json --outDir ./dist --emitDeclarationOnly --declaration
node esbuild.mjs
22 changes: 22 additions & 0 deletions esbuild.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import * as esbuild from "esbuild";

(async () => {
await esbuild.build({
entryPoints: ["src/index.ts"], // Specify your entry point(s) here
outfile: "dist/index.js", // Specify the output file here
bundle: true,
packages: "external",
target: "es2022",
format: "esm",
sourcemap: false,
minify: true,
plugins: [
// sassPlugin({
// type: "style",
// transform: postcssModules({
// // ...put here the options for postcss-modules: https://github.com/madyankin/postcss-modules
// }),
// }),
],
});
})();
7 changes: 7 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions package-publish.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"main": "./dist/index.js",
"module": "./dist/index.js",
"types": "./dist/index.d.ts",
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.js"
}
}
}
38 changes: 38 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "attorneyjs",
"version": "0.8.32",
"author": "Abhishiv Saxena<[email protected]>",
"license": "MIT",
"description": "Ultralean Test runner for browser and node",
"keywords": [],
"repository": {
"type": "git",
"url": "https://github.com/abhishiv/attorney"
},
"files": [
"./dist"
],
"devDependencies": {
"esbuild": "^0.21.5",
"typescript": "^5.2.2",
"vitest": "^2.0.5"
},
"scripts": {
"build": "make build",
"test": "npx vitest --run --passWithNoTests",
"coverage": "vitest run --coverage --run --passWithNoTests",
"typecheck": "npx tsc --noEmit"
},
"main": "./src/index.ts",
"exports": {
".": {
"import": "./src/index.ts"
}
},
"dependencies": {
"buckwheat": "^1.1.2"
},
"peerDependencies": {
"alfama": "*"
}
}
16 changes: 16 additions & 0 deletions src/example.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { describe, runSuite, printTap } from "./index";

const suite = describe("My first test suite", ({ test }) => {
test("my test1", ({ expect }) => {
expect(4).toBe(4);
});

test("my test2", ({ expect }) => {
expect(4).toBe(4);
});
});

runSuite(suite).then((results) => {
// console.log(results);
console.log(printTap(results));
});
51 changes: 51 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import {
Suite,
SuiteMeta,
SuiteHandler,
SuiteAttrs,
TestMeta,
TestHandler,
Results,
} from "./types";
import { expect } from "buckwheat";

export * from "./types/index";
export * from "./tap";

export const describe = (meta: string | SuiteMeta, fn: SuiteHandler) => {
const suite: Suite = {
type: "Suite",
meta: typeof meta === "string" ? { name: meta } : meta,
tests: [],
};
const attrs: SuiteAttrs = {
test: (name: string | TestMeta, fn: TestHandler) => {
suite.tests.push({
type: "Test",
meta: typeof name === "string" ? { name } : name,
fn: fn,
});
},
};
fn(attrs);
return suite;
};

export async function runSuite(suite: Suite) {
const results: Results = [];
suite.tests.forEach(async (test, index) => {
try {
const fn = test.fn({ expect });
const result = await Promise.resolve(fn);
results.push({ index, test, result, type: "passed" });
} catch (e) {
results.push({
index,
test,
type: "failed",
error: e as any,
});
}
});
return results;
}
31 changes: 31 additions & 0 deletions src/tap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Results } from "./types/index";

export function printTap(arg: Results) {
const results = arg;
const tapOutput: string[] = [];

// Pushing the test plan (total number of tests) into the array
tapOutput.push(`1..${results.length}`);

// Iterating through each test result and pushing TAP lines into the array
results.forEach((result, i) => {
if (result.type === "passed") {
tapOutput.push(`ok ${result.index} - ${result.test.meta.name}`);
} else {
tapOutput.push(`not ok ${result.index} - ${result.test.meta.name}`);
tapOutput.push(` ---`);
tapOutput.push(` name: ${result.error.name}`);
tapOutput.push(` operator: error`);
if (result.error instanceof Error) {
tapOutput.push(` message: ${result.error.message}`);
tapOutput.push(` stack: ${result.error.stack}`);
} else {
tapOutput.push(` actual: ${result.error.actual}`);
tapOutput.push(` expected: ${result.error.expected}`);
}
tapOutput.push(` ...`);
}
});

return tapOutput.join("\n");
}
29 changes: 29 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { expect } from "buckwheat";

export type SuiteAttrs = { test: TestDefinition };
export type SuiteHandler = (attrs: SuiteAttrs) => void;

export type TestAttrs = { expect: typeof expect };
export type TestHandler = (attrs: TestAttrs) => void;

export type SuiteMeta = { name: string };
export type TestMeta = { name: string };

export type TestDefinition = (name: string | TestMeta, fn: TestHandler) => void;

export type Test = { type: "Test"; meta: TestMeta; fn: TestHandler };
export type Suite = { type: "Suite"; meta: SuiteMeta; tests: Test[] };

export type PassedResult = {
index: number;
type: "passed";
test: Test;
result: any;
};
export type FailedResult = {
index: number;
type: "failed";
test: Test;
error: { name: string; actual: any; expected: any } | Error;
};
export type Results = (PassedResult | FailedResult)[];
21 changes: 21 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"ts-node": {
"compilerOptions": {
"module": "commonjs"
}
},
"compilerOptions": {
"target": "ESNext",
"useDefineForClassFields": true,
"module": "ESNext",
"moduleResolution": "Node",
"strict": true,
"jsx": "react",
"resolveJsonModule": true,
"isolatedModules": true,
"esModuleInterop": true,
"lib": ["ESNext", "DOM"],
"skipLibCheck": true
},
"include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue"]
}
7 changes: 7 additions & 0 deletions vite.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { defineConfig } from "vite";

export default defineConfig({
build: {
target: "esnext",
},
});

0 comments on commit f2da45f

Please sign in to comment.