npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@loopback/repository-tests

v0.24.1

Published

A shared test suite to verify `@loopback/repository` functionality with a given compatible connector

Downloads

314

Readme

@loopback/repository-tests

A test suite verifying functionality of @loopback/repository in a connector-independent way.

Overview

The module provides test-suite factories to define standardized test suite capable of testing any combination of a Repository class and a corresponding connector, for example DefaultCrudRepository with connectors like memory, mysql and mongodb.

Installation

npm install --save @loopback/repository-tests

Basic use

Add the following file to your test suite, and make the following changes:

  • Replace DefaultCrudRepository with the repository class you want to test.
  • Replace the string 'memory' with the default export of the connector to use, e.g. require('loopback-connector-mysql').
  • If your database uses string primary keys (e.g. GUID/UUID), then change idType to 'string'.
import {DefaultCrudRepository} from '@loopback/repository';
import {
  CrudRepositoryCtor,
  crudRepositoryTestSuite,
} from '@loopback/repository-tests';

describe('DefaultCrudRepository + memory connector', () => {
  crudRepositoryTestSuite(
    {
      connector: 'memory',
      // add any database-specific configuration, e.g. credentials & db name
    },
    // Workaround for the following TypeScript error
    //   Type 'DefaultCrudRepository<T, ID, {}>' is not assignable to
    //     type 'EntityCrudRepository<T, ID, Relations>'.
    // See https://github.com/microsoft/TypeScript/issues/31840
    DefaultCrudRepository as CrudRepositoryCtor,
    {
      idType: 'number',
    },
  );
});

Developer guide

Under the hood, the test suite is composed from individual test suite files, e.g. src/__tests__/crud/create-retrieve.suite.ts.

IMPORTANT Keep the size of test files small. Each test file should cover a small & cohesive subset of our features. If a new test does fit the remaining tests in a test file, then move it to a new one.

A test suite file exports a single factory function that's called to define the tests:

export function createRetrieveSuite(
  dataSourceOptions: DataSourceOptions,
  repositoryClass: CrudRepositoryCtor,
  features: CrudFeatures,
) {
  // test code
}

The factory functions can use describe, it and other Mocha API like before/after hooks to define the tests to be executed.

Besides the arguments passed to the factory function, the test context (this provided by Mocha) is initialized with additional properties like this.dataSource (a data-source instance created with dataSourceOptions to be used by tests).

Because accessing Mocha's this in a type-safe way is cumbersome, we have a helper converting this to a named function argument. Example usage:

let repo: EntityCrudRepository<Product, typeof Product.prototype.id>;
before(
  withCrudCtx(async function setupRepository(ctx: CrudTestContext) {
    repo = new repositoryClass(Product, ctx.dataSource);
    await ctx.dataSource.automigrate(Product.name);
  }),
);

Contributions

Tests

Run npm test from the root folder.

Contributors

See all contributors.

License

MIT