From 2edaf82725bb097d1d807c8398cc6ce3efc71c67 Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 21 Sep 2022 16:11:08 -0300 Subject: [PATCH 1/2] test: implement test base to login --- .eslintrc.json | 15 +++++- jest.config.js | 5 ++ package.json | 29 +++++------- src/botcity/maestro/sdk.ts | 6 ++- tests/index.ts | 96 -------------------------------------- tests/unit/login.test.ts | 76 ++++++++++++++++++++++++++++++ tsconfig.json | 4 +- 7 files changed, 112 insertions(+), 119 deletions(-) create mode 100644 jest.config.js delete mode 100644 tests/index.ts create mode 100644 tests/unit/login.test.ts diff --git a/.eslintrc.json b/.eslintrc.json index eb5c55c..e16cb34 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -1,9 +1,14 @@ { "env": { "browser": true, - "es2021": true + "es2021": true, + "jest/globals": true }, - "extends": "standard-with-typescript", + "parser": "@typescript-eslint/parser", + "extends": [ + "standard-with-typescript", + "plugin:jest/recommended" + ], "overrides": [ ], "parserOptions": { @@ -11,6 +16,12 @@ "sourceType": "module", "project": "tsconfig.json" }, + "plugins": ["jest"], "rules": { + "jest/no-disabled-tests": "warn", + "jest/no-focused-tests": "error", + "jest/no-identical-title": "error", + "jest/prefer-to-have-length": "warn", + "jest/valid-expect": "error" } } diff --git a/jest.config.js b/jest.config.js new file mode 100644 index 0000000..8cbf894 --- /dev/null +++ b/jest.config.js @@ -0,0 +1,5 @@ +/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */ +module.exports = { + preset: 'ts-jest', + testEnvironment: 'node', +}; \ No newline at end of file diff --git a/package.json b/package.json index 9aa33ec..33a28e9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@botcity/botcity-maestro-sdk", - "version": "0.2.2", + "version": "0.2.3", "description": "Botcity Maestro SDK", "author": "botcity", "main": "dist/index.js", @@ -9,23 +9,14 @@ "clean": "rimraf dist", "prebuild": "npm run clean", "build": "tsc", - "release": "release-it", - "lint": "eslint src/**/*.{js,ts,json}", - "lint:fix": "eslint --fix src/**/*.{js,ts,json}", + "lint": "eslint src/**/*.{js,ts,json} tests/**/*.{js,ts,json}", + "lint:fix": "eslint --fix src/**/*.{js,ts,json} --fix tests/**/*.{js,ts,json}", "format": "prettier --write src/**/*.{js,ts,md,json} --config ./.prettierrc", "commit": "git-cz", - "prepare": "husky install" - }, - "release-it": { - "npm": { - "publish": false - }, - "github": { - "release": true - }, - "git": { - "commitMessage": "chore: release ${version}" - } + "prepare": "husky install", + "test": "jest", + "test:watch": "jest --detectOpenHandles --watchAll", + "test:coverage": "jest --detectOpenHandles --coverage" }, "contributors": [ { @@ -47,6 +38,7 @@ "@types/node": "^14.18.26", "@typescript-eslint/eslint-plugin": "^5.36.2", "@typescript-eslint/parser": "5.36.2", + "axios-mock-adapter": "^1.21.2", "commitizen": "4.2.5", "cz-conventional-changelog": "3.3.0", "eslint": "^8.23.0", @@ -54,13 +46,16 @@ "eslint-config-standard-with-typescript": "^22.0.0", "eslint-import-resolver-typescript": "3.5.0", "eslint-plugin-import": "^2.26.0", + "eslint-plugin-jest": "^27.0.4", "eslint-plugin-n": "^15.2.5", "eslint-plugin-prettier": "4.2.1", "eslint-plugin-promise": "^6.0.1", "husky": "8.0.1", + "jest": "^29.0.3", + "jest-mock-axios": "^4.7.0-beta", "prettier": "2.7.1", - "release-it": "^15.4.1", "rimraf": "^3.0.2", + "ts-jest": "^29.0.1", "typescript": "^4.8.2" }, "dependencies": { diff --git a/src/botcity/maestro/sdk.ts b/src/botcity/maestro/sdk.ts index 1436458..1f21791 100644 --- a/src/botcity/maestro/sdk.ts +++ b/src/botcity/maestro/sdk.ts @@ -66,8 +66,10 @@ export class BotMaestroSdk { const data = { login: this._login, key: this._key } const response: AxiosResponse = await axios.post(url, data) this.accessToken = response.data.accessToken - } catch (error) { - console.error(error) + } catch (error: unknown) { + if (axios.isAxiosError(error)) { + throw Error('Login failed to server') + } throw error } } diff --git a/tests/index.ts b/tests/index.ts deleted file mode 100644 index d50d290..0000000 --- a/tests/index.ts +++ /dev/null @@ -1,96 +0,0 @@ -// import { BotMaestroSdk } from "../src/botcity" - -// const sdk = new BotMaestroSdk( - -// ) - -// const tasks = async () => { - // await sdk.login() - // const parametersCreateTask = { - // "param1": "value1", - // "param2": 2, - // "paramN": "etc" - // } - // const createTask = await sdk.createTask("atv_teste_demo", parametersCreateTask, true) - // console.log({createTask}) - - // const finishTask = await sdk.finishTask("27191", "FINISHED", "SUCCESS", "Your custom finish message here!") - // console.log({finishTask}) - - // const getTask = await sdk.getTask("26948") - // console.log({getTask}) -// } - -// const logs = async () => { -// await sdk.login() - // const columnsLog= [ - // {"name": "Column 1", "label": "col1", "width": 100}, - // {"name": "Column 2", "label": "col2", "width": 200}, - // {"name": "Column 3", "label": "col3", "width": 150} - // ] - - // const deleteLog = await sdk.deleteLog("botJavaGabriel") - // console.log({deleteLog}) - - // const createLog = await sdk.createLog("botJavaGabriel", columnsLog) - // console.log({createLog}) - - // const getLogs = await sdk.getLogs() - // console.log({getLogs}) - // console.log({"machines": getLogs[0].machines, notification: getLogs[0].notification}) - - // const getLog = await sdk.getLog("botJavaGabriel") - // console.log({ getLog }) - - // const logEntry = await sdk.logEntry("botJavaGabriel", {"name": "Column 1", "label": "col1", "width": 100}) - // console.log({logEntry}) - - // const fetchDataLog = await sdk.fetchDataLog("botJavaGabriel") - // console.log({fetchDataLog, _id: fetchDataLog[0]._id, columns: fetchDataLog[0].columns}) - - // const downloadCsvLog = await sdk.downloadCsvLog("botJavaGabriel", "test.csv") - // console.log({downloadCsvLog}) -// } - - -// const alerts = async () => { -// await sdk.login() - -// const createAlert = await sdk.createAlert("26137", "My Alert Title", "My message", "INFO") -// console.log({ createAlert }) - -// } - -// const messages = async () => { -// await sdk.login() - -// const createMessage = await sdk.createMessage(["govetrikayque@gmail.com"], [], "Subject of the E-mail", "This is the e-mail body", "TEXT") -// console.log({ createMessage }) - -// } - -// const artifacts = async () => { - // await sdk.login() - - // const createArtifact = await sdk.createArtifact("26137", "User Facing Name.txt", "filename.txt") - // console.log({ createArtifact }) - - - // const uploadFile = await sdk.uploadFile(`${createArtifact.id}`, "C:\\Users\\Kayque\\Pictures\\botcity.png") - // console.log({ uploadFile }) - - // const uploadArticact = await sdk.uploadArtifact("26137", "Botcity", "botcity.png", "C:\\Users\\Kayque\\Pictures\\botcity.png") - // console.log({ uploadArticact }) - - // const getArtifacts = await sdk.getArtifacts("50", "0") - // console.log({getArtifacts, content: getArtifacts.content, sort: getArtifacts.pageable.sort}) - - // const downloadArtifacts = await sdk.downloadArtifact(`${createArtifact.id}`, "test.png") - // console.log({downloadArtifacts}) -// } - -// tasks() -// logs() -// alerts() -// messages() -// artifacts() diff --git a/tests/unit/login.test.ts b/tests/unit/login.test.ts new file mode 100644 index 0000000..d768b31 --- /dev/null +++ b/tests/unit/login.test.ts @@ -0,0 +1,76 @@ +/* eslint-disable @typescript-eslint/no-floating-promises */ +/* eslint-disable jest/valid-expect */ +import { describe, it, beforeAll, afterEach, expect } from '@jest/globals' +import MockAdapter from 'axios-mock-adapter' +import axios from 'axios' +import { BotMaestroSdk } from '../../src' + +describe('Login module', () => { + let mock: MockAdapter + let server: string = 'https://testing.dev' + let login: string = 'testing' + let key: string = 'testing' + const endpointLogin: string = '/api/v2/workspace/login' + + beforeAll(() => { + mock = new MockAdapter(axios) + }) + + afterEach(() => { + mock.reset() + server = 'https://testing.dev' + login = 'testing' + key = 'testing' + }) + + it('Login successfully executed.', async () => { + const response = { + accessToken: 'testing' + } + + const maestro: BotMaestroSdk = new BotMaestroSdk(server, login, key) + + mock.onPost(`${server}${endpointLogin}`).reply(200, response) + + await maestro.login() + + expect(maestro.accessToken).toBe('testing') + }) + + it('Login executed with "Server is required." error', async () => { + server = '' + + const maestro: BotMaestroSdk = new BotMaestroSdk(server, login, key) + + mock.onPost(`${server}${endpointLogin}`).reply(400, {}) + + expect(async () => await maestro.login()).rejects.toThrow('Server is required.') + }) + + it('Login executed with "Login is required." error', async () => { + login = '' + + const maestro: BotMaestroSdk = new BotMaestroSdk(server, login, key) + + mock.onPost(`${server}/api/v2/workspace/login`).reply(400, {}) + + expect(async () => await maestro.login()).rejects.toThrow('Login is required.') + }) + + it('Login executed with "Key is required" error', async () => { + key = '' + const maestro: BotMaestroSdk = new BotMaestroSdk(server, login, key) + + mock.onPost(`${server}/api/v2/workspace/login`).reply(400, {}) + + expect(async () => await maestro.login()).rejects.toThrow('Key is required.') + }) + + it('Login executed with error generic', async () => { + const maestro: BotMaestroSdk = new BotMaestroSdk(server, login, key) + + mock.onPost(`${server}/api/v2/workspace/login`).reply(400, {}) + + expect(async () => await maestro.login()).rejects.toThrow('Login failed to server') + }) +}) diff --git a/tsconfig.json b/tsconfig.json index b09e4d5..2a95837 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -13,6 +13,6 @@ "esModuleInterop": true, "experimentalDecorators": true }, - "include": ["src/**/*"], - "exclude": ["node_modules", "**/*.test.ts"] + "include": ["src/**/*", "tests/**/*"], + "exclude": ["node_modules"] } \ No newline at end of file From 09febed890c2b37ca952571a7c55adfb3666114f Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 21 Sep 2022 16:11:46 -0300 Subject: [PATCH 2/2] ci: insert command test in pipeline build and pull requests --- .github/workflows/publish.yaml | 15 +++++++++++++++ .github/workflows/pull_request.yaml | 4 +++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/.github/workflows/publish.yaml b/.github/workflows/publish.yaml index d9b0195..403c718 100644 --- a/.github/workflows/publish.yaml +++ b/.github/workflows/publish.yaml @@ -25,3 +25,18 @@ jobs: run: npm publish env: NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} + + release: + needs: [ build ] + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v2 + - name: "✏️ Generate release changelog" + uses: heinrichreimer/github-changelog-generator-action@v2.3 + with: + token: ${{ secrets.GITHUB_TOKEN }} + - name: Release + uses: softprops/action-gh-release@v1 + with: + body_path: CHANGELOG.md \ No newline at end of file diff --git a/.github/workflows/pull_request.yaml b/.github/workflows/pull_request.yaml index 577bd3a..4fcab03 100644 --- a/.github/workflows/pull_request.yaml +++ b/.github/workflows/pull_request.yaml @@ -13,4 +13,6 @@ jobs: - name: Install dependencies run: npm install - name: Run eslint - run: npm run lint \ No newline at end of file + run: npm run lint + - name: Run tests + run: npm run test \ No newline at end of file