Skip to content

Commit 22d676c

Browse files
committed
Added Unit Tests for Output Util Function
1 parent 32d8e8c commit 22d676c

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
"postpack": "rm -f oclif.manifest.json",
5959
"posttest": "eslint . --ext .ts --config .eslintrc",
6060
"prepack": "rm -rf lib && tsc -b && oclif-dev manifest && oclif-dev readme",
61-
"test": "jest --detectOpenHandles",
61+
"test": "jest --detectOpenHandles --silent",
6262
"version": "oclif-dev readme && git add README.md"
6363
},
6464
"dependencies": {

test/utils/output.test.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import * as path from 'path'
2+
import generateOutput from '../../src/utils/output'
3+
const invalidJsonOutput = require('../data/invalidRegex.json')
4+
const invalidTableOutput = require('../data/tableData.json')
5+
const regexMessages = require('../../messages/index.json').validateRegex
6+
7+
jest.mock('fs')
8+
9+
describe('Generate Output after Stack is Processed', () => {
10+
beforeEach(() => {
11+
jest.restoreAllMocks()
12+
})
13+
14+
test('Filepath Flag is not set & Invalid Regex is found', async () => {
15+
const resultFile = 'results.csv'
16+
const storagePath = path.resolve(__dirname, '../../', resultFile)
17+
const consoleSpy = jest.spyOn(console, 'log')
18+
await generateOutput({}, invalidJsonOutput, invalidTableOutput)
19+
expect(consoleSpy).toHaveBeenCalledTimes(4)
20+
expect(consoleSpy).toHaveBeenCalledWith(regexMessages.tableOutput)
21+
expect(consoleSpy).toHaveBeenCalledWith(regexMessages.csvOutput, storagePath)
22+
expect(consoleSpy).toHaveBeenCalledWith(regexMessages.docsLink)
23+
})
24+
25+
test('Filepath Flag is set & Invalid Regex is found', async () => {
26+
const flags = {
27+
filePath: '/path/to/output/directory/',
28+
}
29+
const resultFile = 'results.csv'
30+
const storagePath = flags.filePath + resultFile
31+
const consoleSpy = jest.spyOn(console, 'log')
32+
await generateOutput(flags, invalidJsonOutput, invalidTableOutput)
33+
expect(consoleSpy).toHaveBeenCalledTimes(4)
34+
expect(consoleSpy).toHaveBeenCalledWith(regexMessages.tableOutput)
35+
expect(consoleSpy).toHaveBeenCalledWith(regexMessages.csvOutput, storagePath)
36+
expect(consoleSpy).toHaveBeenCalledWith(regexMessages.docsLink)
37+
})
38+
39+
test('Invalid Regex is not found', async () => {
40+
const consoleSpy = jest.spyOn(console, 'log')
41+
await generateOutput({}, [], [])
42+
expect(consoleSpy).toHaveBeenCalledTimes(1)
43+
expect(consoleSpy).toHaveBeenCalledWith(regexMessages.noInvalidRegex)
44+
})
45+
})

0 commit comments

Comments
 (0)