Skip to content

Commit

Permalink
chore: add test
Browse files Browse the repository at this point in the history
  • Loading branch information
QingWei-Li committed Sep 5, 2021
1 parent 91d0a45 commit bf63f56
Show file tree
Hide file tree
Showing 10 changed files with 1,945 additions and 26 deletions.
6 changes: 6 additions & 0 deletions .env.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
STORE_ACCESS_KEY=minio
STORE_SECRET_KEY=minio123
STORE_BUCKET=notea-test
PASSWORD=123
STORE_END_POINT=http://localhost:9000
STORE_FORCE_PATH_STYLE=true
13 changes: 13 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: Test
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Install modules
run: yarn
- name: Run MinIO
run: docker-compose up -d
- name: Run tests
run: yarn test
26 changes: 26 additions & 0 deletions __tests__/api/tree.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { createServer, IncomingMessage, Server, ServerResponse } from 'http'
import { apiResolver } from 'next/dist/server/api-utils'
import treeHandler from 'pages/api/tree'
import supertest from 'supertest'

describe('/api/tree', () => {
let server: Server

beforeEach(async () => {
const requestHandler = (
request: IncomingMessage,
response: ServerResponse
) => apiResolver(request, response, undefined, treeHandler, {} as any, true)
server = createServer(requestHandler)
})

afterEach(() => {
server.close()
})

test('create note', async () => {
const result = await supertest(server).get('/api/tree').expect(200)

expect(result.body).toBeDefined()
})
})
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ services:
MINIO_ACCESS_KEY: minio
MINIO_SECRET_KEY: minio123
entrypoint: sh
command: -c 'mkdir -p /data/notea && /usr/bin/minio server /data'
command: -c 'mkdir -p /data/notea && mkdir -p /data/notea-test && /usr/bin/minio server /data'
20 changes: 20 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require('dotenv').config({ path: '.env.test' })

module.exports = {
collectCoverageFrom: ['**/*.{ts}', '!**/*.d.ts', '!**/node_modules/**'],
testPathIgnorePatterns: ['<rootDir>/node_modules/', '<rootDir>/.next/'],
testEnvironment: 'jsdom',
transform: {
/* Use babel-jest to transpile tests with the next/babel preset
https://jestjs.io/docs/configuration#transform-objectstring-pathtotransformer--pathtotransformer-object */
'^.+\\.(ts)$': ['babel-jest', { presets: ['next/babel'] }],
},
transformIgnorePatterns: [
'/node_modules/',
'^.+\\.module\\.(css|sass|scss)$',
],
moduleNameMapper: {
'pages/(.*)$': '<rootDir>/pages/$1',
'libs/(.*)$': '<rootDir>/libs/$1',
},
}
4 changes: 4 additions & 0 deletions libs/server/middlewares/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ export async function useAuth(
res: ApiResponse,
next: ApiNext
) {
if (process.env.NODE_ENV === 'test') {
return next()
}

if (!isLoggedIn(req)) {
return res.APIError.NEED_LOGIN.throw()
}
Expand Down
4 changes: 4 additions & 0 deletions libs/server/middlewares/csrf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ export const applyCsrf: SSRMiddleware = async (req, _res, next) => {
const ignoredMethods = ['GET', 'HEAD', 'OPTIONS']

export function useCsrf(req: ApiRequest, res: ApiResponse, next: ApiNext) {
if (process.env.NODE_ENV === 'test') {
return next()
}

const token = req.headers[CSRF_HEADER_KEY] as string
const sessionToken = req.session.get(CSRF_HEADER_KEY)

Expand Down
3 changes: 3 additions & 0 deletions libs/server/store/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import { Readable } from 'stream'
// Apparently the stream parameter should be of type Readable|ReadableStream|Blob
// The latter 2 don't seem to exist anywhere.
export async function streamToBuffer(stream: Readable): Promise<Buffer> {
if (Buffer.isBuffer(stream)) {
return stream
}
return await new Promise((resolve, reject) => {
const chunks: Uint8Array[] = []
stream.on('data', (chunk) => chunks.push(chunk))
Expand Down
9 changes: 8 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"format": "prettier --write .",
"lint": "eslint . --ext ts --ext tsx --ext js",
"postinstall": "yarn autoclean --force",
"build:i18n": "node scripts/extract-i18n"
"build:i18n": "node scripts/extract-i18n",
"test": "jest"
},
"repository": "QingWei-Li/notea",
"husky": {
Expand Down Expand Up @@ -78,31 +79,37 @@
"@types/adm-zip": "^0.4.34",
"@types/classnames": "^2.2.11",
"@types/formidable": "^1.0.32",
"@types/jest": "^27.0.1",
"@types/lodash": "^4.14.168",
"@types/md5": "^2.3.0",
"@types/minio": "^7.0.7",
"@types/node": "^12.12.21",
"@types/prosemirror-inputrules": "^1.0.4",
"@types/react": "^17.0.11",
"@types/react-dom": "^17.0.8",
"@types/supertest": "^2.0.11",
"@types/ua-parser-js": "^0.7.35",
"@typescript-eslint/eslint-plugin": "^4.15.0",
"@typescript-eslint/parser": "^4.15.0",
"autoprefixer": "^10.2.4",
"babel-jest": "^27.1.0",
"babel-plugin-lodash": "^3.3.4",
"dotenv": "^10.0.0",
"eslint": "^7.20.0",
"eslint-config-next": "^11.0.0",
"eslint-config-prettier": "^7.2.0",
"eslint-plugin-react": "^7.22.0",
"eslint-plugin-react-hooks": "^4.2.0",
"husky": "^5.0.9",
"i18n-extract": "^0.6.7",
"jest": "^27.1.0",
"lint-staged": "^10.5.4",
"next-pwa": "^5.2.21",
"nightwind": "^1.1.6",
"postcss": "^8.2.6",
"prettier": "^2.2.1",
"react-dom": "^17.0.2",
"supertest": "^6.1.6",
"tailwindcss": "^2.0.3",
"typescript": "^4.3.4"
},
Expand Down
Loading

0 comments on commit bf63f56

Please sign in to comment.