Browse Source

Added testing tools

master 1.0.0
Michaël Lemaire 1 year ago
parent
commit
48308c4f55
  1. 2
      .editorconfig
  2. 21
      normalize.ts
  3. 78
      testing.test.ts
  4. 40
      testing.ts

2
.editorconfig

@ -1,6 +1,6 @@
root = true
[*.ts]
[*.{ts,json}]
charset = utf-8
end_of_line = lf
insert_final_newline = true

21
normalize.ts

@ -37,7 +37,7 @@ async function updateEditorConfig() {
".editorconfig",
`root = true
[*.ts]
[*.{ts,json}]
charset = utf-8
end_of_line = lf
insert_final_newline = true
@ -74,6 +74,25 @@ async function updateVscodeConf() {
config["deno.enable"] = true;
await Deno.writeTextFile(path, JSON.stringify(config));
}
await Deno.writeTextFile(
".vscode/tasks.json",
`{
"version": "2.0.0",
"tasks": [
{
"label": "test",
"type": "shell",
"group": {
"kind": "test",
"isDefault": true
},
"command": "deno test"
}
]
}
`,
);
}
async function updateGitIgnore() {

78
testing.test.ts

@ -0,0 +1,78 @@
import { it, expect, patch } from "./testing.ts";
class Obj {
called = 0;
func(x: number) {
this.called += 1;
return x * 2;
}
}
function checkRestored(o: Obj) {
const called = o.called;
const result = o.func(4);
expect(o.called).toEqual(called + 1);
expect(result).toEqual(8);
}
it("patch with call-through", () => {
const o = new Obj();
const mock = patch(o, "func");
mock.exec(() => {
const result = o.func(3);
expect(o.called).toEqual(1);
expect(result).toEqual(6);
expect(mock.fn).toHaveBeenCalledTimes(1);
expect(mock.fn).toHaveBeenCalledWith(3);
});
mock.exec(() => {
const result = o.func(1);
expect(o.called).toEqual(2);
expect(result).toEqual(2);
expect(mock.fn).toHaveBeenCalledTimes(1);
expect(mock.fn).toHaveBeenCalledWith(1);
});
checkRestored(o);
});
it("patch with single looping stub", () => {
const o = new Obj();
const mock = patch(o, "func", (a) => a * 3);
mock.exec(() => {
let result = o.func(2);
expect(result).toEqual(6);
result = o.func(2);
expect(result).toEqual(6);
result = o.func(2);
expect(result).toEqual(6);
expect(o.called).toEqual(0);
expect(mock.fn).toHaveBeenCalledTimes(3);
});
checkRestored(o);
});
it("patch with stub sequence", () => {
const o = new Obj();
const mock = patch(o, "func", (a) => a * 3, (a) => a * 4);
mock.exec(() => {
let result = o.func(2);
expect(result).toEqual(6);
result = o.func(2);
expect(result).toEqual(8);
result = o.func(2);
expect(result).toBeUndefined();
expect(o.called).toEqual(0);
expect(mock.fn).toHaveBeenCalledTimes(3);
});
checkRestored(o);
});

40
testing.ts

@ -0,0 +1,40 @@
// Testing tools
export {
it,
expect,
} from "https://raw.githubusercontent.com/allain/expect/9effa6c6da3bcf4b66114b44e6b1662e85c91337/mod.ts";
import {
mock,
} from "https://raw.githubusercontent.com/allain/expect/9effa6c6da3bcf4b66114b44e6b1662e85c91337/mod.ts";
export type PatchResult = {
exec(body: Function): void;
fn: Function;
};
export const test = Deno.test;
export function patch<
T extends Object,
K extends keyof T,
F extends T[K] & Function,
>(
obj: T,
method: K,
...stubs: F[]
): PatchResult {
const result = {
fn: () => {},
exec(body: Function) {
const orig = obj[method] as unknown as Function;
result.fn = mock.fn(...(stubs.length ? stubs : [orig.bind(obj)]));
Object.assign(obj, { [method]: result.fn });
try {
body();
} finally {
Object.assign(obj, { [method]: orig });
}
},
};
return result;
}
Loading…
Cancel
Save