Skip to content

Commit

Permalink
test: add initial unit tests for logging library (#17)
Browse files Browse the repository at this point in the history
* test: add initial unit tests for logging library

* add tests to workflow

* fix devDependencies

* add missing new line
  • Loading branch information
jveldboom authored Jul 7, 2023
1 parent 25d7a85 commit d15a49e
Show file tree
Hide file tree
Showing 6 changed files with 4,000 additions and 1,001 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/pull-request.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ jobs:
- name: Lint
run: cd src && npm run lint

# - name: Unit test
# run: yarn test
- name: Unit test
run: cd src && npm test

test-build:
runs-on: ubuntu-latest
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
.whisper
data/*

node_modules
node_modules
coverage
4 changes: 2 additions & 2 deletions src/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ const info = (message, optionalMessage = '') => {
}

const debug = (message, optionalMessage = '') => {
if (process.env.DEBUG === true) console.log(`[DEBUG] ${message}`, optionalMessage)
if (process.env.DEBUG === 'true') console.log(`[DEBUG] ${message}`, optionalMessage)
}

const notice = (message, optionalMessage = '') => {
console.log(message, optionalMessage)
console.log(chalk.white(message), optionalMessage)
}

module.exports = {
Expand Down
60 changes: 60 additions & 0 deletions src/log.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/* eslint-env jest */
const chalk = require('chalk')
const log = require('./log')

jest.spyOn(console, 'log').mockImplementation()

describe('log', () => {
beforeEach(() => {

})

afterEach(() => {
jest.resetAllMocks()
delete process.env.DEBUG
})

const levels = [
{ level: 'error', color: 'red' },
{ level: 'success', color: 'green' },
{ level: 'warn', color: 'yellow' },
{ level: 'info', color: 'cyan' },
{ level: 'notice', color: 'white' }
]

for (const test of levels) {
describe(`${test.level}()`, () => {
it(`should output ${test.level} message`, () => {
log[test.level]('test')
expect(console.log).toBeCalledWith(chalk[test.color]('test'), '')
})
it(`should output ${test.level} with optional message`, () => {
log[test.level]('test', { msg: 'testing' })
expect(console.log).toBeCalledWith(chalk[test.color]('test'), { msg: 'testing' })
})
})
}

// separate test since it uses an env variable
describe('debug()', () => {
it('should not output debug message', () => {
log.debug('test')
expect(console.log).toBeCalledTimes(0)
})
it('should not output debug with optional message', () => {
log.debug('test', { msg: 'testing' })
expect(console.log).toBeCalledTimes(0)
})

it('should output debug message', () => {
process.env.DEBUG = true
log.debug('test')
expect(console.log).toBeCalledWith('[DEBUG] test', '')
})
it('should output debug with optional message', () => {
process.env.DEBUG = true
log.debug('test', { msg: 'testing' })
expect(console.log).toBeCalledWith('[DEBUG] test', { msg: 'testing' })
})
})
})
Loading

0 comments on commit d15a49e

Please sign in to comment.