From 31a0adae6780f54e1bcb7b316c6c8881c1f39edc Mon Sep 17 00:00:00 2001 From: Evgeniy Goryuchkin Date: Mon, 14 Oct 2024 13:04:17 +0300 Subject: [PATCH] switch to github actions, fix some tests --- .github/workflows/main.yml | 101 +++++++++++++++++++++++++++++++++++++ .travis.yml | 10 ---- package.json | 12 ++--- tests/arg-parsing.js | 65 ++++++++++++------------ tests/mocha-hooks.js | 23 +++++---- tests/request.js | 3 -- 6 files changed, 154 insertions(+), 60 deletions(-) create mode 100644 .github/workflows/main.yml delete mode 100644 .travis.yml diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..57d547f --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,101 @@ +name: main + +on: + push: + branches: + - master + pull_request: + branches: + - master + +permissions: + checks: write + contents: write + pull-requests: read + packages: read + +jobs: + linter: + name: run eslint + runs-on: ubuntu-latest + steps: + - name: Check out Git repository + uses: actions/checkout@v4 + + - name: Set up Node.js + uses: actions/setup-node@v4 + with: + node-version: latest + + - name: Install Node.js dependencies + run: npm install --include=dev + + - name: Run linter with report + run: npm run lint + continue-on-error: true + + - name: Save linting report + run: npx eslint --output-file eslint_report.json --format json . + continue-on-error: true + + - name: Annotate Code Linting Results + uses: ataylorme/eslint-annotate-action@v3 + with: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + report-json: "eslint_report.json" + tests: + name: run tests and coverage report + runs-on: ubuntu-latest + env: + NODE_ENV: development + BUHOI_REDIS: redis://localhost:6379 + BUHOI_MQ: amqp://guest:guest@localhost:5672 + services: + redis: + image: redis + options: >- + --health-cmd "redis-cli ping" + --health-interval 10s + --health-timeout 5s + --health-retries 5 + ports: + - 6379:6379 + rabbitmq: + image: rabbitmq + env: + RABBITMQ_DEFAULT_USER: guest + RABBITMQ_DEFAULT_PASS: guest + ports: + - 5672:5672 + steps: + - name: Check out Git repository + uses: actions/checkout@v4 + + - name: Set up Node.js + uses: actions/setup-node@v4 + with: + node-version: latest + + - name: Install Node.js dependencies + run: npm install --include=dev + + - name: Install Mocha reporter + run: npm install mocha-ctrf-json-reporter + + - name: Run tests + run: npx mocha -r should --reporter mocha-ctrf-json-reporter --require tests/mocha-hooks.js --recursive tests + continue-on-error: true + + - name: Add annotations + run: npx github-actions-ctrf ctrf-report.json + working-directory: ctrf + + - name: Create tests coverage report + run: npm run test-coverage + continue-on-error: true + + - name: Send report to Coveralls + uses: coverallsapp/github-action@v2 + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + file: .nyc_output/coverage.lcov diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 268c616..0000000 --- a/.travis.yml +++ /dev/null @@ -1,10 +0,0 @@ -dist: jammy -language: node_js -services: - - redis-server - - rabbitmq -env: - - NODE_ENV=development BUHOI_REDIS=redis://localhost BUHOI_MQ=amqp://guest:guest@localhost -node_js: - - "18" -script: npm run travis-coveralls diff --git a/package.json b/package.json index ac9195c..200e81e 100644 --- a/package.json +++ b/package.json @@ -41,17 +41,18 @@ "devDependencies": { "@eslint/compat": "1.1.1", "@stylistic/eslint-plugin": "2.6.1", - "axios": "1.7.3", - "coveralls": "3.1.1", + "axios": "1.7.7", + "coveralls-next": "^4.2.1", "eslint": "9.8.0", "eslint-plugin-unused-imports": "4.0.1", "husky": "9.1.4", "lint-staged": "15.2.7", "mocha": "10.7.0", + "mocha-lcov-reporter": "^1.3.0", "nyc": "17.0.0", "should": "13.2.3", "should-sinon": "0.0.6", - "sinon": "18.0.0", + "sinon": "19.0.2", "webpack-dev-middleware": "7.3.0", "webpack-hot-middleware": "2.26.1" }, @@ -59,9 +60,8 @@ "precommit": "lint-staged", "prepush": "npm t", "lint": "eslint .", - "test": "npm run lint && mocha -r should --recursive tests", - "cover": "npm run lint && nyc mocha -r should --recursive tests && nyc report --reporter=html && xdg-open coverage/index.html", - "travis-coveralls": "npm run lint && nyc mocha -r should --recursive tests && nyc report --reporter=text-lcov | coveralls", + "test": "mocha -r should --require tests/mocha-hooks.js --recursive tests", + "test-coverage": "nyc npm run test && nyc report --reporter=text-lcov > .nyc_output/coverage.lcov", "release-patch": "npm t && npm version patch && npm publish && git push", "release-minor": "npm t && npm version minor && npm publish && git push", "release-major": "npm t && npm version major && npm publish && git push" diff --git a/tests/arg-parsing.js b/tests/arg-parsing.js index 289bc47..5687600 100644 --- a/tests/arg-parsing.js +++ b/tests/arg-parsing.js @@ -1,9 +1,6 @@ /* eslint-env mocha */ - -const Promise = require('bluebird') -const request = Promise.promisify(require('request')) const fs = require('fs') - +const request = require('./request') const todos = require('./app/features/todos') const users = require('./app/features/users') @@ -13,64 +10,68 @@ describe('buhoi arg parsing', function () { }) it('should treat no content as empty arg array', async function () { - const { statusCode } = await request({ + const { status } = await request({ url: 'https://localhost:3001/rpc/todos.publicProcedure', - method: 'POST', - headers: { 'content-type': 'application/json' }, - strictSSL: false, + method: 'post', + headers: { 'Content-Type': 'application/json' }, timeout: 1000, + validateStatus: () => true, }) todos.publicProcedureSpy.calledOnce.should.eql(true) todos.publicProcedureSpy.lastCall.args.slice(0, -2).should.eql([]) - statusCode.should.eql(200) + status.should.eql(200) }) - it('should treat no args in qs as empty arg array', async function () { - const { statusCode } = await request({ + it('should treat no args in params as empty arg array', async function () { + const { status } = await request({ url: 'https://localhost:3001/rpc/todos.publicProcedure', - method: 'GET', - strictSSL: false, + method: 'get', + headers: { 'Content-Type': 'application/json' }, timeout: 1000, + validateStatus: () => true, }) todos.publicProcedureSpy.calledOnce.should.eql(true) todos.publicProcedureSpy.lastCall.args.slice(0, -2).should.eql([]) - statusCode.should.eql(200) + status.should.eql(200) }) it('should respond with 500 if args are invalid JSON', async function () { - const { statusCode } = await request({ + const { status } = await request({ url: 'https://localhost:3001/rpc/todos.publicProcedure', - method: 'GET', - qs: { args: 'arg' }, - strictSSL: false, + method: 'get', + headers: { 'Content-Type': 'application/json' }, + params: { args: 'arg' }, timeout: 1000, + validateStatus: () => true, }) - statusCode.should.eql(500) + status.should.eql(500) }) it('should respond with 500 if body does not contains array of args', async function () { - const { statusCode } = await request({ + const { status } = await request({ url: 'https://localhost:3001/rpc/todos.publicProcedure', - method: 'POST', - json: 'dolphin', - strictSSL: false, + method: 'post', + headers: { 'Content-Type': 'application/json' }, + data: 'dolphin', timeout: 1000, + validateStatus: () => true, }) - statusCode.should.eql(500) + status.should.eql(500) }) it('should receive forms with files', async function () { - const { statusCode } = await request({ + const form = new FormData() + form.append('user_id', 10) + form.append('avatar', new Blob(await fs.createReadStream(`${__dirname}/app/public/index.html`).toArray())) + + const { status } = await request({ url: 'https://localhost:3001/rpc/users.uploadAvatar', - method: 'POST', - formData: { - user_id: 10, - avatar: fs.createReadStream(`${__dirname}/app/public/index.html`), - }, - strictSSL: false, + method: 'post', + data: form, timeout: 1000, + validateStatus: () => true, }) - statusCode.should.eql(200) + status.should.eql(200) users.uploadAvatarSpy.calledOnce.should.eql(true) const args = users.uploadAvatarSpy.lastCall.args args[0].should.have.property('user_id') diff --git a/tests/mocha-hooks.js b/tests/mocha-hooks.js index c797d08..cebf76b 100644 --- a/tests/mocha-hooks.js +++ b/tests/mocha-hooks.js @@ -2,12 +2,17 @@ const buhoi = require('../src') -before(() => buhoi.start(buhoi.config.simple({ - featuresPath: `${__dirname}/app/features`, - publicPath: `${__dirname}/app/public`, - webpackConfigPath: `${__dirname}/app/pages/webpack.config.js`, - isAuthorized: (session, feature, _procedure) => - feature !== 'secrets' || (session && session.startsWith('dodo')), -}))) - -after(() => buhoi.stop()) +exports.mochaHooks = { + async beforeAll () { + await buhoi.start(buhoi.config.simple({ + featuresPath: `${__dirname}/app/features`, + publicPath: `${__dirname}/app/public`, + webpackConfigPath: `${__dirname}/app/pages/webpack.config.js`, + isAuthorized: (session, feature, _procedure) => + feature !== 'secrets' || (session && session.startsWith('dodo')), + })) + }, + async afterAll () { + await buhoi.stop() + }, +} diff --git a/tests/request.js b/tests/request.js index 5476415..8c189db 100644 --- a/tests/request.js +++ b/tests/request.js @@ -3,9 +3,6 @@ const https = require('https') module.exports = function (params) { return axios({ - headers: { - 'Content-Type': 'application/json', - }, timeout: 1000, httpsAgent: new https.Agent({ rejectUnauthorized: false }), validateStatus: status => status < 500,