Skip to content

Commit

Permalink
tests: convert to TS (#600)
Browse files Browse the repository at this point in the history
* tests: convert to TS

Convert tests to Typescript. Update CI workflow to install
types/mocha package used by tests.

* CONTRIBUTING.md: mention TypeScript

Mention using TypeScript in "Coding standards" section and
add guideline on "any" type.
  • Loading branch information
webwarrior-ws authored Oct 23, 2024
1 parent dbf81d7 commit 0c4d15a
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 84 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/integrate.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ jobs:
mongosh --eval 'db.getSiblingDB("lnp2pbot").createCollection("mycollection")'
- run: npm install @types/node @types/i18n
- run: npm install @types/node @types/i18n @types/mocha
- run: npx tsc
- run: npm ci
- name: Run tests
Expand Down
2 changes: 2 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ Please note that Pull Requests marked `NACK` and/or GitHub's `Change requested`

We ~~try to~~ use [Airbnb javascript style guide](https://github.com/airbnb/javascript) in order to have a cleaner code.

All new code should be TypeScript. The use of `any` type is discouraged, except in the tests.

### Configure Git user name and email metadata

See https://help.github.com/articles/setting-your-username-in-git/ for instructions.
Expand Down
34 changes: 17 additions & 17 deletions tests/bot/bot.spec.js → tests/bot/bot.spec.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
const path = require('path');
const fs = require('fs');
import path from 'path';
import fs from 'fs';
const sinon = require('sinon');
const { expect } = require('chai');
const proxyquire = require('proxyquire');

const { initialize } = require('../../bot');
const { User, Order } = require('../../models');
const { getCurrenciesWithPrice } = require('../../util');
const { mockUpdatesResponseForCurrencies } = require('./mocks/currenciesResponse');
const { mockUpdatesResponseForLanguages } = require('./mocks/languagesResponse');
import { User, Order } from '../../models';
import { getCurrenciesWithPrice } from '../../util';
import { mockUpdatesResponseForCurrencies } from './mocks/currenciesResponse';
import { mockUpdatesResponseForLanguages } from './mocks/languagesResponse';

describe('Telegram bot', () => {
const token = '123456';
let server;
let sandbox;
let server: any;
let sandbox: any;
let order, user;

beforeEach(() => {
Expand Down Expand Up @@ -194,7 +194,7 @@ describe('Telegram bot', () => {
const updates = await client.getUpdates();
expect(updates.ok).to.be.equal(true);
let flags = 0;
updates.result[0].message.reply_markup.inline_keyboard.forEach(flag => {
updates.result[0].message.reply_markup.inline_keyboard.forEach((flag: any) => {
flags += flag.length;
});
let langs = 0;
Expand All @@ -206,12 +206,12 @@ describe('Telegram bot', () => {
});

describe('Bot Initialization', () => {
let initialize;
let botStub;
let scheduleStub;
let validateUserStub;
let startMessageStub;
let attemptPendingPaymentsStub;
let initialize: any;
let botStub: any;
let scheduleStub: any;
let validateUserStub: any;
let startMessageStub: any;
let attemptPendingPaymentsStub: any;

beforeEach(() => {
botStub = {
Expand Down Expand Up @@ -501,7 +501,7 @@ describe('Bot Initialization', () => {
},
};

botStub.on = sinon.stub().callsFake((event, middleware, handler) => {
botStub.on = sinon.stub().callsFake((event: string, middleware: any, handler: any) => {
if (event === 'text') {
const capturedHandler = handler;
capturedHandler(ctx);
Expand Down Expand Up @@ -563,7 +563,7 @@ describe('Bot Initialization', () => {
},
};

botStub.on = sinon.stub().callsFake((event, middleware, handler) => {
botStub.on = sinon.stub().callsFake((event: string, middleware: any, handler: any) => {
if (event === 'text') {
const capturedHandler = handler;
capturedHandler(ctx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,6 @@ const mockUpdatesResponseForCurrencies = {
]
};

module.exports = {
export {
mockUpdatesResponseForCurrencies,
};
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ const mockUpdatesResponseForLanguages = {
}]
};

module.exports = {
export {
mockUpdatesResponseForLanguages,
};
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const mockTelegram = {
};

const mockI18n = {
t: sinon.stub((key, params) => {
t: sinon.stub((key: string, params: any) => {
switch (key) {
case 'dispute_started_channel':
return `User ${params.type} @${params.initiatorUser} TG ID: ${params.initiatorTgId}
Expand Down
Loading

0 comments on commit 0c4d15a

Please sign in to comment.