How to use @loopback/testlab - 10 common examples

To help you get started, we’ve selected a few @loopback/testlab examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github strongloop / loopback-next / packages / repository-tests / src / crud / relations / acceptance / has-many.relation.acceptance.ts View on Github external
it('can create an instance of the related model', async () => {
      const order = await customerRepo.orders(existingCustomerId).create({
        description: 'order 1',
      });

      expect(toJSON(order)).containDeep(
        toJSON({
          customerId: existingCustomerId,
          description: 'order 1',
        }),
      );

      const persisted = await orderRepo.findById(order.id);
      expect(toJSON(persisted)).to.deepEqual(
        toJSON({
          ...order,
          isShipped: features.emptyValue,
          // eslint-disable-next-line @typescript-eslint/camelcase
          shipment_id: features.emptyValue,
        }),
      );
    });
github strongloop / loopback-next / packages / boot / src / __tests__ / unit / booters / repository.booter.unit.ts View on Github external
it('gives a warning if called on an app without RepositoryMixin', async () => {
    const normalApp = new Application();
    await sandbox.copyFile(
      resolve(__dirname, '../../fixtures/multiple.artifact.js'),
    );

    const booterInst = new RepositoryBooter(
      normalApp as ApplicationWithRepositories,
      SANDBOX_PATH,
    );

    // Load uses discovered property
    booterInst.discovered = [resolve(SANDBOX_PATH, 'multiple.artifact.js')];
    await booterInst.load();

    sinon.assert.calledOnce(stub);
    sinon.assert.calledWith(
      stub,
      'app.repository() function is needed for RepositoryBooter. You can add it ' +
        'to your Application using RepositoryMixin from @loopback/repository.',
    );
  });
github strongloop / loopback-next / packages / boot / src / __tests__ / unit / booters / datasource.booter.unit.ts View on Github external
describe('datasource booter unit tests', () => {
  const SANDBOX_PATH = resolve(__dirname, '../../../.sandbox');
  const sandbox = new TestSandbox(SANDBOX_PATH);

  const DATASOURCES_PREFIX = 'datasources';
  const DATASOURCES_TAG = 'datasource';

  class AppWithRepo extends RepositoryMixin(Application) {}

  let app: AppWithRepo;
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  let stub: sinon.SinonStub<[any?, ...any[]], void>;

  beforeEach('reset sandbox', () => sandbox.reset());
  beforeEach(getApp);
  beforeEach(createStub);
  afterEach(restoreStub);

  it('gives a warning if called on an app without RepositoryMixin', async () => {
github strongloop / loopback-next / packages / boot / src / __tests__ / unit / booters / repository.booter.unit.ts View on Github external
describe('repository booter unit tests', () => {
  const SANDBOX_PATH = resolve(__dirname, '../../../.sandbox');
  const sandbox = new TestSandbox(SANDBOX_PATH);

  const REPOSITORIES_PREFIX = 'repositories';
  const REPOSITORIES_TAG = 'repository';

  class RepoApp extends RepositoryMixin(Application) {}

  let app: RepoApp;
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  let stub: sinon.SinonStub<[any?, ...any[]], void>;

  beforeEach('reset sandbox', () => sandbox.reset());
  beforeEach(getApp);
  beforeEach(createStub);
  afterEach(restoreStub);

  it('gives a warning if called on an app without RepositoryMixin', async () => {
github strongloop / loopback-next / packages / boot / src / __tests__ / integration / repository.booter.integration.ts View on Github external
describe('repository booter integration tests', () => {
  const SANDBOX_PATH = resolve(__dirname, '../../.sandbox');
  const sandbox = new TestSandbox(SANDBOX_PATH);

  // Remnants from Refactor -- need to add these to core
  const REPOSITORIES_PREFIX = 'repositories';
  const REPOSITORIES_TAG = 'repository';

  let app: BooterApp;

  beforeEach('reset sandbox', () => sandbox.reset());
  beforeEach(getApp);

  it('boots repositories when app.boot() is called', async () => {
    const expectedBindings = [
      `${REPOSITORIES_PREFIX}.ArtifactOne`,
      `${REPOSITORIES_PREFIX}.ArtifactTwo`,
    ];
github strongloop / loopback-next / packages / repository / src / __tests__ / unit / repositories / belongs-to-repository-factory.unit.ts View on Github external
it('throws an error when the target does not have any primary key', () => {
    class Product extends Entity {
      static definition = new ModelDefinition(
        'Product',
      ).addProperty('categoryId', {type: Number});
    }

    class Category extends Entity {
      static definition = new ModelDefinition('Category');
    }

    const productRepo = createStubInstance(DefaultCrudRepository);
    const categoryRepo = createStubInstance(DefaultCrudRepository);

    const relationMeta: BelongsToDefinition = {
      type: RelationType.belongsTo,
      targetsMany: false,
      name: 'category',
      source: Product,
      target: () => Category,
      keyFrom: 'categoryId',
      // Let the relation to look up keyTo as the primary key of Category
      // (which is not defined!)
      keyTo: undefined,
    };

    expect(() =>
      createBelongsToAccessor(
        relationMeta,
github strongloop / loopback-next / packages / repository / src / __tests__ / unit / repositories / belongs-to-repository-factory.unit.ts View on Github external
it('throws an error when the target does not have any primary key', () => {
    class Product extends Entity {
      static definition = new ModelDefinition(
        'Product',
      ).addProperty('categoryId', {type: Number});
    }

    class Category extends Entity {
      static definition = new ModelDefinition('Category');
    }

    const productRepo = createStubInstance(DefaultCrudRepository);
    const categoryRepo = createStubInstance(DefaultCrudRepository);

    const relationMeta: BelongsToDefinition = {
      type: RelationType.belongsTo,
      targetsMany: false,
      name: 'category',
      source: Product,
      target: () => Category,
      keyFrom: 'categoryId',
      // Let the relation to look up keyTo as the primary key of Category
      // (which is not defined!)
      keyTo: undefined,
    };

    expect(() =>
      createBelongsToAccessor(
github strongloop / loopback-next / packages / context / src / __tests__ / unit / interceptor.unit.ts View on Github external
const invocationCtx = givenInvocationContext();

    const keys = invocationCtx.getGlobalInterceptorBindingKeys();
    expect(keys).to.eql([
      'globalInterceptors.authInterceptor',
      'globalInterceptors.logInterceptor',
    ]);
  });

  // See https://v8.dev/blog/array-sort
  function isSortStable() {
    // v8 7.0 or above
    return +process.versions.v8.split('.')[0] >= 7;
  }

  skipIf(
    !isSortStable(),
    it,
    'sorts by binding order without group tags',
    async () => {
      ctx
        .bind('globalInterceptors.authInterceptor')
        .to(authInterceptor)
        .apply(asGlobalInterceptor());

      ctx
        .bind('globalInterceptors.logInterceptor')
        .to(logInterceptor)
        .apply(asGlobalInterceptor());

      const invocationCtx = givenInvocationContext();
github strongloop / loopback-next / packages / cli / smoke-test / openapi / real-world-apis.smoke.js View on Github external
realWorldAPIs = process.env.APIS.split(/\s+/)
        .filter(Boolean)
        .map(url => ({
          swaggerUrl: url,
          name: '',
          version: '',
        }));
      return;
    }

    // This hook sometimes takes several seconds, due to the large download
    // eslint-disable-next-line no-invalid-this
    this.timeout(10000);

    // Download a list of over 1500 real-world Swagger APIs from apis.guru
    const res = await supertest('https://api.apis.guru')
      .get('/v2/list.json')
      .expect(200);
    if (!res.ok) {
      throw new Error('Unable to download API listing from apis.guru');
    }

    // Remove certain APIs that are known to cause problems
    const apis = res.body;

    // GitHub's CORS policy blocks this request
    delete apis['googleapis.com:adsense'];

    // These APIs cause infinite loops in json-schema-ref-parser.  Still investigating.
    // https://github.com/BigstickCarpet/json-schema-ref-parser/issues/56
    delete apis['bungie.net'];
    delete apis['stripe.com'];
github strongloop / loopback-next / packages / rest / src / __tests__ / integration / rest.server.integration.ts View on Github external
it('parses query without decorated rest query params', async () => {
    // This handler responds with the query object (which is expected to
    // be parsed by Express)
    function requestWithQueryHandler({request, response}: RequestContext) {
      response.json(request.query);
      response.end();
    }

    // See https://github.com/strongloop/loopback-next/issues/2088
    const server = await givenAServer();
    server.handler(requestWithQueryHandler);
    await server.start();
    await supertest(server.url)
      .get('/?x=1&y[a]=2')
      .expect(200, {x: '1', y: {a: '2'}});
    await server.stop();
  });