Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add example Unit Test for IncrementCounter for streamdeck create's example project #91

Open
k0pernikus opened this issue Feb 20, 2025 · 1 comment

Comments

@k0pernikus
Copy link

Feature Request for improving Developer Experience

Adding an example Unit Test for IncrementCounter on streamdeck create


The example project is working, yet I have troubles getting a test workflow up and running. I tried unsuccessfully adding mocha / chai yet, yet I don't mind if you want to default to jest or something completely different. Having a specific and even locked-in test setup out of the box is better than having none.

Hence it be great if the cli on streamdeck create would also generates an example unit test of IncrementCounter with basic assertion on onWillAppear and onKeyDown, and that there is also a a test command in the package.json

"scripts": {
        "build": "rollup -c",
        "watch": "rollup -c -w --watch.onEnd=\"streamdeck restart com.kopernikus.odoo-time-tracker\""
        "test": ... // this should be added
    },

While I have background in nodejs and TypeScript development, I have never used rollup before and I find its configuration weird and I don't know what configuration to adapt and how to have my tests be executed. Should my tests files be transpiled into the same folder as my src files? How to run them?

Due to that: Any development setup that is already provided for me by the streamdeck sdk cli to adapt and continue would ease the friction and I can focus on actually implementing my plugin that setting up my project. I really appreciate it.

@ChekTek
Copy link
Member

ChekTek commented Feb 25, 2025

Hi @k0pernikus,

Great suggestion! We will look into adding this to the output of the CLI tool. In the mean time, you can set up tests in your plugin project with the following steps.

  1. Update your tsconfig.json file to include a test folder.
    "include": [
        "src/**/*.ts",
        "tests/**/*.ts"
    ],
  1. Install jest
npm install --save-dev jest @types/jest ts-jest
  1. Create a jest.config.cjs file in your projects root directory.
module.exports = {
	preset: "ts-jest",
	testEnvironment: "node",
	moduleFileExtensions: ["ts", "js"],
	testMatch: ["**/*.test.ts"],
	testMatch: ["**/tests/**/*.test.ts"],
	roots: ["<rootDir>/src", "<rootDir>/tests"],
};
  1. Create a test in a tests folder in your projects root directory. i.e. tests/utils.test.ts
import { clamp } from "../src/utils";

describe("utils.ts", () => {
	describe("clamp", () => {
		test("should return min when value is less than min", () => {
			expect(clamp(2, 5, 10)).toBe(5);
		});
		test("should return max when value is greater than max", () => {
			expect(clamp(12, 5, 10)).toBe(10);
		});
		test("should return value when value is between min and max", () => {
			expect(clamp(7, 5, 10)).toBe(7);
		});
	});
});
  1. Update the your package.json.
	"scripts": {
		"test": "jest"
	},
  1. Run npm run test.

Some of the test configuration is subjective, if you'd prefer your tests in the src folder or to call your test files .spec.ts, you can make config adjustments accordingly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants