Back to Repositories

Testing Markdown String Processing and Template Literals in Parcel

This test suite validates Markdown string escaping and template literal functionality in Parcel’s diagnostic module. It ensures proper handling of special characters and formatting tags while maintaining Markdown syntax integrity.

Test Coverage Overview

The test suite provides comprehensive coverage for two main components: escapeMarkdown function and markdown tagged template literals.

  • Tests various Markdown special characters (*_~)
  • Validates backslash escaping scenarios
  • Covers formatting functions: bold, italic, underline, strikethrough
  • Examines template literal placeholder behavior

Implementation Analysis

The testing approach uses Jest’s describe/it pattern for organized test grouping. Each test case employs Node’s assert.strictEqual for precise string comparison, ensuring exact matching of escaped sequences.

The implementation leverages Flow type checking and handles edge cases including null values, undefined, and Symbol.toPrimitive conversions.

Technical Details

  • Testing Framework: Jest
  • Assertion Library: Node.js assert module
  • Type System: Flow
  • Test Structure: Describe blocks with individual test cases
  • Module Testing: ‘../src/diagnostic’ module imports

Best Practices Demonstrated

The test suite exemplifies strong testing practices through clear test case organization and comprehensive edge case coverage.

  • Systematic test case naming and grouping
  • Isolated function testing
  • Explicit expected vs actual value comparisons
  • Coverage of special character combinations
  • Template literal behavior verification

parcel-bundler/parcel

packages/core/diagnostic/test/markdown.test.js

            
// @flow
import assert from 'assert';

import {escapeMarkdown, md} from '../src/diagnostic';

describe('escapeMarkdown', () => {
  it('returns an escaped string 01', () => {
    assert.strictEqual('\\*test\\*', escapeMarkdown('*test*'));
  });

  it('returns an escaped string 02', () => {
    assert.strictEqual('\\_test\\_', escapeMarkdown('_test_'));
  });

  it('returns an escaped string 03', () => {
    assert.strictEqual('\\~test\\~', escapeMarkdown('~test~'));
  });

  it('returns an escaped string 04', () => {
    assert.strictEqual('\\*\\_\\~test\\~\\_\\*', escapeMarkdown('*_~test~_*'));
  });

  it('returns an escaped string with backslash 01', () => {
    assert.strictEqual('\\\\test\\\\', escapeMarkdown('\\test\\'));
  });

  it('returns an escaped string with backslash 02', () => {
    assert.strictEqual('\\\\\\*test\\*\\\\', escapeMarkdown('\\*test*\\'));
  });
});

describe('md tagged template literal', () => {
  it('bold placeholder', () => {
    assert.strictEqual(
      '*Test*: **\\_abc\\_**',
      md`*Test*: ${md.bold('_abc_')}`,
    );
  });

  it('italic placeholder', () => {
    assert.strictEqual(
      '*Test*: _\\_abc\\__',
      md`*Test*: ${md.italic('_abc_')}`,
    );
  });

  it('underline placeholder', () => {
    assert.strictEqual(
      '*Test*: __\\_abc\\___',
      md`*Test*: ${md.underline('_abc_')}`,
    );
  });

  it('strikethrough placeholder', () => {
    assert.strictEqual(
      '*Test*: ~~\\_abc\\_~~',
      md`*Test*: ${md.strikethrough('_abc_')}`,
    );
  });

  it('escapes only placeholders', () => {
    assert.strictEqual('*Test*: \\_abc\\_', md`*Test*: ${'_abc_'}`);
  });

  it('behaves like native template literal', () => {
    let v = {
      toString() {
        return 'a';
      },
      // $FlowFixMe[invalid-computed-prop]
      [Symbol.toPrimitive]() {
        return 'b';
      },
    };
    assert.strictEqual('Test: b', md`Test: ${v}`);
  });

  it('supports null and undefined', () => {
    assert.strictEqual('Test: undefined null', md`Test: ${undefined} ${null}`);
  });
});