Skip to content

Commit

Permalink
Merge pull request #5 from cofacts/test
Browse files Browse the repository at this point in the history
Introduce jest test
  • Loading branch information
MrOrz authored Jan 10, 2024
2 parents a694e07 + cc36e28 commit 7b91454
Show file tree
Hide file tree
Showing 19 changed files with 11,722 additions and 3,783 deletions.
3 changes: 2 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
**/dist/**
**/node_modules/**
**/node_modules/**
**/rumors-db/
33 changes: 33 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: CI test

on:
# Triggers the workflow on push or pull request events
- pull_request
- push
# Allows you to run this workflow manually from the Actions tab
- workflow_dispatch

jobs:
install-and-test:
runs-on: ubuntu-latest
services:
rumors-test-db:
image: docker.elastic.co/elasticsearch/elasticsearch-oss:6.3.2
ports:
- 62223:9200

steps:
- uses: actions/checkout@v3
with:
submodules: true
- uses: actions/setup-node@v2
with:
node-version: '18'
cache: 'npm'
- run: npm ci
- run: npm run lint
- run: npm run test -- --coverage
- name: Update coveralls
uses: coverallsapp/github-action@master
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "test/rumors-db"]
path = test/rumors-db
url = [email protected]:cofacts/rumors-db.git
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# collab-server

[![CI test](https://github.com/cofacts/collab-server/actions/workflows/ci.yml/badge.svg)](https://github.com/cofacts/collab-server/actions/workflows/ci.yml) [![Coverage Status](https://coveralls.io/repos/github/cofacts/collab-server/badge.svg)](https://coveralls.io/github/cofacts/collab-server)

A [hocuspocus](https://github.com/ueberdosis/hocuspocus) application that serves as a collaboration backend.

## Development
Expand All @@ -20,3 +23,36 @@ npm run dev
```
docker build -t collab-server .
```

## Test

To prepare test DB, first start an elastic search server on port 62223:

```
$ docker run -d -p "62223:9200" --name "rumors-test-db" docker.elastic.co/elasticsearch/elasticsearch-oss:6.3.2
# for apple silicon Mac:
$ docker run -d -p "62223:9200" --name "rumors-test-db" webhippie/elasticsearch:6.4
# If it says 'The name "rumors-test-db" is already in use',
# Just run:
$ docker start rumors-test-db
```

Then run this to start testing:

```
$ npm t
```

If you want to run test on a specific file (ex: `src/xxx/__tests__/ooo.js`), run:

```
$ npm t -- src/xxx/__tests__/ooo.js
```


When you want to update jest snapshot, run:

```
$ npm t -- -u
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import {
newHocuspocus,
syncedNewHocuspocusProvider,
delayForMs,
} from 'test/utils';
import { Elasticsearch } from '../elasticsearch';
import elasticsearch from '@elastic/elasticsearch';

describe('elasticsearch extension', () => {
const elasticsearchOpts: elasticsearch.ClientOptions = {
node: process.env.ELASTICSEARCH_URL,
};
afterEach(async () => {
// server.destroy() does not resolve after cleanup functions such as onStoreDocument called
// thus we should wait for these functions finished
await delayForMs(1000);
});

it('return default ydoc when fetched documentName does not exist', async () => {
const server = await newHocuspocus({
yDocOptions: { gc: false, gcFilter: () => true },
port: process.env.PORT ? Number(process.env.PORT) : 1234,
extensions: [
new Elasticsearch({
elasticsearchOpts,
}),
],
});

const provider = await syncedNewHocuspocusProvider(server);
expect(provider.document.share.size).toBe(0);
provider.configuration.websocketProvider.disconnect();
provider.disconnect();

await server.destroy();
});

it('return fetched ydoc', async () => {
const textName = 'test_name';
const server = await newHocuspocus({
yDocOptions: { gc: false, gcFilter: () => true },
port: process.env.PORT ? Number(process.env.PORT) : 1234,
extensions: [
new Elasticsearch({
elasticsearchOpts,
}),
],
});
const provider1 = await syncedNewHocuspocusProvider(server);
const ydoc = provider1.document;
ydoc.getText(textName).insert(0, 'foo');
provider1.configuration.websocketProvider.disconnect();
provider1.disconnect();

const provider2 = await syncedNewHocuspocusProvider(server);
expect(provider2.document.getText(textName)).toMatchInlineSnapshot(`"foo"`);
provider2.configuration.websocketProvider.disconnect();
provider2.disconnect();

await server.destroy();
});

it('logs error', async () => {
jest.spyOn(global.console, 'error');
const server = await newHocuspocus({
yDocOptions: { gc: false, gcFilter: () => true },
port: 1234,

extensions: [
new Elasticsearch({
elasticsearchOpts: {
node: 'https://wrong-url/',
},
}),
],
});
const provider = await syncedNewHocuspocusProvider(server);
expect(console.error).toBeCalledTimes(1);
provider.configuration.websocketProvider.disconnect();
provider.disconnect();

// Note: console.error will be called again because onStoreDocument will be called as server cleanup
await server.destroy();
});
});
3 changes: 3 additions & 0 deletions hocuspocus-extension-elasticsearch/src/elasticsearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export class Elasticsearch extends Database {
fetch: async ({ documentName }) => {
// console.log(`DB fetch ${documentName}`);
try {
/* istanbul ignore next */
const {
body: {
_source: { ydoc: data },
Expand All @@ -40,6 +41,7 @@ export class Elasticsearch extends Database {
store: async ({ documentName, state }) => {
// console.log(`DB store ${state}`)
try {
/* istanbul ignore next */
await this.db?.update({
index: this.dbIndex,
type: 'doc',
Expand Down Expand Up @@ -70,6 +72,7 @@ export class Elasticsearch extends Database {
}

async onConfigure() {
/* istanbul ignore next */
const elasticsearchOpts = this.configuration.elasticsearchOpts || {
node: 'http://localhost:62222',
};
Expand Down
16 changes: 16 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
// modulePaths: [`<rootDir>/test/utils`],
setupFilesAfterEnv: ['./test/setup.ts'],
coveragePathIgnorePatterns: ['./test/'],
transform: {
'^.+\\.tsx?$': [
'ts-jest',
{
tsconfig: './tsconfig.json',
},
],
},
};
Loading

0 comments on commit 7b91454

Please sign in to comment.