1.8.0 • Published 1 year ago

@reflow-work/test-fixture-factory v1.8.0

Weekly downloads
-
License
-
Repository
github
Last release
1 year ago

@reflow-work/test-fixture-factory

Create test fixtures easily!

This library is inspired by Ecto Test factories.

Install

npm install --save-dev @reflow-work/test-fixture-factory

Usage

import { FixtureFactory } from '@reflow-work/test-fixture-factory';

type UserType = {
  name: string;
  age: number;
};

const userFactory = new FixtureFactory<UserType>(() => {
  return { name: 'name', age: 30 };
});

// creating a user
const user0 = userFactory.create();

// { name: 'name', age: 30 }

// creating a user with overriding attributes
const user1 = userFactory.create({ name: 'new name' });

// { name: 'new name', age: 30 }

// creating users
const [user2, user3] = userFactory.createList(2);

// [{ name: 'name', age: 30 }, { name: 'name', age: 30 }]

// creating users with overriding attributes
const [user4, user5] = userFactory.createList(2, [
  { name: 'new name' },
  { age: 40 },
]);

// [{ name: 'new name', age: 30 }, { name: 'name', age: 40 }]

You can make more complicated fixture factory with custom attributes of generator.

import { FixtureFactory } from '@reflow-work/test-fixture-factory';

const integerFactory = new FixtureFactory<
  number,
  { min: number | undefined; max: number | undefined }
>((attrs) => {
  const minNumber = attrs?.min ?? 0;
  const maxNumber = attrs?.max ?? Number.MAX_SAFE_INTEGER;

  return Math.floor(Math.random() * (maxNumber - minNumber)) + minNumber;
});

const number = integerFactory.create({ min: 0, max: 10 });

You can also composite those factories.

import { FixtureFactory } from '@reflow-work/test-fixture-factory';

type UserType = {
  name: string;
  age: number;
};

const integerFactory = new FixtureFactory<
  number,
  { min: number | undefined; max: number | undefined }
>((attrs) => {
  const minNumber = attrs?.min ?? 0;
  const maxNumber = attrs?.max ?? Number.MAX_SAFE_INTEGER;

  return Math.floor(Math.random() * (maxNumber - minNumber)) + minNumber;
});

const userFactory = new FixtureFactory<UserType>(() => {
  return {
    name: 'name',
    age: integerFactory.create({ min: 0, max: 100 }),
  };
});

When creating a FixtureFactory using a class, you should use | undefined = undefined to the fields instead of ?(optional). This is because the generator is set to return Required<T>. This prevents merging non-existent keys when overriding attrs.

See also When to use typescript optional property? How is it different from declaring property as undefined

class UserClass {
  constructor(
    public name: string,
    public age: number | undefined = undefined // instead of `number?`
  );
}

const userFactory = new FixtureFactory<UserClass>(() => ...)

For more information, see also test codes

Contributing

To install dependencies:

bun install

To run test:

bun test src

This project was created using bun init in bun v1.0.6. Bun is a fast all-in-one JavaScript runtime.

2.0.0-rc2

1 year ago

2.0.0-rc1

1 year ago

1.8.0

2 years ago

1.7.1

2 years ago

1.7.0

2 years ago

1.6.0

2 years ago

1.5.0

2 years ago

1.4.1

2 years ago

1.4.0

2 years ago

1.3.1

2 years ago

1.3.0

2 years ago

1.2.0

2 years ago

1.1.0

2 years ago

1.0.0

2 years ago