Unit tests (Jest) (#411)

* adding unit testing to the node project

* added to git workflow

* added some more coverage and updated workflow so the label is correct

* added some more coverage and updated workflow so the label is correct

* remove unused dependency

* added new test file for json
This commit is contained in:
kevkevin 2022-08-23 11:56:03 -06:00 committed by GitHub
parent 5a21208e4f
commit 905717edb8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 147 additions and 15 deletions

19
.github/workflows/unit_test.yml vendored Normal file
View file

@ -0,0 +1,19 @@
name: unit-test (Pull Request)
on:
pull_request:
branches:
- master
jobs:
lint:
name: Lint (Pull Request)
runs-on:
- ubuntu-latest
steps:
- name: checkout
uses: actions/checkout@v2
- name: install npm dependencies
run: npm i
- name: run tests
run: npm run test-unit

9
jest.config.js Normal file
View file

@ -0,0 +1,9 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
transform: {
'^.+\\.ts?$': 'ts-jest',
},
transformIgnorePatterns: ['<rootDir>/node_modules/'],
collectCoverage: true,
}

View file

@ -3,7 +3,9 @@
"version": "1.0.0",
"description": "",
"main": "dist/app.js",
"test": "jest",
"scripts": {
"test-unit": "jest ./src/tests/unit_tests",
"test": "ava",
"docker-dev": "nodemon ./app.js",
"build": "tsc",
@ -22,16 +24,9 @@
"author": "",
"license": "ISC",
"ava": {
"files": [
"!src/tests/utils/**",
"!src/tests/types"
],
"extensions": [
"ts"
],
"require": [
"ts-node/register"
]
"files": ["!src/tests/utils/**", "!src/tests/types"],
"extensions": ["ts"],
"require": ["ts-node/register"]
},
"dependencies": {
"@boltz/bolt11": "^1.2.7",
@ -64,6 +59,7 @@
"helmet": "^3.21.1",
"ip": "^1.1.5",
"jasmine": "^3.5.0",
"jest": "^28.1.3",
"js-sha256": "^0.9.0",
"jscryptor-2": "0.0.1",
"lodash": "^4.17.21",
@ -99,6 +95,7 @@
"sphinx-bot": "^0.2.21",
"sqlite3": "4.1.1",
"tail": "^2.0.3",
"ts-jest": "^28.0.8",
"ts-node": "^8.5.4",
"typescript": "^4.1.3",
"underscore": "^1.13.1",
@ -111,6 +108,7 @@
"@types/cron": "^2.0.0",
"@types/crypto-js": "^4.1.1",
"@types/ip": "^1.1.0",
"@types/jest": "^28.1.7",
"@types/md5": "^2.3.2",
"@types/minimist": "^1.2.2",
"@types/multer": "^1.4.7",
@ -132,10 +130,7 @@
"socket.io-client-legacy": "npm:socket.io-client@^2.4.0"
},
"pkg": {
"assets": [
"config/*",
"dist/config/*"
]
"assets": ["config/*", "dist/config/*"]
},
"husky": {
"hooks": {

View file

@ -0,0 +1,24 @@
import { toSnake, toCamel } from '../../utils/case'
describe('tests for src/utils/case', () => {
const notSnakeObj = {
superCamelCase: 20,
nextValueHere: 'words',
}
const correct_snaked_string = {
super_camel_case: 20,
next_value_here: 'words',
}
test('toSnake', async () => {
const snaked_string = toSnake(notSnakeObj)
expect(snaked_string).toStrictEqual(correct_snaked_string)
})
test('toCamel', async () => {
const camel_string = toCamel(correct_snaked_string)
expect(camel_string).toStrictEqual(notSnakeObj)
})
})

View file

@ -0,0 +1,85 @@
import { messageToJson } from '../../utils/json'
describe('tests for src/utils/json.ts', () => {
const currentTime = new Date('2022-08-22T17:49:42.727Z').toISOString()
const message = {
id: 0,
uuid: '0',
chatId: 0,
type: 0,
sender: 0,
receiver: 0,
amount: 0,
amountMsat: 0,
paymentHash: '',
paymentRequest: '',
date: currentTime,
expirationDate: currentTime,
messageContent: '',
remoteMessageContent: '',
status: 0,
statusMap: '',
parentId: 0,
subscriptionId: 0,
mediaKey: '',
mediaType: '',
mediaToken: '',
seen: false,
createdAt: currentTime,
updatedAt: currentTime,
senderAlias: '', // for tribes, no "sender" id maybe
senderPic: '', // for tribes, no "sender" id maybe
originalMuid: '', // for tribe, remember the og muid
replyUuid: '',
network_type: 0,
tenant: 0,
recipientAlias: '', // for direct payment display in tribes
recipientPic: '', // for direct payment display in tribes
forwardedSats: false,
}
const messageConvertedToJson = {
amount: 0,
amount_msat: 0,
chat: null,
chat_id: 0,
contact: null,
created_at: currentTime,
date: currentTime,
expiration_date: currentTime,
forwarded_sats: false,
id: 0,
media_key: '',
media_token: '',
media_type: '',
message_content: '',
network_type: 0,
original_muid: '',
parent_id: 0,
payment_hash: '',
payment_request: '',
receiver: 0,
recipient_alias: '',
recipient_pic: '',
remote_message_content: '',
reply_uuid: '',
seen: 0,
sender: 0,
sender_alias: '',
sender_pic: '',
status: 0,
status_map: null,
subscription_id: 0,
tenant: 0,
type: 0,
updated_at: currentTime,
uuid: '0',
}
test('sendContactKeys', () => {
const messageInJson = messageToJson(message)
expect(messageInJson).toStrictEqual(messageConvertedToJson)
})
})

View file

@ -1,7 +1,7 @@
{
"compileOnSave": false,
"compilerOptions": {
"types": ["node"],
"types": ["node", "jest"],
"outDir": "dist",
"allowJs": true,
"target": "es6",