1.0.0 • Published 8 months ago

@virgodev/test-utils v1.0.0

Weekly downloads
-
License
ISC
Repository
-
Last release
8 months ago

Provides a framework for a fake REST database

Getting Started

install

npm install -d @virgodev/test-utils

setup database before all tests:

import FakeDatabase from '../composables/FakeDatabase';

FakeDatabase.addModelEndpoint('posts', [
  { id: 1, title: 'Welcome to my new blog!', tags: 'new' },
  { id: 2, title: "I'm back, i'm going to be posting a lot more", tags: 'startover' },
  { id: 3, title: 'Sorry about the hiatus, it has been busy here', tags: 'busy' },
]);

FakeDatabase.addEndpoint('GET', 'profile/me/', async (req, res, ctx) => {
  return res(ctx.status(200), ctx.json({ name: 'me' }));
});

you can use a test setup file by adding it to vitest.config.js

import { defineConfig } from 'vitest/config'

export default defineConfig({
  test: {
    globals: true,
    environment: 'jsdom',
    setupFiles: ['./__tests__/setup.js'],
    coverage: {
      reportsDirectory: './coverage',
    }
  },
});

use in tests like this:

import { test, assert } from 'vitest';
import FakeDatabase from '../FakeDatabase';
import api from '@virgodev/bazaar/functions/api';

FakeDatabase.setup();

test('REST get', async () => {
  const response = await api({ url: 'posts/' });
  assert.equal(response.status, 200);
  assert.equal(response.body.results.length, 3);
});