     1→import { codeFrameColumns } from '@babel/code-frame';
     2→import { describe, expect, it } from 'vitest';
     3→import { Context } from '../src/common';
     4→import { ParseError } from '../src/errors';
     5→import { type Options } from '../src/options';
     6→import { parseSource } from '../src/parser';
     7→
     8→const IS_CI = Boolean(process.env.CI);
     9→// https://github.com/vitest-dev/vitest/issues/8151
    10→const toTestTile = (code: string) => code.replaceAll('\r', '␍␊');
    11→
    12→type NormalizedTestCase = { code: string; options?: Options; context?: Context; only?: true };
    13→type TestCase = string | NormalizedTestCase;
    14→
    15→const serializeParserError = (code: string, error: unknown) => {
    16→  if (!(error instanceof ParseError)) {
    17→    throw error;
    18→  }
    19→
    20→  const {
    21→    message,
    22→    loc: { start, end },
    23→    description,
    24→  } = error;
    25→
    26→  const codeFrame = codeFrameColumns(
    27→    code,
    28→    {
    29→      start: { line: start.line, column: start.column + 1 },
    30→      end: { line: end.line, column: end.column + 1 },
    31→    },
    32→    { highlightCode: false, message: description },
    33→  );
    34→
    35→  return `${error.name} ${message}\n${codeFrame}`;
    36→};
    37→
    38→function runTests(testCases: TestCase[], callback: (testCase: NormalizedTestCase) => void) {
    39→  for (let testCase of testCases) {
    40→    if (typeof testCase === 'string') {
    41→      testCase = { code: testCase };
    42→    }
    43→
    44→    const { code, only } = testCase;
    45→
    46→    if (IS_CI && only) {
    47→      throw new Error("Please remove 'only'.");
    48→    }
    49→
    50→    // https://github.com/vitest-dev/vitest/issues/8151
    51→    (only ? it.only : it)(toTestTile(code), () => {
    52→      callback(testCase);
    53→    });
    54→  }
    55→}
    56→
    57→export const pass = (name: string, testCases: TestCase[]) => {
    58→  describe(name, () => {
    59→    runTests(testCases, ({ code, options, context }) => {
    60→      const parseResult = parseSource(code, options, context ?? Context.None);
    61→      expect(parseResult).toMatchSnapshot();
    62→    });
    63→  });
    64→};
    65→
    66→export const fail = (name: string, testCases: TestCase[]) => {
    67→  describe(name, () => {
    68→    runTests(testCases, ({ code, options, context }) => {
    69→      let error;
    70→      try {
    71→        parseSource(code, options, context ?? Context.None);
    72→      } catch (parseError) {
    73→        error = parseError;
    74→      }
    75→
    76→      if (!error) {
    77→        throw new Error('Expect a ParserError thrown');
    78→      }
    79→
    80→      expect(serializeParserError(code, error)).toMatchSnapshot();
    81→    });
    82→  });
    83→};
