Skip to content

Commit

Permalink
Merge pull request #4 from ugurcanatas/feat/add-jest-conf
Browse files Browse the repository at this point in the history
feat: add jest and test file
  • Loading branch information
ugurcanatas authored Sep 14, 2024
2 parents 87b1054 + 3707829 commit b757d15
Show file tree
Hide file tree
Showing 7 changed files with 2,345 additions and 92 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
with:
node-version: 20
- run: npm install
# - run: yarn test
- run: npm test

publish-npm:
needs: build
Expand Down
4 changes: 3 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
node_modules
tsconfig.json
src
.gitignore
.gitignore
.github
__tests__
56 changes: 56 additions & 0 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { TinyProxy } from "../src/index";

describe("Timeout Proxy", () => {
beforeEach(() => {
jest.useFakeTimers({
advanceTimers: true,
});
});
afterEach(() => {
jest.useRealTimers();
});
test("should confirm if setTimeout is being called", () => {
const p = new TinyProxy({
shouldLog: true,
});
p.setupTimerProxy();
const spySetTimeout = jest.spyOn(global, "setTimeout");

const timerId = setTimeout(() => {}, 100);

// Check if setTimeout was called and got the correct ID
expect(spySetTimeout).toHaveBeenCalledTimes(1);
expect(spySetTimeout).toHaveBeenCalledWith(expect.any(Function), 100);

// Advance timers
jest.advanceTimersByTime(100);

// Verify the timer is in the activeTimeouts
expect(p.activeTimeouts.has(timerId)).toBe(true);
expect(p.activeTimeouts.size).toBe(1);
});

test("should confirm if clearTimeout is being called", () => {
const p = new TinyProxy({
shouldLog: true,
});
p.setupTimerProxy();
const spyClearTimeout = jest.spyOn(global, "clearTimeout");

const timerId = setTimeout(() => {}, 100);

expect(spyClearTimeout).toHaveBeenCalledTimes(0);

// Advance timers
jest.advanceTimersByTime(100);

// Verify the timer is in the activeTimeouts
expect(p.activeTimeouts.has(timerId)).toBe(true);
expect(p.activeTimeouts.size).toBe(1);

clearTimeout(timerId);

expect(p.activeTimeouts.has(timerId)).toBe(false);
expect(p.activeTimeouts.size).toBe(0);
});
});
7 changes: 7 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/** @type {import('ts-jest').JestConfigWithTsJest} **/
module.exports = {
testEnvironment: "node",
transform: {
"^.+.tsx?$": ["ts-jest",{}],
},
};
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
{
"name": "tiny-timer-proxy",
"version": "0.1.1",
"version": "0.2.0",
"description": "A simple proxy class for timers and intervals",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"build": "tsc",
"start": "ts-node src/index.ts"
"start": "ts-node src/index.ts",
"test": "jest"
},
"files": [
"dist/**/*"
],
"author": "Ugurcan Emre Atas",
"license": "MIT",
"devDependencies": {
"@types/jest": "^29.5.13",
"@types/node": "^22.5.4",
"jest": "^29.7.0",
"ts-jest": "^29.2.5",
"ts-node": "^10.9.2",
"typescript": "^5.6.2"
}
Expand Down
4 changes: 3 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,5 +106,7 @@
/* Completeness */
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
"skipLibCheck": true /* Skip type checking all .d.ts files. */
}
},
"include": ["src/**/*.ts"], // Includes test files
"exclude": ["node_modules", "tests/**/*.ts"]
}
Loading

0 comments on commit b757d15

Please sign in to comment.