(check: Check, context?: TestCheckContext) => Check

defineTestCheck wraps your checks and decorates these functions with sensible defaults for the CheckContext that is passed to your message, validate and fix functions that you can override. This cuts down on repetitive boilerplate when writing tests for your checks.

Parameters

check
Check
required

The Check object that you want to test

context
CheckContext
An object containing metadata about the package the check is being run against.

Returns

Returns the original check function, however the validate, fix, and message functions will be passed the defaults in TestCheckContext rather than requiring that all properties CheckContext are explicitly passed when testing check methods.

Example

// Without defineTestCheck
test('validate - returns true when valid', () => {
  mockFs({
    'package.json': JSON.stringify({
      name: 'foo',
      description: 'bar',
    }),
  });

  const check = myCheck();
  const result = myCheck.validate({
    package: {
      path: './',
      relativePath: './',
    },

    allPackages: [
      {
        path: './',
        relativePath: './',
      },
    ],
    codeowners: [],
    tags: [],
  });

  expect(result).toEqual(true);
});

// With defineTestCheck
test('validate - returns true when valid', () => {
  mockFs({
    'package.json': JSON.stringify({
      name: 'foo',
      description: 'bar',
    }),
  });

  const check = defineTestCheck(myCheck());
  const result = myCheck.validate();

  expect(result).toEqual(true);
});

Was this page helpful?