Introducing a new way to review your PRs.

Read the announcement

less / less.js

Less. The dynamic stylesheet language.

View on GitHub

17 documents · Updated Sep 7, 2026

docs / test-and-fixture-system

Test and fixture system

The test and fixture system checks that Less input produces stable CSS and stable errors across Node.js, browser, plugin, export, and source-map use cases. It uses the shared @less/test-data corpus as the main contract between the compiler and its test runners. For the wider compiler flow, see Less compiler pipeline; for runtime-specific behavior, see Node, browser, and CLI integrations.

Test coverage model

The test system compares a compiler result with an expected artifact or a behavior rule. This keeps language behavior, option behavior, public package entry points, and browser behavior under one test command while allowing each area to use the check that fits it best.

Fixture-driven compilation

The fixture corpus describes a small input/output contract for each case. A Less file is the input, a sibling CSS file is the normal expected result, and special cases use a text or JSON expectation instead. The runner discovers these files from @less/test-data and applies any nearby styles.config.cjs settings before it renders the input.

flowchart LR
    L[Less fixture] --> C[Render with fixture options]
    C --> R{Expected result type}
    R -->|CSS| D[Compare generated CSS]
    R -->|Error text| E[Compare reported error]
    R -->|Source map| M[Compare map and mapping validity]

The default suite covers the language and compiler rules in tests-unit. Configuration cases in tests-config add options, imports, plugins, URL rules, variable injection, and source-map settings. The runner also checks synchronous rendering, warnings, import tracking, global function registration, and selected remote-import behavior. References: packages/…/index.js• L66-247, packages/…/less-test.js• L497-718, packages/…/package.json• L1-18

Test orchestration

The less package owns the test commands. A full test run builds the distributable code, runs the Node suites, checks command-line options and plugins, starts the browser test setup, and then runs the generated browser pages. The Node-only task keeps the ESM, CJS, option, and plugin checks for fast CI and pre-publish use.

The top-level package forwards test and test:node into packages/less. Inside that package, quicktest skips the build step, while sourcemap-test creates a small web test environment for source-map output. References: package.json• L7-18, packages/…/package.json• L58-68, packages/…/Gruntfile.cjs• L347-399

Node compatibility coverage

Node coverage checks both compiler behavior and the ways applications load the package. The main ESM runner exercises the broad fixture corpus through the source entry point, while smaller checks protect the published CJS path and the package export map.

Module and API contracts

The module tests protect the user-facing contract for import less from 'less' and require('less'). They check promise-based and callback-based rendering, normal module properties, and the fact that the CJS export is a compiler object rather than a thenable value. A second CJS suite renders representative fixtures through dist/less-node.cjs, so a successful source test cannot hide a broken built artifact.

The export checks also verify that the browser condition resolves to the browser distributable and that Webpack can bundle that path for a web target. These checks connect package metadata to the artifacts produced by the build system. See Packaging and distributable builds for the artifact lifecycle. References: packages/…/test-es6.js• L1-29, packages/…/test-cjs.cjs• L1-45, packages/…/test-cjs-suite.cjs• L1-46, packages/…/import-patterns.cjs• L1-32, packages/…/webpack-browser.cjs• L1-69

Command-line and plugin checks

The Node task checks option combinations that are easy to break at the CLI boundary. It also runs representative plugin commands for compression, autoprefixing, source maps, and multiple plugins. These checks confirm that command-line values reach the compiler and that plugins can be loaded in more than one working directory. See Plugin architecture for the plugin lifecycle and extension points. References: packages/…/Gruntfile.cjs• L219-254, packages/…/Gruntfile.cjs• L359-386

Browser compatibility coverage

Browser coverage checks client-side Less processing rather than only checking the browser bundle as a file. The browser runner builds HTML pages from fixture lists, loads the Less and expected CSS as linked stylesheets, waits for browser compilation, and compares the resulting style text.

Generated browser pages

The browser generator uses a configuration entry to pair a fixture set with helper scripts, a Mocha specification, and an output HTML page. The normal browser set covers the unit corpus. Separate sets cover errors, strict units, URL rewriting, root paths, production output, injected variables, and plugin types. Some Node-only or browser-incompatible fixtures are excluded from the browser set so each environment tests behavior it can actually provide.

The local runner serves the repository and uses Playwright Chromium to load each generated page. It captures Mocha results, browser console output, page errors, and optional coverage data. References: packages/…/runner.config.cjs• L11-183, packages/…/generate.js• L19-82, packages/…/runner.js• L156-207

Browser result comparison

The shared browser helper waits for Less to finish and compares generated style text with the expected CSS file. It normalizes browser URL resolution and legacy Internet Explorer style text before comparison. Error pages use the matching .txt fixture and compare the displayed or logged message after removing browser-only details such as stack traces and local paths.

The browser checks therefore cover both the compiler result and the browser integration around it. The configured Sauce Labs matrix extends this model to desktop and mobile browser targets, while local Playwright provides the repeatable runner used by the current browser task. References: packages/…/common.js• L41-125, packages/…/common.js• L148-249, packages/…/Gruntfile.cjs• L18-89, packages/…/Gruntfile.cjs• L304-345

Plugin and export coverage

Plugin fixtures check that extension points work at the stage where users expect them to work. Each plugin test pairs a small plugin with a Less input and an expected CSS result, which makes a lifecycle change visible without requiring a large application fixture.

Plugin lifecycle cases

The corpus covers four plugin roles. A preprocessor changes the Less source before normal parsing, a visitor changes the parsed tree, a postprocessor changes the generated CSS, and a file manager changes how an import is found. Node and browser runners load the same roles through their local plugin fixtures, then compare the final CSS with the shared expectation.

Plugin roleContract shown by the fixture
PreprocessorInjected source can affect later Less evaluation.
VisitorA tree change can remove or alter output.
PostprocessorCSS can be changed after compiler output.
File managerA custom import source can satisfy a non-standard file name.

The browser plugin cases use the browser global form of each plugin and the Node cases use the package test plugin form. This split checks that the same extension idea survives both runtime environments. References: packages/…/index.cjs• L1-19, packages/…/index.cjs• L1-24, packages/…/index.cjs• L1-14, packages/…/index.cjs• L1-19, packages/…/preProcessor.less• L1-3, packages/…/postProcessor.css• L1-4, packages/…/filemanager.less• L1-4

Package export cases

Export tests treat package resolution as a product behavior. They verify that Node ESM, Node CJS, and a browser-targeted bundler each receive a usable Less API from the package name. The Webpack case is important because browser resolution must select a browser-safe artifact without exposing Node-only module dependencies. References: packages/…/import-patterns.cjs• L11-32, packages/…/webpack-browser.cjs• L21-61, packages/…/webpack-browser-entry.js• L1-13, packages/…/package.json• L25-41

Source-map cases

Source-map tests protect the link between generated CSS and its Less sources. They compare the full map against an expected JSON artifact, then perform additional checks that mapped sources exist and that source content and line references are plausible.

Source-map variants

The source-map corpus covers normal maps, URL annotations, root paths, base paths, embedded source content, variable selectors, disabled URL annotations, and empty output. The fixture configuration supplies the map options and can also supply global variables. Expected maps live with the test runner or the fixture set, so the test can represent both normal output and path-sensitive output.

The map check normalizes path separators before comparison. It replaces controlled path tokens in expected data so the same fixture can run on different machines. A map with no CSS must remain absent, while a map without a URL annotation must still match the separately returned map value. References: packages/…/index.js• L140-180, packages/…/less-test.js• L150-329, packages/…/styles.config.cjs• L1-12, packages/…/basic.less• L1-27, packages/…/sourcemaps-url.json• L1, packages/…/sourcemaps-rootpath.json• L1, packages/…/sourcemaps-include-source.json• L1

Mapping validation

After the expected map matches, the runner asks a source-map consumer to resolve each non-empty CSS line. It checks that mapped source names appear in the map and that mapped line numbers fit the available source content. This catches maps that have the right serialized shape but point to invalid source locations. References: packages/…/less-test.js• L81-147, packages/…/less-test.js• L208-235

Shared fixture corpus

@less/test-data is the shared language used by the Node and browser suites. Its layout separates normal CSS output, option-specific behavior, and expected failures. A fixture directory can add a local styles.config.cjs file to set the Less options for that group without changing the runner itself.

CSS and Less pairs

The tests-unit tree is the main regression corpus for Less language behavior. It groups cases by user-visible feature such as variables, mixins, imports, selectors, at-rules, URLs, functions, operations, and empty output. Most cases use a .less and .css pair, which lets the default runner compare the complete generated stylesheet.

The default unit configuration enables browser-safe common settings for this corpus. A case can still use a more specific configuration tree when it needs strict units, strict math, injected variables, or another option. References: packages/…/package.json• L1-18, packages/…/styles.config.cjs• L1-9, packages/…/less-test.js• L578-627, packages/…/less-test.js• L663-693

Error fixtures

The tests-error tree defines failure behavior as carefully as success behavior. Each parse or evaluation input has a matching .txt file with the expected error text. The Node runner compares the complete error string after path replacement, while the browser runner compares the message shown in the page or sent to the console.

Separate configuration cases cover JavaScript type errors and the error raised when inline JavaScript is disabled. These fixtures make option-controlled failure behavior part of the public contract instead of treating it as an incidental log message. References: packages/…/index.js• L155-160, packages/…/less-test.js• L366-405, packages/…/runner.config.cjs• L41-60, packages/…/styles.config.cjs• L1-9, packages/…/no-js-errors.txt• L1-4

Fixture configuration

Fixture settings are loaded from the nearest styles.config.cjs file and merged with options supplied by the test set. This lets one corpus support several execution modes without copying its Less source. Variable files such as globalVars/*.json and modifyVars/*.json provide request-time values when a fixture needs them.

The runner adds the fixture directory to import paths and replaces machine-specific path markers in expected CSS, errors, and maps. This keeps comparisons focused on compiler behavior rather than the local checkout path. References: packages/…/less-test.js• L426-467, packages/…/less-test.js• L578-627, packages/…/index.js• L182-207

Change checklist

When these files or sections are changed, remember to consider these:

  • A new .less fixture needs the matching .css, .txt, or .json expectation unless its test set has a dedicated verifier.
  • A new tests-config option case needs the local styles.config.cjs path and the matching Node or browser runner entry.
  • A browser fixture must have a generated page configuration and a helper/spec pair that can compare its output in a DOM.
  • A source-map fixture must keep its expected map path aligned with the name mapping in test/index.js and less-test.js.
  • A package export or build entry change must be checked in the ESM, CJS, and browser-bundler paths.
  • A plugin fixture change must keep the plugin loading form aligned between the Node module and browser global cases.

Important failure modes

TriggerConsequenceGuard
A normal Less fixture has no sibling CSS fileThe default runner skips the fixture instead of checking its outputAdd the expected CSS file or assign a verifier that owns another expectation type.
A source-map path or URL annotation changesThe map comparison fails even when CSS output looks correctUpdate the matching map expectation and keep path tokens portable.
A browser test uses a Node-only fixture or optionThe generated page cannot load or compare the case reliablyKeep environment-specific exclusions in the browser configuration.
The package browser export points to a missing or wrong artifactBrowser bundlers resolve the wrong module or fail on Node dependenciesRun the export and Webpack browser checks after changing package exports or builds.
A fixture error message changes its path or stack shapeNode and browser error comparisons report a mismatchPreserve the replacement rules or update the shared expected error contract deliberately.
A plugin is loaded in one runtime form onlyNode or browser plugin coverage can pass while the other form is brokenKeep the corresponding plugin helper and browser option entry together.

Key files

FileWhy to read it
packages/…/Gruntfile.cjsDefines the full, Node-only, browser, option, plugin, and source-map test tasks.
packages/…/index.jsMaps fixture groups to the checks that validate CSS, errors, imports, warnings, and source maps.
packages/…/less-test.jsProvides the shared fixture loader, option merge, comparison rules, and result reporting.
packages/…/runner.config.cjsShows how fixture groups become browser test pages.
packages/…/generate.jsShows how configured pages are generated and run.
packages/…/common.jsDefines browser CSS, error, URL, and legacy-browser comparisons.
packages/…/runner.jsDefines the local browser execution and result collection boundary.
packages/test-data/tests-unitHolds the main Less/CSS regression corpus for language behavior.
packages/test-data/tests-configHolds option, plugin, URL, variable, and source-map fixtures.
packages/test-data/tests-errorHolds Less inputs and expected parse or evaluation errors.
packages/less/test/exportsProtects package import patterns and browser bundler resolution.
packages/less/test/sourcemapsHolds expected source-map JSON for path and mapping cases.