Skip to content

Commit 0924bdc

Browse files
committed
Add tests
Change-type: patch
1 parent 7db5e58 commit 0924bdc

4 files changed

Lines changed: 113 additions & 6 deletions

File tree

package.json

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22
"name": "blinking",
33
"version": "0.0.8",
44
"description": "A module for \"blinking\" to a file (useful for writing to an LED file for actual blinks)",
5+
"type": "commonjs",
56
"main": "dist/index.js",
67
"types": "dist/index.d.ts",
78
"scripts": {
8-
"prepare": "npx tsc",
9+
"prepare": "npx tsc --project tsconfig.build.json",
910
"lint": "balena-lint src",
1011
"lint-fix": "balena-lint --fix src",
11-
"test": "npm run lint && npm run prepare"
12+
"pretest": "npm run prepare",
13+
"test": "mocha && npm run lint && npx tsc --noEmit"
1214
},
1315
"repository": {
1416
"type": "git",
@@ -22,9 +24,25 @@
2224
},
2325
"devDependencies": {
2426
"@balena/lint": "^8.0.2",
27+
"@types/chai": "^4.3.16",
28+
"@types/mocha": "^10.0.7",
29+
"@types/mock-fs": "^4.13.4",
2530
"@types/node": "^16.18.96",
31+
"@types/sinon": "^17.0.3",
32+
"chai": "^4.5.0",
33+
"mocha": "^10.7.0",
34+
"mock-fs": "^5.2.0",
35+
"sinon": "^18.0.0",
36+
"ts-node": "^10.9.2",
2637
"typescript": "^5.4.5"
2738
},
39+
"mocha": {
40+
"reporter": "spec",
41+
"recursive": true,
42+
"require": "ts-node/register/transpile-only",
43+
"bail": true,
44+
"_": "test/*"
45+
},
2846
"versionist": {
2947
"publishedAt": "2024-04-29T15:19:34.661Z"
3048
}

test/index.ts

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import { expect } from 'chai';
2+
import * as mockFs from 'mock-fs';
3+
import { useFakeTimers, SinonFakeTimers } from 'sinon';
4+
import { promises as fs } from 'fs';
5+
6+
import blinking = require('..');
7+
8+
describe('blinking', () => {
9+
const LED_FILE_PATH = '/sys/class/leds/led0/brightness';
10+
let clock: SinonFakeTimers;
11+
const blink = blinking(LED_FILE_PATH);
12+
13+
const expectValue = async (path: string, expected: string) =>
14+
expect(await fs.readFile(path, 'utf-8')).to.equal(expected);
15+
const expectLedTurnedOn = async () => await expectValue(LED_FILE_PATH, '1');
16+
const expectLedTurnedOff = async () => await expectValue(LED_FILE_PATH, '0');
17+
18+
beforeEach(() => {
19+
clock = useFakeTimers();
20+
mockFs({
21+
[LED_FILE_PATH]: '0',
22+
});
23+
});
24+
25+
afterEach(() => {
26+
mockFs.restore();
27+
clock.restore();
28+
});
29+
30+
it('blinks with default pattern', async () => {
31+
blink.pattern.start();
32+
await clock.tickAsync(1);
33+
await expectLedTurnedOn();
34+
await clock.tickAsync(200);
35+
await expectLedTurnedOff();
36+
blink.pattern.stop();
37+
});
38+
39+
it('cancels blink on calling stop', async () => {
40+
blink.pattern.start();
41+
await clock.tickAsync(1);
42+
await expectLedTurnedOn();
43+
blink.pattern.stop();
44+
await clock.tickAsync(1);
45+
await expectLedTurnedOff();
46+
});
47+
48+
it('blinks with custom pattern', async () => {
49+
const pattern = {
50+
blinks: 3,
51+
onDuration: 500,
52+
offDuration: 300,
53+
pause: 100,
54+
};
55+
const expectPattern = async () => {
56+
for (let i = 0; i < pattern.blinks; i++) {
57+
await clock.tickAsync(1);
58+
// 1 ms
59+
await expectLedTurnedOn();
60+
await clock.tickAsync(pattern.onDuration - 2);
61+
// 499 ms
62+
await expectLedTurnedOn();
63+
await clock.tickAsync(1);
64+
// 500 ms
65+
await expectLedTurnedOff();
66+
await clock.tickAsync(pattern.offDuration - 1);
67+
// 799 ms
68+
await expectLedTurnedOff();
69+
await clock.tickAsync(1);
70+
// 800 ms
71+
}
72+
};
73+
blink.pattern.start(pattern);
74+
await expectPattern();
75+
// Should pause between blink sequences
76+
await clock.tickAsync(pattern.pause);
77+
await expectPattern();
78+
blink.pattern.stop();
79+
});
80+
});

tsconfig.build.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"compilerOptions": {
4+
"sourceMap": false,
5+
"outDir": "./dist",
6+
},
7+
"include": [
8+
"src/**/*"
9+
]
10+
}

tsconfig.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@
55
"strict": true,
66
"removeComments": false,
77
"preserveConstEnums": true,
8-
"sourceMap": false,
98
"target": "es2015",
109
"noUnusedParameters": true,
1110
"noUnusedLocals": true,
12-
"moduleResolution": "node",
13-
"outDir": "./dist",
11+
"moduleResolution": "node"
1412
},
1513
"include": [
16-
"src/index.ts"
14+
"src/**/*",
15+
"test/**/*"
1716
]
1817
}

0 commit comments

Comments
 (0)