From 5f32924ddbec915f7c0e8eac3f212ee1630c7535 Mon Sep 17 00:00:00 2001 From: Thomas Pyle Date: Thu, 28 Sep 2023 20:58:25 -0400 Subject: [PATCH] Adds some simple unit tests around printRunSummary --- .github/workflows/unit-tests.yml | 42 +++++++------ packages/bruno-cli/package.json | 3 + packages/bruno-cli/src/commands/run.js | 3 +- packages/bruno-cli/src/commands/run.test.js | 67 +++++++++++++++++++++ 4 files changed, 94 insertions(+), 21 deletions(-) create mode 100644 packages/bruno-cli/src/commands/run.test.js diff --git a/.github/workflows/unit-tests.yml b/.github/workflows/unit-tests.yml index 49b558f66..c9f7a9ada 100644 --- a/.github/workflows/unit-tests.yml +++ b/.github/workflows/unit-tests.yml @@ -1,29 +1,31 @@ name: Unit Tests on: push: - branches: [ main ] + branches: [main] pull_request: - branches: [ main ] + branches: [main] jobs: test: timeout-minutes: 60 runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 - - uses: actions/setup-node@v3 - with: - node-version: 16 - - name: Install dependencies - run: npm i --legacy-peer-deps - - name: Test Package bruno-query - run: npm run test --workspace=packages/bruno-query - - name: Build Package bruno-query - run: npm run build --workspace=packages/bruno-query - - name: Test Package bruno-lang - run: npm run test --workspace=packages/bruno-lang - - name: Test Package bruno-schema - run: npm run test --workspace=packages/bruno-schema - - name: Test Package bruno-app - run: npm run test --workspace=packages/bruno-app - - name: Test Package bruno-js - run: npm run test --workspace=packages/bruno-js + - uses: actions/checkout@v3 + - uses: actions/setup-node@v3 + with: + node-version: 16 + - name: Install dependencies + run: npm i --legacy-peer-deps + - name: Test Package bruno-query + run: npm run test --workspace=packages/bruno-query + - name: Build Package bruno-query + run: npm run build --workspace=packages/bruno-query + - name: Test Package bruno-lang + run: npm run test --workspace=packages/bruno-lang + - name: Test Package bruno-schema + run: npm run test --workspace=packages/bruno-schema + - name: Test Package bruno-app + run: npm run test --workspace=packages/bruno-app + - name: Test Package bruno-js + run: npm run test --workspace=packages/bruno-js + - name: Test Package bruno-cli + run: npm run test --workspace=packages/bruno-cli diff --git a/packages/bruno-cli/package.json b/packages/bruno-cli/package.json index 91ce4a19c..fb134ae82 100644 --- a/packages/bruno-cli/package.json +++ b/packages/bruno-cli/package.json @@ -13,6 +13,9 @@ "type": "git", "url": "git+https://github.com/usebruno/bruno.git" }, + "scripts": { + "test": "jest" + }, "files": [ "src", "bin", diff --git a/packages/bruno-cli/src/commands/run.js b/packages/bruno-cli/src/commands/run.js index 5ed53e343..ff0ab42ea 100644 --- a/packages/bruno-cli/src/commands/run.js +++ b/packages/bruno-cli/src/commands/run.js @@ -389,5 +389,6 @@ module.exports = { command, desc, builder, - handler + handler, + printRunSummary }; diff --git a/packages/bruno-cli/src/commands/run.test.js b/packages/bruno-cli/src/commands/run.test.js new file mode 100644 index 000000000..979aec5a3 --- /dev/null +++ b/packages/bruno-cli/src/commands/run.test.js @@ -0,0 +1,67 @@ +const { describe, it, expect } = require('@jest/globals'); + +const { printRunSummary } = require('./run'); + +describe('printRunSummary', () => { + // Suppress console.log output + jest.spyOn(console, 'log').mockImplementation(() => {}); + + it('should produce the correct summary for a successful run', () => { + const results = [ + { + testResults: [{ status: 'pass' }, { status: 'pass' }, { status: 'pass' }], + assertionResults: [{ status: 'pass' }, { status: 'pass' }], + error: null + }, + { + testResults: [{ status: 'pass' }, { status: 'pass' }], + assertionResults: [{ status: 'pass' }, { status: 'pass' }, { status: 'pass' }], + error: null + } + ]; + + const summary = printRunSummary(results); + + expect(summary.totalRequests).toBe(2); + expect(summary.passedRequests).toBe(2); + expect(summary.failedRequests).toBe(0); + expect(summary.totalAssertions).toBe(5); + expect(summary.passedAssertions).toBe(5); + expect(summary.failedAssertions).toBe(0); + expect(summary.totalTests).toBe(5); + expect(summary.passedTests).toBe(5); + expect(summary.failedTests).toBe(0); + }); + + it('should produce the correct summary for a failed run', () => { + const results = [ + { + testResults: [{ status: 'fail' }, { status: 'pass' }, { status: 'pass' }], + assertionResults: [{ status: 'pass' }, { status: 'fail' }], + error: null + }, + { + testResults: [{ status: 'pass' }, { status: 'fail' }], + assertionResults: [{ status: 'pass' }, { status: 'fail' }, { status: 'fail' }], + error: null + }, + { + testResults: [], + assertionResults: [], + error: new Error('Request failed') + } + ]; + + const summary = printRunSummary(results); + + expect(summary.totalRequests).toBe(3); + expect(summary.passedRequests).toBe(2); + expect(summary.failedRequests).toBe(1); + expect(summary.totalAssertions).toBe(5); + expect(summary.passedAssertions).toBe(2); + expect(summary.failedAssertions).toBe(3); + expect(summary.totalTests).toBe(5); + expect(summary.passedTests).toBe(3); + expect(summary.failedTests).toBe(2); + }); +});