Skip to content

Commit

Permalink
test: added jest and some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Kos-M committed Sep 25, 2022
1 parent 270544f commit c30ae86
Show file tree
Hide file tree
Showing 6 changed files with 1,712 additions and 20 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module.exports = {
commonjs: true,
es2021: true,
node: true,
jest: true,
},
extends: ['airbnb-base', 'prettier'],
overrides: [],
Expand Down
1 change: 1 addition & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
. "$(dirname -- "$0")/_/husky.sh"

yarn lint-staged
CI=true yarn run test
61 changes: 61 additions & 0 deletions __tests__/index.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
const mockeData = require('./utils');
const Master = require('../index');

process.env.transferEncryptToken = '00000000000000000000000000000000';
process.env.token = 'demoCHannel0';
process.env.LOG_LEVEL = 'off';

let master;
beforeEach(() => {
master = new Master();
});

describe('pushNewJob method', () => {
it('Reject push job if not pass payload', async () => {
await master.pushNewJob().catch((e) => {
expect(e.message).toBe('payload is undefined');
});
});
it('Push job succesfully and enrcypted , if job is object', async () => {
const encryptedPayload = master.crypt.encrypt(
JSON.stringify(mockeData.payloadObject)
);
await master.pushNewJob(mockeData.payloadObject);

expect(encryptedPayload).toHaveProperty('iv');
expect(encryptedPayload).toHaveProperty('encryptedData');
expect(master.jobs).toHaveLength(1);
expect(JSON.parse(master.jobs[0])).toEqual(encryptedPayload);
});
it('Push job succesfully and enrcypted , if job is array', async () => {
const encryptedPayload = master.crypt.encrypt(
JSON.stringify(mockeData.payloadArray)
);
await master.pushNewJob(mockeData.payloadArray);

expect(encryptedPayload).toHaveProperty('iv');
expect(encryptedPayload).toHaveProperty('encryptedData');
expect(master.jobs).toHaveLength(1);
expect(JSON.parse(master.jobs[0])).toEqual(encryptedPayload);
});
it('Push job succesfully and enrcypted , if job is string', async () => {
const jobData = '50';
const encryptedPayload = master.crypt.encrypt(jobData);
await master.pushNewJob(jobData);

expect(encryptedPayload).toHaveProperty('iv');
expect(encryptedPayload).toHaveProperty('encryptedData');
expect(master.jobs).toHaveLength(1);
expect(JSON.parse(master.jobs[0])).toEqual(encryptedPayload);
});
it('Push job succesfully and enrcypted , if job is number', async () => {
const jobData = 50;
const encryptedPayload = master.crypt.encrypt(jobData);
await master.pushNewJob(jobData);

expect(encryptedPayload).toHaveProperty('iv');
expect(encryptedPayload).toHaveProperty('encryptedData');
expect(master.jobs).toHaveLength(1);
expect(JSON.parse(master.jobs[0])).toEqual(encryptedPayload);
});
});
10 changes: 10 additions & 0 deletions __tests__/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = {
payloadObject: {
a: 1,
b: 2,
},
payloadArray: [
{ a: 1, b: 2 },
{ a: 10, b: 20 },
],
};
10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"description": "Master queue, push jobs and gathers results from online workers.",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"test": "jest --forceExit",
"test:dev": "jest --watch ",
"lint": "eslint --ext .js,.ts .",
"format": "prettier --write .",
"prepare": "husky install"
Expand All @@ -23,11 +24,13 @@
"prompts": "^2.4.1"
},
"devDependencies": {
"@types/jest": "^29.0.3",
"cz-conventional-changelog": "^3.3.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-import": "^2.25.2",
"husky": "^8.0.1",
"jest": "^29.0.3",
"lint-staged": "^13.0.3",
"prettier": "2.7.1"
},
Expand All @@ -40,5 +43,10 @@
"hooks": {
"prepare-commit-msg": "exec < /dev/tty && npx cz --hook || true"
}
},
"jest": {
"modulePathIgnorePatterns": [
"utils"
]
}
}
Loading

0 comments on commit c30ae86

Please sign in to comment.