Skip to content

Commit

Permalink
Add tests!!! Thank you sinon! Screw you jest!
Browse files Browse the repository at this point in the history
  • Loading branch information
Arrow7000 committed Jul 28, 2022
1 parent 6519e08 commit 693b29d
Show file tree
Hide file tree
Showing 4 changed files with 281 additions and 8 deletions.
5 changes: 5 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
};
13 changes: 11 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"start": "microbundle watch",
"build": "microbundle",
"watch": "tsc -w",
"prepublish": "npm run build"
"prepublish": "npm run build",
"test": "jest"
},
"keywords": [
"queue",
Expand All @@ -26,7 +27,15 @@
},
"license": "MIT",
"repository": "github:Arrow7000/qew",
"packageManager": "[email protected]",
"devDependencies": {
"microbundle": "^0.11.0"
"@sinonjs/fake-timers": "^9.1.2",
"@types/jest": "^28.1.6",
"@types/node": "^18.6.1",
"@types/sinonjs__fake-timers": "^8.1.2",
"jest": "^28.1.3",
"microbundle": "^0.15.0",
"ts-jest": "^28.0.7",
"typescript": "^4.7.4"
}
}
249 changes: 249 additions & 0 deletions qew.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,249 @@
import * as sinon from "@sinonjs/fake-timers";
import { Qew } from "./qew";

const callAfter = (ms: number, cb: () => void) =>
new Promise((res) => setTimeout(() => res(cb()), ms));

/**
* Because Jest's fake timers are accursed and we shall never speak of them again.
*/
let clock: sinon.InstalledClock;

const advanceClock = async (ms: number) => clock.tickAsync(ms);

beforeAll(() => {
clock = sinon.install();
});

beforeEach(() => {
clock.reset();
});

afterAll(() => {
clock.uninstall();
});

it("Test that qew can run a single function", async () => {
const qew = new Qew();

const fn = jest.fn(() => 1);
const oneSecFunc = () => callAfter(1000, fn);

const prom = qew.push(oneSecFunc);

expect(fn).toHaveBeenCalledTimes(0);

await advanceClock(999);
expect(fn).toHaveBeenCalledTimes(0);

await advanceClock(1);
expect(fn).toHaveBeenCalledTimes(1);

await advanceClock(1000);
expect(fn).toHaveBeenCalledTimes(1);

expect(prom).resolves.toBe(1);
});

it("Test singlethreaded qew with a delay", async () => {
const qew = new Qew(1, 500);

const fn1 = jest.fn(() => 1);
const fn2 = jest.fn(() => 2);

const prom1 = qew.push(() => callAfter(1000, fn1));
const prom2 = qew.push(() => callAfter(1000, fn2));

expect(fn1).toHaveBeenCalledTimes(0);
expect(fn2).toHaveBeenCalledTimes(0);

await advanceClock(999);
expect(fn1).toHaveBeenCalledTimes(0);
expect(fn2).toHaveBeenCalledTimes(0);

await advanceClock(1);
expect(fn1).toHaveBeenCalledTimes(1);
expect(fn2).toHaveBeenCalledTimes(0);

await advanceClock(500);
expect(fn1).toHaveBeenCalledTimes(1);
expect(fn2).toHaveBeenCalledTimes(0);

await advanceClock(999);
expect(fn1).toHaveBeenCalledTimes(1);
expect(fn2).toHaveBeenCalledTimes(0);

await advanceClock(1);
expect(fn1).toHaveBeenCalledTimes(1);
expect(fn2).toHaveBeenCalledTimes(1);

await advanceClock(5000);
expect(fn1).toHaveBeenCalledTimes(1);
expect(fn2).toHaveBeenCalledTimes(1);

expect(prom1).resolves.toBe(1);
expect(prom2).resolves.toBe(2);
});

it("Test qew with max concurrent tasks of 2 and no delay", async () => {
const qew = new Qew(2);

const fn1 = jest.fn(() => 1);
const fn2 = jest.fn(() => 2);
const fn3 = jest.fn(() => 3);
const fn4 = jest.fn(() => 4);

const prom1 = qew.push(() => callAfter(1000, fn1));
const prom2 = qew.push(() => callAfter(1000, fn2));
const prom3 = qew.push(() => callAfter(1000, fn3));
const prom4 = qew.push(() => callAfter(1000, fn4));

expect(fn1).toHaveBeenCalledTimes(0);
expect(fn2).toHaveBeenCalledTimes(0);
expect(fn3).toHaveBeenCalledTimes(0);
expect(fn4).toHaveBeenCalledTimes(0);

await advanceClock(999);
expect(fn1).toHaveBeenCalledTimes(0);
expect(fn2).toHaveBeenCalledTimes(0);
expect(fn3).toHaveBeenCalledTimes(0);
expect(fn4).toHaveBeenCalledTimes(0);

await advanceClock(1);
expect(fn1).toHaveBeenCalledTimes(1);
expect(fn2).toHaveBeenCalledTimes(1);
expect(fn3).toHaveBeenCalledTimes(0);
expect(fn4).toHaveBeenCalledTimes(0);

await advanceClock(999);
expect(fn1).toHaveBeenCalledTimes(1);
expect(fn2).toHaveBeenCalledTimes(1);
expect(fn3).toHaveBeenCalledTimes(0);
expect(fn4).toHaveBeenCalledTimes(0);

// Hmm this is odd. 1ms should definitely be enough but maybe internally the promises need extra 1ms to resolve? Idk.
await advanceClock(2);
expect(fn1).toHaveBeenCalledTimes(1);
expect(fn2).toHaveBeenCalledTimes(1);
expect(fn3).toHaveBeenCalledTimes(1);
expect(fn4).toHaveBeenCalledTimes(1);

await advanceClock(5000);
expect(fn1).toHaveBeenCalledTimes(1);
expect(fn2).toHaveBeenCalledTimes(1);
expect(fn3).toHaveBeenCalledTimes(1);
expect(fn4).toHaveBeenCalledTimes(1);

expect(prom1).resolves.toBe(1);
expect(prom2).resolves.toBe(2);
expect(prom3).resolves.toBe(3);
expect(prom4).resolves.toBe(4);
});

it("Test qew with max concurrent tasks of 2 and a delay", async () => {
const qew = new Qew(2, 500);

const fn1 = jest.fn(() => 1);
const fn2 = jest.fn(() => 2);
const fn3 = jest.fn(() => 3);
const fn4 = jest.fn(() => 4);

const prom1 = qew.push(() => callAfter(1000, fn1));
const prom2 = qew.push(() => callAfter(1000, fn2));
const prom3 = qew.push(() => callAfter(1000, fn3));
const prom4 = qew.push(() => callAfter(1000, fn4));

expect(fn1).toHaveBeenCalledTimes(0);
expect(fn2).toHaveBeenCalledTimes(0);
expect(fn3).toHaveBeenCalledTimes(0);
expect(fn4).toHaveBeenCalledTimes(0);

await advanceClock(999);
expect(fn1).toHaveBeenCalledTimes(0);
expect(fn2).toHaveBeenCalledTimes(0);
expect(fn3).toHaveBeenCalledTimes(0);
expect(fn4).toHaveBeenCalledTimes(0);

await advanceClock(1);
expect(fn1).toHaveBeenCalledTimes(1);
expect(fn2).toHaveBeenCalledTimes(1);
expect(fn3).toHaveBeenCalledTimes(0);
expect(fn4).toHaveBeenCalledTimes(0);

await advanceClock(1499);
expect(fn1).toHaveBeenCalledTimes(1);
expect(fn2).toHaveBeenCalledTimes(1);
expect(fn3).toHaveBeenCalledTimes(0);
expect(fn4).toHaveBeenCalledTimes(0);

await advanceClock(1);
expect(fn1).toHaveBeenCalledTimes(1);
expect(fn2).toHaveBeenCalledTimes(1);
expect(fn3).toHaveBeenCalledTimes(1);
expect(fn4).toHaveBeenCalledTimes(1);

await advanceClock(5000);
expect(fn1).toHaveBeenCalledTimes(1);
expect(fn2).toHaveBeenCalledTimes(1);
expect(fn3).toHaveBeenCalledTimes(1);
expect(fn4).toHaveBeenCalledTimes(1);

expect(prom1).resolves.toBe(1);
expect(prom2).resolves.toBe(2);
expect(prom3).resolves.toBe(3);
expect(prom4).resolves.toBe(4);
});

it("Test qew with max concurrent tasks of 3, a delay and functions that run for different amounts of time", async () => {
const qew = new Qew(3, 100);

const fn1 = jest.fn(() => 1);
const fn2 = jest.fn(() => 2);
const fn3 = jest.fn(() => 3);
const fn4 = jest.fn(() => 4);

const prom1 = qew.push(() => callAfter(1000, fn1));
const prom2 = qew.push(() => callAfter(100, fn2));
const prom3 = qew.push(() => callAfter(800, fn3));
const prom4 = qew.push(() => callAfter(300, fn4));

expect(fn1).toHaveBeenCalledTimes(0);
expect(fn2).toHaveBeenCalledTimes(0);
expect(fn3).toHaveBeenCalledTimes(0);
expect(fn4).toHaveBeenCalledTimes(0);

await advanceClock(100);
expect(fn1).toHaveBeenCalledTimes(0);
expect(fn2).toHaveBeenCalledTimes(1);
expect(fn3).toHaveBeenCalledTimes(0);
expect(fn4).toHaveBeenCalledTimes(0);

await advanceClock(400);
expect(fn1).toHaveBeenCalledTimes(0);
expect(fn2).toHaveBeenCalledTimes(1);
expect(fn3).toHaveBeenCalledTimes(0);
expect(fn4).toHaveBeenCalledTimes(1);

await advanceClock(300);
expect(fn1).toHaveBeenCalledTimes(0);
expect(fn2).toHaveBeenCalledTimes(1);
expect(fn3).toHaveBeenCalledTimes(1);
expect(fn4).toHaveBeenCalledTimes(1);

await advanceClock(200);
expect(fn1).toHaveBeenCalledTimes(1);
expect(fn2).toHaveBeenCalledTimes(1);
expect(fn3).toHaveBeenCalledTimes(1);
expect(fn4).toHaveBeenCalledTimes(1);

await advanceClock(5000);
expect(fn1).toHaveBeenCalledTimes(1);
expect(fn2).toHaveBeenCalledTimes(1);
expect(fn3).toHaveBeenCalledTimes(1);
expect(fn4).toHaveBeenCalledTimes(1);

expect(prom1).resolves.toBe(1);
expect(prom2).resolves.toBe(2);
expect(prom3).resolves.toBe(3);
expect(prom4).resolves.toBe(4);
});
22 changes: 16 additions & 6 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,19 @@
"compilerOptions": {
/* Basic Options */
// "incremental": true, /* Enable incremental compilation */
"target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */
"module": "ES2015", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
// "lib": [], /* Specify library files to be included in the compilation. */
"target": "es5" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */,
"module": "CommonJS" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */,
"lib": [
"DOM",
"ES2015",
"ES2016",
"ES2017",
"ES2018",
"ES2019",
"ES2020",
"ES2021",
"ES2022"
] /* Specify library files to be included in the compilation. */,
// "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
Expand All @@ -23,7 +33,7 @@
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */

/* Strict Type-Checking Options */
"strict": true, /* Enable all strict type-checking options. */
"strict": true /* Enable all strict type-checking options. */,
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
// "strictNullChecks": true, /* Enable strict null checks. */
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
Expand All @@ -46,7 +56,7 @@
// "typeRoots": [], /* List of folders to include type definitions from. */
// "types": [], /* Type declaration files to be included in compilation. */
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */

Expand All @@ -61,6 +71,6 @@
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */

/* Advanced Options */
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
}
}

0 comments on commit 693b29d

Please sign in to comment.