Back to Repositories

Testing File Path Ignore Patterns in Create-React-App

This test suite validates the file ignoring functionality in Create React App’s development utilities, specifically focusing on how different file paths are handled for watch exclusions. The tests ensure proper handling of node_modules and source directory paths.

Test Coverage Overview

The test suite provides comprehensive coverage of the ignoredFiles utility, examining various file path scenarios.

Key areas tested include:
  • Regular file path handling
  • Node modules detection
  • Source directory path handling
  • Nested node_modules within source directories
  • Path prefix matching

Implementation Analysis

The testing approach uses Jest’s describe/it pattern to organize related test cases logically. Each test case follows a consistent structure of setting up a source path, testing file paths against the ignore regex, and asserting expected outcomes using Jest’s expect statements.

The implementation leverages Jest’s built-in assertion library and employs clear test case isolation.

Technical Details

Testing tools and configuration:
  • Jest as the testing framework
  • Regular expressions for path matching
  • Mock file paths for various scenarios
  • Strict mode JavaScript
  • MIT licensed test implementation

Best Practices Demonstrated

The test suite exemplifies several testing best practices in its implementation.

Notable practices include:
  • Descriptive test case naming
  • Isolated test scenarios
  • Consistent test structure
  • Clear expected vs actual assertions
  • Comprehensive edge case coverage

facebook/create-react-app

packages/react-dev-utils/__tests__/ignoredFiles.test.js

            
/**
 * Copyright (c) 2015-present, Facebook, Inc.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

'use strict';

const ignoredFiles = require('../ignoredFiles');

describe('ignore watch files regex', () => {
  it('normal file', () => {
    const appSrc = '/root/src/';
    const isIgnored = ignoredFiles(appSrc).test('/foo');
    const isIgnoredInSrc = ignoredFiles(appSrc).test('/root/src/foo');

    expect(isIgnored).toBe(false);
    expect(isIgnoredInSrc).toBe(false);
  });

  it('node modules', () => {
    const appSrc = '/root/src/';
    const isIgnored = ignoredFiles(appSrc).test('/root/node_modules/foo');

    expect(isIgnored).toBe(true);
  });

  it('node modules inside source directory', () => {
    const appSrc = '/root/src/';
    const isIgnored = ignoredFiles(appSrc).test('/root/src/node_modules/foo');
    const isIgnoredMoreThanOneLevel = ignoredFiles(appSrc).test(
      '/root/src/bar/node_modules/foo'
    );

    expect(isIgnored).toBe(false);
    expect(isIgnoredMoreThanOneLevel).toBe(false);
  });

  it('path contains source directory', () => {
    const appSrc = '/root/src/';
    const isIgnored = ignoredFiles(appSrc).test(
      '/bar/root/src/node_modules/foo'
    );

    expect(isIgnored).toBe(true);
  });

  it('path starts with source directory', () => {
    const appSrc = '/root/src/';
    const isIgnored = ignoredFiles(appSrc).test('/root/src2/node_modules/foo');

    expect(isIgnored).toBe(true);
  });
});