Skip to content

consistent-spacing-between-blocks rule encounters undefined error when parsing dynamic tests #384

Closed
@JesseDocken

Description

@JesseDocken

Our project has a number of test files that generate a bunch of dynamic tests in order to check a bunch of similar conditions without copying the code multiple times. For example:

describe('delete', () => {
  [
    {
      description: 'bad request',
      id: '0000',
    },
    {
      description: 'invalid id',
      id: 'abcd',
    }
  ].forEach((t) =>
    it(`delete: /rest/resources/:id (${t.description})`, async () => {
      // Shared test logic here
    });
  );
});

When the consistent-spacing-between-blocks rule is enabled, the rule fails to parse this properly because the isFirstStatementInScope function assumes that the grandparent node always has a body, but in this case the AST node is the call expression. This should be handled more gracefully by using optional chaining, e.g. node.parent?.parent?.body?.[0] === node.parent.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions