Skip to content

Commit

Permalink
test: add config env test + some github workflows
Browse files Browse the repository at this point in the history
  • Loading branch information
Tien Nam Dao committed Feb 21, 2023
1 parent 1fd7998 commit e8bf9ff
Show file tree
Hide file tree
Showing 12 changed files with 1,862 additions and 55 deletions.
9 changes: 9 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
NEXT_PUBLIC_BLOCK_INTERVAL=5000
NEXT_PUBLIC_TRANSACTION_INTERVAL=5000
NEXT_PUBLIC_CHART_INTERVAL=600000
NEXT_PUBLIC_DATE_FORMAT_VI='DD/MM/YYYY'
NEXT_PUBLIC_DATE_FROMAT_OTHER='MM/DD/YYYY'
NEXT_PUBLIC_ITEM_SHOW_HOME_PAGE=7
NEXT_PUBLIC_PAGE_OFFSET=10
NEXT_PUBLIC_MAXIMUM_FRACTION_DIGITS=4
NEXT_PUBLIC_NATIVE_TOKEN='asa'
51 changes: 51 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
name: Lint

on:
pull_request:
push:
branches:
- main

jobs:
unit-tests:
runs-on: ubuntu-latest

strategy:
fail-fast: true
matrix:
node: [16]

name: Lint

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Setup Node
uses: actions/setup-node@v2
with:
node-version: 16.x
registry-url: 'https://npm.pkg.github.com'
scope: '@astraprotocol'
token: ${{ secrets.PAT_ASTRAPROTOCOL }}

- name: Get yarn cache directory path
id: yarn-cache-dir-path
run: echo "::set-output name=dir::$(yarn cache dir)"

- uses: actions/cache@v2
id: yarn-cache # use this to check for `cache-hit` (`steps.yarn-cache.outputs.cache-hit != 'true'`)
with:
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-
- name: Setup npm token
run: npm config set '//npm.pkg.github.com/:_authToken' "${{secrets.PAT_ASTRAPROTOCOL}}"

- name: Install dependencies
run: yarn install --frozen-lockfile

- name: Run lint
run: yarn lint
17 changes: 17 additions & 0 deletions .github/workflows/lintPrTitle.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: 'Lint PR Title'

on:
pull_request_target:
types:
- opened
- edited
- synchronize

jobs:
main:
name: Validate PR title
runs-on: ubuntu-latest
steps:
- uses: amannn/action-semantic-pull-request@v4
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
45 changes: 45 additions & 0 deletions .github/workflows/unit-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: Unit Tests

on:
pull_request:
push:
branches:
- feat/add-config-test
- dev
- main

jobs:
unit-tests:
runs-on: ubuntu-latest

strategy:
fail-fast: true
matrix:
node: [16]

name: Unit tests (Jest)

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Setup Node
uses: actions/setup-node@v2

- name: Get yarn cache directory path
id: yarn-cache-dir-path
run: echo "::set-output name=dir::$(yarn cache dir)"

- uses: actions/cache@v2
id: yarn-cache # use this to check for `cache-hit` (`steps.yarn-cache.outputs.cache-hit != 'true'`)
with:
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-
- name: Install dependencies
run: yarn install --frozen-lockfile

- name: Run tests
run: yarn test
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
node_modules/
.next/
.DS_STORE
.env
.env.*
package-lock.json
yarn-error.log
.sentryclirc
6 changes: 0 additions & 6 deletions .husky/pre-commit

This file was deleted.

24 changes: 24 additions & 0 deletions __tests__/config.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
describe('check config', () => {
it('Check not empty any config define in next.config.js', () => {
expect(process.env.NEXT_PUBLIC_BLOCK_INTERVAL).toBeDefined()
expect(process.env.NEXT_PUBLIC_TRANSACTION_INTERVAL).toBeDefined()
expect(process.env.NEXT_PUBLIC_CHART_INTERVAL).toBeDefined()
expect(process.env.NEXT_PUBLIC_DATE_FORMAT_VI).toBeDefined()
expect(process.env.NEXT_PUBLIC_DATE_FROMAT_OTHER).toBeDefined()
expect(process.env.NEXT_PUBLIC_ITEM_SHOW_HOME_PAGE).toBeDefined()
expect(process.env.NEXT_PUBLIC_PAGE_OFFSET).toBeDefined()
expect(process.env.NEXT_PUBLIC_MAXIMUM_FRACTION_DIGITS).toBeDefined()
expect(process.env.NEXT_PUBLIC_NATIVE_TOKEN).toBeDefined()
})
it('Check env number is right', () => {
expect(Number(process.env.NEXT_PUBLIC_BLOCK_INTERVAL)).toBeGreaterThanOrEqual(0)
expect(Number(process.env.NEXT_PUBLIC_TRANSACTION_INTERVAL)).toBeGreaterThanOrEqual(0)
expect(Number(process.env.NEXT_PUBLIC_CHART_INTERVAL)).toBeGreaterThanOrEqual(0)
expect(Number(process.env.NEXT_PUBLIC_ITEM_SHOW_HOME_PAGE)).toBeGreaterThanOrEqual(0)
expect(Number(process.env.NEXT_PUBLIC_PAGE_OFFSET)).toBeGreaterThanOrEqual(0)
expect(Number(process.env.NEXT_PUBLIC_MAXIMUM_FRACTION_DIGITS)).toBeGreaterThanOrEqual(0)
})
it('Check native token is right', () => {
expect(process.env.NEXT_PUBLIC_NATIVE_TOKEN).toBe('asa')
})
})
17 changes: 17 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const nextJest = require('next/jest')

// Providing the path to your Next.js app which will enable loading next.config.js and .env files
const createJestConfig = nextJest({ dir: './' })

// Any custom config you want to pass to Jest
const customJestConfig = {
// testPathIgnorePatterns: ['<rootDir>/cypress/', '<rootDir>/src/config/__tests__/', '<rootDir>/packages'],

moduleDirectories: ['node_modules', __dirname],
testTimeout: 20000,
setupFilesAfterEnv: ['./jest.setup.js'],
testEnvironment: 'jest-environment-jsdom'
}

// createJestConfig is exported in this way to ensure that next/jest can load the Next.js configuration, which is async
module.exports = createJestConfig(customJestConfig)
41 changes: 41 additions & 0 deletions jest.setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Optional: configure or set up a testing framework before each test.
// If you delete this file, remove `setupFilesAfterEnv` from `jest.config.js`

// Used for __tests__/testing-library.js
// Learn more: https://github.com/testing-library/jest-dom
import { loadEnvConfig } from '@next/env'
import '@testing-library/jest-dom/extend-expect'
import { TextDecoder, TextEncoder } from 'util'

loadEnvConfig(process.cwd())

global.setImmediate = jest.useRealTimers
global.TextDecoder = TextDecoder
global.TextEncoder = TextEncoder

Object.defineProperty(window, 'matchMedia', {
writable: true,
value: jest.fn().mockImplementation(query => ({
matches: false,
media: query,
onchange: null,
addListener: jest.fn(), // Deprecated
removeListener: jest.fn(), // Deprecated
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
dispatchEvent: jest.fn()
}))
})

window.IntersectionObserver = jest.fn(() => ({
observe: jest.fn(),
unobserve: jest.fn(),
disconnect: jest.fn()
}))

jest.mock('next/dynamic', () => () => {
const DynamicComponent = () => null
DynamicComponent.displayName = 'LoadableComponent'
DynamicComponent.preload = jest.fn()
return DynamicComponent
})
12 changes: 1 addition & 11 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,7 @@ const { withSentryConfig } = require('@sentry/nextjs')

const nextConfig = {
reactStrictMode: false,
env: {
NEXT_PUBLIC_BLOCK_INTERVAL: '5000',
NEXT_PUBLIC_TRANSACTION_INTERVAL: '5000',
NEXT_PUBLIC_CHART_INTERVAL: 600000, // 10 mintues refresh chart overview: price, transactions
NEXT_PUBLIC_DATE_FORMAT_VI: 'DD/MM/YYYY',
NEXT_PUBLIC_DATE_FROMAT_OTHER: 'MM/DD/YYYY',
NEXT_PUBLIC_ITEM_SHOW_HOME_PAGE: 7,
NEXT_PUBLIC_PAGE_OFFSET: 10,
NEXT_PUBLIC_MAXIMUM_FRACTION_DIGITS: 4,
NEXT_PUBLIC_NATIVE_TOKEN: 'asa'
},

images: {
remotePatterns: [
{
Expand Down
12 changes: 10 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
"description": "Explorer for inspecting and analyzing EVM & Cosmos based blockchains",
"scripts": {
"dev": "next",
"build": "next build",
"build": "jest && next build",
"start": "next start",
"lint": "eslint",
"prepare": "husky install",
"prettier": "prettier --write ."
"prettier": "prettier --write .",
"test": "jest",
"test:watch": "jest --watch"
},
"keywords": [],
"author": "",
Expand Down Expand Up @@ -61,6 +63,10 @@
"@commitlint/cli": "^17.1.2",
"@commitlint/config-conventional": "^17.1.0",
"@svgr/webpack": "^6.3.1",
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^14.0.0",
"@testing-library/react-hooks": "^8.0.1",
"@types/jest": "^29.4.0",
"@types/lodash": "^4.14.185",
"@types/node": "^18.8.3",
"@types/numeral": "^2.0.2",
Expand All @@ -69,6 +75,8 @@
"eslint": "^8.23.1",
"eslint-config-next": "^12.3.1",
"husky": "^8.0.1",
"jest": "^29.4.3",
"jest-environment-jsdom": "^29.4.3",
"prettier": "^2.7.1",
"sass": "^1.54.9",
"typescript": "^4.8.4"
Expand Down
Loading

0 comments on commit e8bf9ff

Please sign in to comment.