import path from "node:path"; import createEsmUtils from "esm-utils"; import installBrowser from "./tests/config/install-browser.js"; import installPrettier from "./tests/config/install-prettier.js"; const { dirname: PROJECT_ROOT } = createEsmUtils(import.meta); const isProduction = process.env.NODE_ENV === "production"; const ENABLE_CODE_COVERAGE = Boolean(process.env.ENABLE_CODE_COVERAGE); const TEST_STANDALONE = Boolean(process.env.TEST_STANDALONE); const INSTALL_PACKAGE = Boolean(process.env.INSTALL_PACKAGE); const TEST_RUNTIME = process.env.TEST_RUNTIME ?? "nodejs"; // When debugging production test, this flag can skip installing package const SKIP_PRODUCTION_INSTALL = Boolean(process.env.SKIP_PRODUCTION_INSTALL); const nodejsMajorVersion = Number(process.versions.node.split(".", 1)[0]); let PRETTIER_DIR = isProduction ? path.join(PROJECT_ROOT, "dist/prettier") : PROJECT_ROOT; let PRETTIER_INSTALLED_DIR = ""; if ( INSTALL_PACKAGE || (isProduction && !TEST_STANDALONE && !SKIP_PRODUCTION_INSTALL && TEST_RUNTIME === "nodejs") ) { PRETTIER_INSTALLED_DIR = installPrettier(PRETTIER_DIR); PRETTIER_DIR = path.join(PRETTIER_INSTALLED_DIR, "node_modules/prettier"); } if (TEST_RUNTIME === "browser") { installBrowser(); } process.env.PRETTIER_INSTALLED_DIR = PRETTIER_INSTALLED_DIR; process.env.PRETTIER_DIR = PRETTIER_DIR; const testPathIgnorePatterns = []; if (TEST_STANDALONE || TEST_RUNTIME !== "nodejs") { testPathIgnorePatterns.push("tests/integration/"); } if (isProduction) { // Only run unit test for development testPathIgnorePatterns.push("tests/unit/"); } else { // Only test bundles for production testPathIgnorePatterns.push("tests/integration/__tests__/bundle.js"); } // Currently can't load plugins in browser if (TEST_RUNTIME === "browser") { testPathIgnorePatterns.push( "tests/format/misc/front-matter/with-plugins/format.test.js", "tests/format/misc/plugins/embed-async-printer/format.test.js", "tests/format/misc/_errors_/broken-plugin/format.test.js", "tests/format/vue/with-plugins/format.test.js", "tests/format/misc/plugins/async-printer/format.test.js", "tests/format/handlebars/front-matter/toml/format.test.js", "tests/format/markdown/broken-plugins/format.test.js", "tests/format/vue/broken-plugins/format.test.js", ); } if (nodejsMajorVersion <= 18) { // Uses import attributes and `Array#toSorted()` testPathIgnorePatterns.push( "tests/integration/__tests__/help-options.js", "tests/format/html/attributes/lowercase/format.test.js", ); } if (nodejsMajorVersion <= 14) { testPathIgnorePatterns.push( "tests/integration/__tests__/plugin-parsers.js", "tests/integration/__tests__/normalize-doc.js", "tests/integration/__tests__/doc-utilities-clean-doc.js", "tests/integration/__tests__/config-invalid.js", // `@prettier/cli` uses `node:stream/consumers`, not available on Node.js v14 "tests/integration/__tests__/experimental-cli.js", // Fails on Node.js v14 "tests/dts/unit/run.js", ); } const config = { setupFiles: [ "/tests/config/format-test-setup.js", "/tests/integration/integration-test-setup.js", ], runner: // Doesn't work well on child process ENABLE_CODE_COVERAGE ? "jest-light-runner" : "jest-light-runner/child-process", testEnvironmentOptions: { customExportConditions: ["development"], }, snapshotSerializers: [ "jest-snapshot-serializer-raw", "jest-snapshot-serializer-ansi", ], testMatch: [ "/tests/format/**/format.test.js", "/tests/integration/__tests__/**/*.js", "/tests/unit/**/*.js", "/tests/dts/unit/**/*.js", ], testPathIgnorePatterns: testPathIgnorePatterns.map( (file) => `/${file}`, ), // Disabled https://github.com/nicolo-ribaudo/jest-light-runner/pull/13 // collectCoverage: ENABLE_CODE_COVERAGE, collectCoverageFrom: ["/src/**/*.js", "/bin/**/*.js"], coveragePathIgnorePatterns: [ "/src/standalone.js", "/src/document/debug.js", ], coverageReporters: ["text", "lcov"], moduleNameMapper: { "prettier-local": "/tests/config/prettier-entry.js", "prettier-standalone": "/tests/config/require-standalone.cjs", }, modulePathIgnorePatterns: [ "/dist", "/website", "/scripts/release", ], transform: {}, watchPlugins: [ "jest-watch-typeahead/filename", "jest-watch-typeahead/testname", ], }; export default config;