Skip to content

Commit

Permalink
Merge branch 'release/0.1.0' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
devsheva committed Sep 3, 2024
2 parents 8644656 + 89200f3 commit 66ee593
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 22 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

All notable changes to this project will be documented in this file.

## [unreleased]
## [0.1.0] - 2024-09-03

### 🚀 Features

Expand All @@ -11,6 +11,11 @@ All notable changes to this project will be documented in this file.
- *(logger)* Add base class with logger property
- *(logger)* Add name and level property
- *(logger)* Add log method for all levels
- Add changelog with git-cliff

### 🐛 Bug Fixes

- [**breaking**] Remove base class since it was causing runtime error

### 🚜 Refactor

Expand All @@ -19,6 +24,7 @@ All notable changes to this project will be documented in this file.
### 📚 Documentation

- Use shields.io for badges
- Add instructions on how to temporary type a property added by a decorator

### 🧪 Testing

Expand Down
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,19 @@
![Codecov](https://img.shields.io/codecov/c/github/devsheva/simplelog)

A simple logger decorator amplify like

## Important

**Experimental decorators** are used for this package implementation and since experimental it has some bugs like unsafe typing.
TypeScript compiler complains when it finds out a property used inside a class that calls a decorator cause it cannot infer it, so as a temporary workaround simply declare an interface

```ts
interface Example {logger: any}

@Logger()
class Example {
hello() {
this.logger.debug() // THIS IS VALID
}
}
```
9 changes: 0 additions & 9 deletions spec/base.spec.ts

This file was deleted.

42 changes: 33 additions & 9 deletions spec/logger.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, expectTypeOf, it, vi } from 'vitest'
import Base from '../src/base.js'

import {
getLogLevel,
loggerDecorator,
Expand All @@ -9,19 +9,28 @@ import {

describe('logger', () => {
it('has a logger property', () => {
class Test extends Base {}
interface Test {
logger: any
}
class Test {}
loggerDecorator()(Test)

expect(Test.prototype).toHaveProperty('logger')
})

it('overrides name if not provided', () => {
class Test extends Base {}
interface Test {
logger: any
}
class Test {}
loggerDecorator()(Test)
})

it('sets name if provided', () => {
class Test extends Base {}
interface Test {
logger: any
}
class Test {}
loggerDecorator('test')(Test)
const instance = new Test()
expect(instance.logger).toHaveProperty('name', 'test')
Expand All @@ -30,14 +39,20 @@ describe('logger', () => {
describe('options', () => {
describe('level', () => {
it('defaults to info', () => {
class Test extends Base {}
interface Test {
logger: any
}
class Test {}
loggerDecorator()(Test)
const instance = new Test()
expect(instance.logger).toHaveProperty('level', 'info')
})

it('can be overridden', () => {
class Test extends Base {}
interface Test {
logger: any
}
class Test {}
loggerDecorator('', { level: 'debug' })(Test)
const instance = new Test()
expect(instance.logger).toHaveProperty('level', 'debug')
Expand All @@ -49,7 +64,10 @@ describe('logger', () => {
it.each(['verbose', 'debug', 'info', 'warn', 'error'])(
`has a %s method`,
(level) => {
class Test extends Base {}
interface Test {
logger: any
}
class Test {}
loggerDecorator()(Test)
const instance = new Test()
expect(instance.logger).toHaveProperty(level)
Expand All @@ -58,7 +76,10 @@ describe('logger', () => {
)

it('should log a message at the specified level', () => {
class Test extends Base {
interface Test {
logger: any
}
class Test {
hello() {
this.logger.debug('hello')
}
Expand All @@ -72,7 +93,10 @@ describe('logger', () => {
})

it('should not log a message at a lower level', () => {
class Test extends Base {
interface Test {
logger: any
}
class Test {
hello() {
this.logger.debug('hello')
}
Expand Down
3 changes: 0 additions & 3 deletions src/base.ts

This file was deleted.

0 comments on commit 66ee593

Please sign in to comment.