|
| 1 | +/* |
| 2 | + * Copyright (c) 2020, salesforce.com, inc. |
| 3 | + * All rights reserved. |
| 4 | + * Licensed under the BSD 3-Clause license. |
| 5 | + * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause |
| 6 | + */ |
| 7 | +import * as path from 'path'; |
| 8 | +import * as fs from 'fs'; |
| 9 | +import { execCmd, TestSession } from '@salesforce/cli-plugins-testkit'; |
| 10 | +import { config, expect } from 'chai'; |
| 11 | +import { TestRunIdResult } from '@salesforce/apex-node'; |
| 12 | +import { RunResult } from '../../../../src/reporters'; |
| 13 | +config.truncateThreshold = 0; |
| 14 | + |
| 15 | +describe('apex get test', () => { |
| 16 | + let session: TestSession; |
| 17 | + let testId: string | undefined; |
| 18 | + before(async () => { |
| 19 | + session = await TestSession.create({ |
| 20 | + project: { |
| 21 | + gitClone: 'https://github.com/trailheadapps/dreamhouse-lwc.git', |
| 22 | + }, |
| 23 | + devhubAuthStrategy: 'AUTO', |
| 24 | + scratchOrgs: [ |
| 25 | + { |
| 26 | + config: path.join('config', 'project-scratch-def.json'), |
| 27 | + setDefault: true, |
| 28 | + alias: 'org', |
| 29 | + }, |
| 30 | + ], |
| 31 | + }); |
| 32 | + |
| 33 | + execCmd('project:deploy:start -o org --source-dir force-app', { ensureExitCode: 0 }); |
| 34 | + testId = execCmd<TestRunIdResult>('apex:run:test --json', { |
| 35 | + ensureExitCode: 0, |
| 36 | + }).jsonOutput?.result.testRunId.trim(); |
| 37 | + expect(testId).to.be.a('string'); |
| 38 | + }); |
| 39 | + |
| 40 | + after(async () => { |
| 41 | + await session?.zip(undefined, 'artifacts'); |
| 42 | + await session?.clean(); |
| 43 | + }); |
| 44 | + |
| 45 | + describe('--result-format', () => { |
| 46 | + it('will print tap format', async () => { |
| 47 | + const result = execCmd(`apex:get:test --result-format tap --test-run-id ${testId}`, { ensureExitCode: 0 }) |
| 48 | + .shellOutput.stdout; |
| 49 | + expect(result).to.include('1..1'); |
| 50 | + expect(result).to.include('ok 1'); |
| 51 | + expect(result).to.include('--result-format <format>" to retrieve test results in a different format.'); |
| 52 | + }); |
| 53 | + it('will print junit format', async () => { |
| 54 | + const result = execCmd(`apex:get:test --result-format junit --test-run-id ${testId}`, { ensureExitCode: 0 }) |
| 55 | + .shellOutput.stdout; |
| 56 | + expect(result).to.include('<?xml version="1.0" encoding="UTF-8"?>'); |
| 57 | + expect(result).to.include('<testsuites>'); |
| 58 | + expect(result).to.include('<testsuite name="force.apex" timestamp="'); |
| 59 | + expect(result).to.include( |
| 60 | + '<testcase name="testGetPicturesWithResults" classname="TestPropertyController" time="' |
| 61 | + ); |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + it('will run report on test', async () => { |
| 66 | + const result = execCmd(`apex:get:test --test-run-id ${testId}`, { ensureExitCode: 0 }).shellOutput.stdout; |
| 67 | + expect(result).to.include('=== Test Summary'); |
| 68 | + expect(result).to.include('=== Test Results'); |
| 69 | + }); |
| 70 | + |
| 71 | + it('will get tests --json', async () => { |
| 72 | + const result = execCmd<RunResult>(`apex:get:test --test-run-id ${testId} --json`, { ensureExitCode: 0 }).jsonOutput |
| 73 | + ?.result; |
| 74 | + expect(result?.tests.length).to.equal(11); |
| 75 | + expect(result?.summary.outcome).to.equal('Passed'); |
| 76 | + expect(result?.summary.testsRan).to.equal(11); |
| 77 | + expect(result?.summary).to.have.all.keys( |
| 78 | + 'outcome', |
| 79 | + 'testsRan', |
| 80 | + 'passing', |
| 81 | + 'failing', |
| 82 | + 'skipped', |
| 83 | + 'passRate', |
| 84 | + 'failRate', |
| 85 | + 'testStartTime', |
| 86 | + 'testExecutionTime', |
| 87 | + 'testTotalTime', |
| 88 | + 'commandTime', |
| 89 | + 'hostname', |
| 90 | + 'orgId', |
| 91 | + 'username', |
| 92 | + 'testRunId', |
| 93 | + 'userId' |
| 94 | + ); |
| 95 | + expect(result?.tests[0]).to.have.all.keys( |
| 96 | + 'Id', |
| 97 | + 'QueueItemId', |
| 98 | + 'StackTrace', |
| 99 | + 'Message', |
| 100 | + 'AsyncApexJobId', |
| 101 | + 'MethodName', |
| 102 | + 'Outcome', |
| 103 | + 'ApexClass', |
| 104 | + 'RunTime', |
| 105 | + 'FullName' |
| 106 | + ); |
| 107 | + }); |
| 108 | + |
| 109 | + describe('--code-coverage', () => { |
| 110 | + it('will run default tests sync with --code-coverage', async () => { |
| 111 | + const result = execCmd(`apex:get:test --test-run-id ${testId} --code-coverage`, { ensureExitCode: 0 }).shellOutput |
| 112 | + .stdout; |
| 113 | + expect(result).to.include('=== Apex Code Coverage by Class'); |
| 114 | + expect(result).to.match(/TEST NAME\s+OUTCOME\s+MESSAGE\s+RUNTIME \(MS\)/); |
| 115 | + }); |
| 116 | + it('will run default tests sync with --code-coverage --json', async () => { |
| 117 | + const result = execCmd<RunResult>(`apex:get:test --test-run-id ${testId} --code-coverage --json`, { |
| 118 | + ensureExitCode: 0, |
| 119 | + }).jsonOutput?.result; |
| 120 | + expect(result?.summary).to.have.all.keys( |
| 121 | + 'commandTime', |
| 122 | + 'failRate', |
| 123 | + 'passRate', |
| 124 | + 'failing', |
| 125 | + 'hostname', |
| 126 | + 'orgId', |
| 127 | + 'outcome', |
| 128 | + 'passing', |
| 129 | + 'skipped', |
| 130 | + 'orgWideCoverage', |
| 131 | + 'testRunCoverage', |
| 132 | + 'testExecutionTime', |
| 133 | + 'testRunId', |
| 134 | + 'testStartTime', |
| 135 | + 'testTotalTime', |
| 136 | + 'testsRan', |
| 137 | + 'userId', |
| 138 | + 'username' |
| 139 | + ); |
| 140 | + }); |
| 141 | + }); |
| 142 | + |
| 143 | + it('will create --output-dir', () => { |
| 144 | + const result = execCmd(`apex:get:test --output-dir testresults --code-coverage --test-run-id ${testId}`, { |
| 145 | + ensureExitCode: 0, |
| 146 | + }).shellOutput.stdout; |
| 147 | + expect(result).to.include('Test result files written to testresults'); |
| 148 | + const outputDir = path.join(session.project.dir, 'testresults'); |
| 149 | + expect(fs.statSync(outputDir).isDirectory()).to.be.true; |
| 150 | + expect(fs.readdirSync(outputDir).length).to.equal(6); |
| 151 | + expect(fs.existsSync(path.join(outputDir, 'test-result-codecoverage.json'))).to.be.true; |
| 152 | + expect(fs.existsSync(path.join(outputDir, 'test-result.txt'))).to.be.true; |
| 153 | + expect(fs.existsSync(path.join(outputDir, 'test-run-id.txt'))).to.be.true; |
| 154 | + }); |
| 155 | +}); |
0 commit comments