Skip to content

Commit

Permalink
fix: test.js
Browse files Browse the repository at this point in the history
  • Loading branch information
ValeriaVG committed Oct 28, 2021
1 parent fd23358 commit a8612fb
Show file tree
Hide file tree
Showing 13 changed files with 133 additions and 32 deletions.
Binary file removed .DS_Store
Binary file not shown.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
*.log
*.log
.DS_Store
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,14 @@ run().then(prettify);
For benchmarks and usage examples see `benchmark` folder.

```js
const { it, run, title } = new Test("name-of-your-test");
const { it, run, title, before, after } = new Test("name-of-your-test");

before(async () => {
// some setup
});
before(async () => {
// some cleanup
});

it("toBe", () => {
expect(2 + 2).toBe(4);
Expand Down
8 changes: 7 additions & 1 deletion benchmark/deno.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
//@ts-ignore
import { Test, expect, prettify } from "../dist/mod.ts";
const { it, run, title } = new Test("node-tiny-jest");
const { it, run, title, before, after } = new Test("deno-tiny-jest");

after(() => console.log("After test"));
before(() => console.log("Before test"));
it("works", () => {
console.log("During test");
});

it("toBe", () => {
expect(2 + 2).toBe(4);
Expand Down
8 changes: 6 additions & 2 deletions benchmark/node.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
const { performance } = require("perf_hooks");
const { Test, expect, prettify } = require("../dist/index");

const { it, run, title } = new Test("node-tiny-jest");

const { it, run, title, after, before } = new Test("node-tiny-jest");
after(() => console.log("After test"));
before(() => console.log("Before test"));
it("works", () => {
console.log("During test");
});
it("toBe", () => {
expect(2 + 2).toBe(4);
expect(2 + 2).not.toBe(5);
Expand Down
20 changes: 16 additions & 4 deletions dist/Test.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,24 @@ export declare type TestResult = {
passed?: boolean;
error?: Error;
};
export declare type FixtureFn = () => Promise<void> | void;
export default class Test {
title?: string;
private suite;
private results;
suite: {
title: string;
fn?: Function;
}[];
results: TestResult[];
private _before;
private _after;
constructor(title?: string);
it: (title: string, fn?: Function) => void;
xit: (title: string, fn?: Function) => void;
run: () => Promise<TestResult[]>;
xit: (title: string, _fn?: Function) => void;
run: () => Promise<TestResult[] | {
title: string;
error: any;
passed: boolean;
}[]>;
before: (fn: FixtureFn) => void;
after: (fn: FixtureFn) => void;
}
22 changes: 21 additions & 1 deletion dist/Test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,22 @@ class Test {
this.suite = [];
// Stores last results
this.results = [];
this._before = [];
this._after = [];
this.it = (title, fn) => {
this.suite.push({ title, fn });
};
this.xit = (title, fn) => {
this.xit = (title, _fn) => {
this.suite.push({ title });
};
this.run = () => __awaiter(this, void 0, void 0, function* () {
this.results = [];
try {
yield Promise.all(this._before.map((fn) => fn()));
}
catch (error) {
return [{ title: this.title, error, passed: false }];
}
for (let test of this.suite) {
if (!test.fn) {
this.results.push({ title: test.title, skipped: true });
Expand All @@ -35,8 +43,20 @@ class Test {
this.results.push({ title: test.title, error, passed: false });
}
}
try {
yield Promise.all(this._after.map((fn) => fn()));
}
catch (error) {
console.error(error);
}
return this.results;
});
this.before = (fn) => {
this._before.push(fn);
};
this.after = (fn) => {
this._after.push(fn);
};
this.title = title;
}
}
Expand Down
2 changes: 1 addition & 1 deletion dist/bundle.js

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

29 changes: 24 additions & 5 deletions dist/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,22 +124,30 @@ export type TestResult = {
passed?: boolean;
error?: Error;
};
export type FixtureFn = () => Promise<void> | void;
export class Test {
title?: string;
private suite: { title: string; fn?: Function }[] = [];
title: string;
suite: { title: string; fn?: Function }[] = [];
// Stores last results
private results: TestResult[] = [];
results: TestResult[] = [];
private _before: FixtureFn[] = [];
private _after: FixtureFn[] = [];
constructor(title?: string) {
this.title = title;
this.title = title ?? "";
}
it = (title: string, fn?: Function) => {
this.suite.push({ title, fn });
};
xit = (title: string, fn?: Function) => {
xit = (title: string, _fn?: Function) => {
this.suite.push({ title });
};
run = async () => {
this.results = [];
try {
await Promise.all(this._before.map((fn) => fn()));
} catch (error) {
return [{ title: this.title, error, passed: false }];
}
for (let test of this.suite) {
if (!test.fn) {
this.results.push({ title: test.title, skipped: true });
Expand All @@ -152,6 +160,17 @@ export class Test {
this.results.push({ title: test.title, error, passed: false });
}
}
try {
await Promise.all(this._after.map((fn) => fn()));
} catch (error) {
console.error(error);
}
return this.results;
};
before = (fn: FixtureFn) => {
this._before.push(fn);
};
after = (fn: FixtureFn) => {
this._after.push(fn);
};
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tiny-jest",
"version": "1.0.4",
"version": "1.1.0",
"description": "Minimalistic zero dependency Jest-like test library to run tests in browser, nodejs or deno.",
"keywords": [
"jest",
Expand Down
26 changes: 23 additions & 3 deletions src/Test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,31 @@ export type TestResult = {
passed?: boolean;
error?: Error;
};

export type FixtureFn = () => Promise<void> | void;
export default class Test {
title?: string;
private suite: { title: string; fn?: Function }[] = [];
suite: { title: string; fn?: Function }[] = [];
// Stores last results
private results: TestResult[] = [];
results: TestResult[] = [];
private _before: FixtureFn[] = [];
private _after: FixtureFn[] = [];
constructor(title?: string) {
this.title = title;
}
it = (title: string, fn?: Function) => {
this.suite.push({ title, fn });
};
xit = (title: string, fn?: Function) => {
xit = (title: string, _fn?: Function) => {
this.suite.push({ title });
};
run = async () => {
this.results = [];
try {
await Promise.all(this._before.map((fn) => fn()));
} catch (error) {
return [{ title: this.title, error, passed: false }];
}
for (let test of this.suite) {
if (!test.fn) {
this.results.push({ title: test.title, skipped: true });
Expand All @@ -32,6 +41,17 @@ export default class Test {
this.results.push({ title: test.title, error, passed: false });
}
}
try {
await Promise.all(this._after.map((fn) => fn()));
} catch (error) {
console.error(error);
}
return this.results;
};
before = (fn: FixtureFn) => {
this._before.push(fn);
};
after = (fn: FixtureFn) => {
this._after.push(fn);
};
}
30 changes: 18 additions & 12 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
const fs = require("fs");
const fs = require("fs/promises");
const path = require("path");
const { prettify } = require("./dist");
// Read directory
function runTests(dir) {
fs.readdir(dir, async (err, files) => {
if (err) return console.error(err);
for (let file of files) {
if (/\.(test|spec)\.js/.test(file) {
const test = require(path.join(__dirname, dir, file));
console.log("Running", test.title, ":", test.suite.length, "tests");
await test.run().then(prettify);
}
async function runTests(dir) {
let passed = true;
const files = await fs.readdir(dir);
for (let file of files) {
if (/\.(test|spec)\.js/.test(file)) {
const test = require(path.join(__dirname, dir, file));
console.log("Running", test.title, ":", test.suite.length, "tests");
const results = await test.run();
prettify(results);
if (passed) passed = results.every((test) => test.passed);
}
});
}
return passed;
}
runTests("./tests");
runTests("./tests").then((passed) => {
if (passed) return console.log(`All tests have passed`);
console.error("Some tests have failed");
process.exit(-1);
});
6 changes: 6 additions & 0 deletions tests/Test.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ const test = new Test("Test");

test.it("properly runs tests", async () => {
const tmp = new Test("tmp-test");
tmp.before(() => {
console.log("Before test");
});
tmp.after(() => {
console.log("After test");
});
tmp.it("passed", () => {});
tmp.it("failed", () => {
throw new Error("failed");
Expand Down

0 comments on commit a8612fb

Please sign in to comment.