Overview

In Ship testing settled through Jest framework and MongoDB memory server with possibility running them in CI/CD pipeline. MongoDB’s memory server allows connecting to the MongoDB server and running integration tests isolated.

Tests should be placed in the tests directory specified for each resource from the resources folder and have next naming format user.service.spec.ts.

apps/api/src/resources/user/tests/user.service.spec.ts
resources/
  user/
    tests/
      user.service.spec.ts

Run tests and linter.

pnpm run test

Run only tests.

pnpm run test:unit

Example

import { Database } from '@paralect/node-mongo';

import { DATABASE_DOCUMENTS } from 'app-constants';

import { User } from 'types';
import { userSchema } from 'schemas';

const database = new Database(process.env.MONGO_URL as string);

const userService = database.createService<User>(DATABASE_DOCUMENTS.USERS, {
  schemaValidator: (obj) => userSchema.parseAsync(obj),
});

describe('User service', () => {
  beforeAll(async () => {
    await database.connect();
  });

  it('should insert doc to collection', async () => {
    const mockUser = { _id: '12q', name: 'John' };

    await userService.insertOne(mockUser);

    const insertedUser = await userService.findOne({ _id: mockUser._id });

    expect(insertedUser).toEqual(mockUser);
  });

  afterAll(async () => {
    await database.close();
  });
});

GitHub Actions

By default, tests run for each pull request to the main branch through the run-tests.yml workflow.

.github/workflows/run-tests.yml
name: run-tests

on:
  pull_request:
    branches:
      - main

jobs:
  test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [ 16.x ]
    steps:
      - uses: actions/checkout@v2
      - name: Test api using jest
        uses: actions/setup-node@v3
        with:
          node-version: ${{ matrix.node-version }}
          cache: 'npm'
      - run: npm install
      - run: npm test

To set up pull request rejection if tests failed visit Settings > Branches tab in your repository. Then add the branch protection rule “Require status checks to pass before merging”.