Skip to content

Commit bf63f56

Browse files
committed
chore: add test
1 parent 91d0a45 commit bf63f56

File tree

10 files changed

+1945
-26
lines changed

10 files changed

+1945
-26
lines changed

.env.test

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
STORE_ACCESS_KEY=minio
2+
STORE_SECRET_KEY=minio123
3+
STORE_BUCKET=notea-test
4+
PASSWORD=123
5+
STORE_END_POINT=http://localhost:9000
6+
STORE_FORCE_PATH_STYLE=true

.github/workflows/test.yml

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
name: Test
2+
on: [push]
3+
jobs:
4+
build:
5+
runs-on: ubuntu-latest
6+
steps:
7+
- uses: actions/checkout@v2
8+
- name: Install modules
9+
run: yarn
10+
- name: Run MinIO
11+
run: docker-compose up -d
12+
- name: Run tests
13+
run: yarn test

__tests__/api/tree.test.ts

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { createServer, IncomingMessage, Server, ServerResponse } from 'http'
2+
import { apiResolver } from 'next/dist/server/api-utils'
3+
import treeHandler from 'pages/api/tree'
4+
import supertest from 'supertest'
5+
6+
describe('/api/tree', () => {
7+
let server: Server
8+
9+
beforeEach(async () => {
10+
const requestHandler = (
11+
request: IncomingMessage,
12+
response: ServerResponse
13+
) => apiResolver(request, response, undefined, treeHandler, {} as any, true)
14+
server = createServer(requestHandler)
15+
})
16+
17+
afterEach(() => {
18+
server.close()
19+
})
20+
21+
test('create note', async () => {
22+
const result = await supertest(server).get('/api/tree').expect(200)
23+
24+
expect(result.body).toBeDefined()
25+
})
26+
})

docker-compose.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ services:
99
MINIO_ACCESS_KEY: minio
1010
MINIO_SECRET_KEY: minio123
1111
entrypoint: sh
12-
command: -c 'mkdir -p /data/notea && /usr/bin/minio server /data'
12+
command: -c 'mkdir -p /data/notea && mkdir -p /data/notea-test && /usr/bin/minio server /data'

jest.config.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
require('dotenv').config({ path: '.env.test' })
2+
3+
module.exports = {
4+
collectCoverageFrom: ['**/*.{ts}', '!**/*.d.ts', '!**/node_modules/**'],
5+
testPathIgnorePatterns: ['<rootDir>/node_modules/', '<rootDir>/.next/'],
6+
testEnvironment: 'jsdom',
7+
transform: {
8+
/* Use babel-jest to transpile tests with the next/babel preset
9+
https://jestjs.io/docs/configuration#transform-objectstring-pathtotransformer--pathtotransformer-object */
10+
'^.+\\.(ts)$': ['babel-jest', { presets: ['next/babel'] }],
11+
},
12+
transformIgnorePatterns: [
13+
'/node_modules/',
14+
'^.+\\.module\\.(css|sass|scss)$',
15+
],
16+
moduleNameMapper: {
17+
'pages/(.*)$': '<rootDir>/pages/$1',
18+
'libs/(.*)$': '<rootDir>/libs/$1',
19+
},
20+
}

libs/server/middlewares/auth.ts

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ export async function useAuth(
77
res: ApiResponse,
88
next: ApiNext
99
) {
10+
if (process.env.NODE_ENV === 'test') {
11+
return next()
12+
}
13+
1014
if (!isLoggedIn(req)) {
1115
return res.APIError.NEED_LOGIN.throw()
1216
}

libs/server/middlewares/csrf.ts

+4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ export const applyCsrf: SSRMiddleware = async (req, _res, next) => {
2727
const ignoredMethods = ['GET', 'HEAD', 'OPTIONS']
2828

2929
export function useCsrf(req: ApiRequest, res: ApiResponse, next: ApiNext) {
30+
if (process.env.NODE_ENV === 'test') {
31+
return next()
32+
}
33+
3034
const token = req.headers[CSRF_HEADER_KEY] as string
3135
const sessionToken = req.session.get(CSRF_HEADER_KEY)
3236

libs/server/store/utils.ts

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import { Readable } from 'stream'
33
// Apparently the stream parameter should be of type Readable|ReadableStream|Blob
44
// The latter 2 don't seem to exist anywhere.
55
export async function streamToBuffer(stream: Readable): Promise<Buffer> {
6+
if (Buffer.isBuffer(stream)) {
7+
return stream
8+
}
69
return await new Promise((resolve, reject) => {
710
const chunks: Uint8Array[] = []
811
stream.on('data', (chunk) => chunks.push(chunk))

package.json

+8-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"format": "prettier --write .",
1212
"lint": "eslint . --ext ts --ext tsx --ext js",
1313
"postinstall": "yarn autoclean --force",
14-
"build:i18n": "node scripts/extract-i18n"
14+
"build:i18n": "node scripts/extract-i18n",
15+
"test": "jest"
1516
},
1617
"repository": "QingWei-Li/notea",
1718
"husky": {
@@ -78,31 +79,37 @@
7879
"@types/adm-zip": "^0.4.34",
7980
"@types/classnames": "^2.2.11",
8081
"@types/formidable": "^1.0.32",
82+
"@types/jest": "^27.0.1",
8183
"@types/lodash": "^4.14.168",
8284
"@types/md5": "^2.3.0",
8385
"@types/minio": "^7.0.7",
8486
"@types/node": "^12.12.21",
8587
"@types/prosemirror-inputrules": "^1.0.4",
8688
"@types/react": "^17.0.11",
8789
"@types/react-dom": "^17.0.8",
90+
"@types/supertest": "^2.0.11",
8891
"@types/ua-parser-js": "^0.7.35",
8992
"@typescript-eslint/eslint-plugin": "^4.15.0",
9093
"@typescript-eslint/parser": "^4.15.0",
9194
"autoprefixer": "^10.2.4",
95+
"babel-jest": "^27.1.0",
9296
"babel-plugin-lodash": "^3.3.4",
97+
"dotenv": "^10.0.0",
9398
"eslint": "^7.20.0",
9499
"eslint-config-next": "^11.0.0",
95100
"eslint-config-prettier": "^7.2.0",
96101
"eslint-plugin-react": "^7.22.0",
97102
"eslint-plugin-react-hooks": "^4.2.0",
98103
"husky": "^5.0.9",
99104
"i18n-extract": "^0.6.7",
105+
"jest": "^27.1.0",
100106
"lint-staged": "^10.5.4",
101107
"next-pwa": "^5.2.21",
102108
"nightwind": "^1.1.6",
103109
"postcss": "^8.2.6",
104110
"prettier": "^2.2.1",
105111
"react-dom": "^17.0.2",
112+
"supertest": "^6.1.6",
106113
"tailwindcss": "^2.0.3",
107114
"typescript": "^4.3.4"
108115
},

0 commit comments

Comments
 (0)