Skip to content

Commit

Permalink
add unit test and workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
arcphysx committed Nov 7, 2023
1 parent b662d7a commit 487c6ad
Show file tree
Hide file tree
Showing 7 changed files with 241 additions and 56 deletions.
35 changes: 35 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Build Project
on:
push:
branches:
- main
- 'release/**'
- 'feature/**'
pull_request_target:
branches:
- main
- 'release/**'

jobs:
build:
runs-on: ubuntu-latest
steps:
# Setup
- uses: actions/checkout@v4
- name: Setup Node
uses: actions/setup-node@v3
with:
node-version: '18.x'
registry-url: 'https://registry.npmjs.org'
- name: Install Dependencies
run: yarn install --frozen-lockfile

# Tests
- name: Unit tests
run: yarn test:cov
- name: Codecov
uses: codecov/codecov-action@v3
with:
token: ${{ secrets.CODECOV_TOKEN }}
file: ./coverage/lcov.info
fail_ci_if_error: true
21 changes: 21 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Publish to NPM
on:
release:
types: [created]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Node
uses: actions/setup-node@v3
with:
node-version: '18.x'
registry-url: 'https://registry.npmjs.org'
- name: Install dependencies and build 🔧
run: yarn install --frozen-lockfile && yarn build
- name: Publish package on NPM 📦
run: npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
10 changes: 10 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
coverage:
status:
project:
default:
target: 80%
patch:
default:
target: 80%

comment: false
13 changes: 11 additions & 2 deletions jest.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,20 @@
"json",
"ts"
],
"rootDir": "src",
"testRegex": ".spec.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"coverageDirectory": "../coverage",
"collectCoverageFrom": [
"**/*.(t|j)s",
"!**/*module.ts",
"!**/enum/*",
"!**/interface/*",
"!**/index.ts"
],
"roots": [
"<rootDir>/src/"
],
"coverageDirectory": "./coverage",
"testEnvironment": "node"
}
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,22 @@
"lint": "tslint -p tsconfig.json -c tslint.json",
"test": "jest",
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:e2e": "jest --config ./test/jest-e2e.json"
"test:cov": "jest --coverage"
},
"author": "arcphysx",
"license": "MIT",
"dependencies": {
"@nestjs/common": "^10.2.7",
"@nestjs/core": "^10.2.8",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.8.1",
"xendit-node": "^3.4.0"
},
"types": "dist/index.d.ts",
"devDependencies": {
"@types/jest": "^29.5.6",
"@golevelup/ts-jest": "^0.4.0",
"@nestjs/testing": "^10.2.8",
"@types/jest": "^29.5.7",
"@types/node": "^20.8.8",
"@types/supertest": "^2.0.15",
"jest": "^29.7.0",
Expand Down
35 changes: 35 additions & 0 deletions src/xendit.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Test, TestingModule } from '@nestjs/testing';
import { XenditService } from './xendit.service';
import { createMock } from '@golevelup/ts-jest';
import * as XND from 'xendit-node';

jest.mock('xendit-node', ()=> {
return {
Xendit : jest.fn().mockImplementation(() => { return {} })
}
});

describe('XenditService', () => {
let xenditService: XenditService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
XenditService,
{ provide: XenditService, useValue: createMock(XND.Xendit) }
],
}).compile();

xenditService = module.get<XenditService>(XenditService);
});

it('should be defined', () => {
expect(xenditService).toBeDefined();
});

it('should fill xendit-related property', () => {
let xendit = new XND.Xendit({secretKey: 'secret-key'})
let service = new XenditService(xendit)
expect(XND.Xendit).toHaveBeenCalledTimes(1)
});
});
Loading

0 comments on commit 487c6ad

Please sign in to comment.