Skip to content

⬅️ Back to Table of Contents

📄 integration-test-base

📊 Analysis Summary

Metric Count
🔧 Functions 4
📦 Imports 2
⚡ Async/Await Patterns 6

📚 Table of Contents

🛠️ File Location:

📂 packages/integration-tests/tools/integration-test-base.ts

📦 Imports

Name Source
execFile ./pack-packages.js
FIXTURES_DESTINATION_DIR ./pack-packages.js

Async/Await Patterns

Type Function Await Expressions Promise Chains
await-expression integrationTest executeTest(testFolder) none
await-expression eslintIntegrationTest execFile( 'pnpm', [ 'exec', 'eslint', '--format', 'json', '--output-file', ou... none
promise-chain nodeIntegrationTest none Promise.allSettled
await-expression nodeIntegrationTest Promise.allSettled([ execFile('node', [scriptName], { cwd: testFolder, }), ]) none
promise-chain typescriptIntegrationTest none Promise.allSettled
await-expression typescriptIntegrationTest Promise.allSettled([ execFile( 'pnpm', ['exec', 'tsc6', '--noEmit', '--skipLi... none

Functions

eslintIntegrationTest(testFilename: string, filesGlob: string): void

Parameters:

  • testFilename string
  • filesGlob string

Returns: void

Calls:

  • integrationTest
  • path.join
  • execFile (from ./pack-packages.js)
  • String
  • console.error
  • expect(stderr).toHaveLength
  • (await fs.readFile(outFile, { encoding: 'utf-8' })) // clean the output to remove any changing facets so tests are stable .replaceAll( new RegExp("filePath": ?"(/private)?${testFolder}, 'g'), '"filePath": "<root>', ) .replaceAll
  • path.relative
  • JSON.parse
  • expect(lintOutput).toMatchSnapshot

Internal Comments:

// lint, outputting to a JSON file (x2)
// we expect eslint will "fail" because we have intentional lint errors
// useful for debugging
// console.log('Lint complete.');
// assert the linting state is consistent (x2)

Code
export function eslintIntegrationTest(
  testFilename: string,
  filesGlob: string,
): void {
  integrationTest('eslint', testFilename, async testFolder => {
    // lint, outputting to a JSON file
    const outFile = path.join(testFolder, 'eslint.json');

    let stderr = '';
    try {
      await execFile(
        'pnpm',
        [
          'exec',
          'eslint',
          '--format',
          'json',
          '--output-file',
          outFile,
          '--fix-dry-run',
          filesGlob,
        ],
        {
          cwd: testFolder,
          shell: true,
        },
      );
    } catch (ex) {
      // we expect eslint will "fail" because we have intentional lint errors

      // useful for debugging
      if (typeof ex === 'object' && ex != null && 'stderr' in ex) {
        stderr = String(ex.stderr);
      }
    }
    // console.log('Lint complete.');
    if (stderr.length > 0) {
      console.error(stderr);
    }
    expect(stderr).toHaveLength(0);

    // assert the linting state is consistent
    const lintOutputRAW = (await fs.readFile(outFile, { encoding: 'utf-8' }))
      // clean the output to remove any changing facets so tests are stable
      .replaceAll(
        new RegExp(`"filePath": ?"(/private)?${testFolder}`, 'g'),
        '"filePath": "<root>',
      )
      .replaceAll(
        /"filePath":"([^"]*)"/g,
        (_, testFile: string) =>
          `"filePath": "<root>/${path.relative(testFolder, testFile)}"`,
      );
    let lintOutput: unknown;
    try {
      lintOutput = JSON.parse(lintOutputRAW);
    } catch {
      throw new Error(
        `Lint output could not be parsed as JSON: \`${lintOutputRAW}\`.`,
      );
    }
    expect(lintOutput).toMatchSnapshot();
  });
}

nodeIntegrationTest(testFilename: string, scriptName: string, assertOutput: (stderr: string) => void): void

Parameters:

  • testFilename string
  • scriptName string
  • assertOutput (stderr: string) => void

Returns: void

Calls:

  • integrationTest
  • Promise.allSettled
  • execFile (from ./pack-packages.js)
  • assertOutput
Code
export function nodeIntegrationTest(
  testFilename: string,
  scriptName: string,
  assertOutput: (stderr: string) => void,
): void {
  integrationTest(`node ${scriptName}`, testFilename, async testFolder => {
    const [result] = await Promise.allSettled([
      execFile('node', [scriptName], {
        cwd: testFolder,
      }),
    ]);

    const stderr =
      result.status === 'rejected'
        ? (result.reason as { stderr: string }).stderr
        : result.value.stderr;

    assertOutput(stderr);
  });
}

typescriptIntegrationTest(testName: string, testFilename: string, tscArgs: string[], assertOutput: (out: string) => void): void

Parameters:

  • testName string
  • testFilename string
  • tscArgs string[]
  • assertOutput (out: string) => void

Returns: void

Calls:

  • integrationTest
  • Promise.allSettled
  • execFile (from ./pack-packages.js)
  • assertOutput
  • (result.reason as { stdout: string }).stdout.replace
  • expect(result.value.stdout).toBe
  • expect(result.value.stderr).toBe

Internal Comments:

// this looks weird - but it means that we can show the stdout (the errors) (x3)
// in the test output when typescript fails which helps with debugging (x3)
// on macos the tmp path might be shown by TS with `/private/`, but
// the tmp util does not include that prefix folder
// TS logs nothing when it succeeds (x5)

Code
export function typescriptIntegrationTest(
  testName: string,
  testFilename: string,
  tscArgs: string[],
  assertOutput: (out: string) => void,
): void {
  integrationTest(testName, testFilename, async testFolder => {
    const [result] = await Promise.allSettled([
      execFile(
        'pnpm',
        ['exec', 'tsc6', '--noEmit', '--skipLibCheck', ...tscArgs],
        {
          cwd: testFolder,
          shell: true,
        },
      ),
    ]);

    if (result.status === 'rejected') {
      // this looks weird - but it means that we can show the stdout (the errors)
      // in the test output when typescript fails which helps with debugging
      assertOutput(
        (result.reason as { stdout: string }).stdout.replace(
          // on macos the tmp path might be shown by TS with `/private/`, but
          // the tmp util does not include that prefix folder
          new RegExp(`(/private)?${testFolder}`),
          '/<tmp_folder>',
        ),
      );
    } else {
      // TS logs nothing when it succeeds
      expect(result.value.stdout).toBe('');
      expect(result.value.stderr).toBe('');
    }
  });
}

integrationTest(testName: string, testFilename: string, executeTest: (testFolder: string) => Promise<void>): void

Parameters:

  • testName string
  • testFilename string
  • executeTest (testFolder: string) => Promise<void>

Returns: void

Calls:

  • path.parse(testFilename).name.replace
  • path.join
  • describe
  • it
  • executeTest
Code
function integrationTest(
  testName: string,
  testFilename: string,
  executeTest: (testFolder: string) => Promise<void>,
): void {
  const fixture = path.parse(testFilename).name.replace('.test', '');

  const testFolder = path.join(FIXTURES_DESTINATION_DIR, fixture);

  describe(fixture, () => {
    describe(testName, () => {
      it('should work successfully', async () => {
        await executeTest(testFolder);
      });
    });
  });
}

Generated by Syntax Scribe