Skip to content

Commit

Permalink
HCK-9565: init unit tests automation config (#36)
Browse files Browse the repository at this point in the history
* HCK-9565: add base unit tests example using nodejs test runner

* prettify example tests and fix hooks installation

* move running tests to pre-push hook

* finalize running tests on pre-push hook
  • Loading branch information
VitaliiBedletskyi authored Jan 20, 2025
1 parent 87d5eb6 commit c8a6dc1
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 2 deletions.
1 change: 1 addition & 0 deletions buildConstants.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const EXCLUDED_FILES = [
'.prettierignore',
'.prettierrc',
'.dockerignore',
'test',
'build',
'release',
'node_modules',
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,13 @@
},
"simple-git-hooks": {
"pre-commit": "npx lint-staged",
"pre-push": "npx eslint ."
"pre-push": "npx eslint . && npm run test:unit"
},
"scripts": {
"lint": "eslint . --max-warnings=0",
"package": "node esbuild.package.js"
"test:unit": "node --experimental-test-module-mocks --test ./test/**/*.spec.js",
"package": "node esbuild.package.js",
"postinstall": "npx simple-git-hooks"
},
"devDependencies": {
"@hackolade/hck-esbuild-plugins-pack": "0.0.1",
Expand Down
11 changes: 11 additions & 0 deletions test/testExample/calculator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const { add, multiply } = require('./math');

function calculateSum(a, b) {
return add(a, b);
}

function calculateProduct(a, b) {
return multiply(a, b);
}

module.exports = { calculateSum, calculateProduct };
33 changes: 33 additions & 0 deletions test/testExample/calculator.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const { mock, before, describe, it } = require('node:test');
const assert = require('node:assert');

describe('calculator', () => {
let calculator;
let addSpy;
let multiplySpy;

before(() => {
addSpy = mock.fn(() => 42);
multiplySpy = mock.fn(() => -1);

mock.module('./math', {
namedExports: {
add: addSpy,
multiply: multiplySpy,
},
});
calculator = require('./calculator');
});

it('calculateSum uses mocked add function', () => {
const result = calculator.calculateSum(2, 3);
assert.strictEqual(result, 42);
assert.strictEqual(addSpy.mock.calls.length, 1);
});

it('calculateProduct uses mocked multiply function', () => {
const result = calculator.calculateProduct(2, 3);
assert.strictEqual(result, -1);
assert.strictEqual(multiplySpy.mock.calls.length, 1);
});
});
9 changes: 9 additions & 0 deletions test/testExample/math.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function add(a, b) {
return a + b;
}

function multiply(a, b) {
return a * b;
}

module.exports = { add, multiply };

0 comments on commit c8a6dc1

Please sign in to comment.