Passed
Push — master ( 0a8095...60f354 )
by hung
01:03
created

src/test/helper/output.test.js   A

Complexity

Total Complexity 15
Complexity/F 1.25

Size

Lines of Code 69
Function Count 12

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
wmc 15
c 1
b 0
f 0
nc 1
mnd 1
bc 18
fnc 12
dl 0
loc 69
rs 10
bpm 1.5
cpm 1.25
noi 2

1 Function

Rating   Name   Duplication   Size   Complexity  
A output.test.js ➔ ??? 0 63 1
1
const fs = require('fs')
2
const { join } = require('path')
3
4
const outputHelper = require('../../helper/output')
5
const TIMEOUT = 1000
6
7
describe('Output helper', () => {
8
  const spyError = jest.spyOn(console, 'error').mockImplementation(() => {})
0 ignored issues
show
Bug introduced by
The variable jest seems to be never declared. If this is a global, consider adding a /** global: jest */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
9
  const spyLog = jest.spyOn(console, 'log').mockImplementation(() => {})
0 ignored issues
show
Bug introduced by
The variable jest seems to be never declared. If this is a global, consider adding a /** global: jest */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
10
11
  const file = 'test/output.txt'
12
  const path = join(__dirname, '../../', file)
13
14
  it('output has empty data', () => {
15
    const result = outputHelper('', null)
16
    expect(result).toBe(null)
17
  })
18
19
  it('output is console', () => {
20
    outputHelper(null, 'data')
21
    expect(spyLog).toHaveBeenCalled()
22
    expect(spyLog.mock.calls[0][0]).toContain('data')
23
  })
24
25
  it('output is invalid', () => {
26
    outputHelper(1, 'data')
27
    expect(spyError).toHaveBeenCalled()
28
    expect(spyError.mock.calls[0][0]).toContain('Invalid Output')
29
  })
30
31
  it('output to file', done => {
32
    outputHelper(file, 'data')
33
    setTimeout(() => {
34
      try {
35
        expect(fs.readFileSync(path, 'utf8')).toBe('data')
36
        done()
37
      } catch (err) {
38
        done.fail(err)
39
      }
40
    }, TIMEOUT)
41
  })
42
43
  it('output to not existed file', done => {
44
    const dummy = 'dummy.txt'
45
    outputHelper(dummy, 'data')
46
    setTimeout(() => {
47
      try {
48
        expect(spyError).toHaveBeenCalled()
49
        expect(spyError.mock.calls[1][0]).toContain('Cannot write file to')
50
        done()
51
      } catch (err) {
52
        done.fail(err)
53
      }
54
    }, TIMEOUT)
55
  })
56
57
  it('output to write stream', done => {
58
    const writeable = fs.createWriteStream(path)
59
    outputHelper(writeable, 'data_stream')
60
    setTimeout(() => {
61
      try {
62
        expect(fs.readFileSync(path, 'utf8')).toBe('data_stream\r\n')
63
        done()
64
      } catch (err) {
65
        done.fail(err)
66
      }
67
    }, TIMEOUT)
68
  })
69
})
70