UNPKG

@robby-rabbitman/nx-plus-web-test-runner

Version:
87 lines 3.47 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createNodesV2 = exports.DEFAULT_WEB_TEST_RUNNER_TARGET_NAME = void 0; const devkit_1 = require("@nx/devkit"); const fs_1 = require("fs"); const path_1 = require("path"); /** * The glob pattern to match `Web Test Runner` configuration files. * * https://modern-web.dev/docs/test-runner/cli-and-configuration/#configuration-file */ const WEB_TEST_RUNNER_CONFIG_FILE_NAME_GLOB = '**/@(web-test-runner|wtr).config.@(js|cjs|mjs)'; /** * The name of the `Web Test Runner` command. * * https://modern-web.dev/docs/test-runner/overview/#basic-commands */ const WEB_TEST_RUNNER_COMMAND = 'web-test-runner'; exports.DEFAULT_WEB_TEST_RUNNER_TARGET_NAME = 'test'; exports.createNodesV2 = [ WEB_TEST_RUNNER_CONFIG_FILE_NAME_GLOB, (webTestRunnerConfigPaths, schema, context) => (0, devkit_1.createNodesFromFiles)(createWebTestRunnerTarget, webTestRunnerConfigPaths, schema, context), ]; const createWebTestRunnerTarget = (webTestRunnerConfigPath, userOptions, context) => { const options = normalizeWebTestRunnerOptions(userOptions); const { testTargetName, testTargetConfig } = options; const webTestRunnerInformation = extractWebTestRunnerInformation(webTestRunnerConfigPath, context.workspaceRoot); if (!webTestRunnerInformation.isProject) { return {}; } /** * TODO: '@web/test-runner' does not export the `TestRunnerCliArgs` type - add * them when its exported from their public api * * https://github.com/modernweb-dev/web/blob/17cfc0d70f46b321912e4506b2cccae1b16b1534/packages/test-runner/src/config/readCliArgs.ts#L7-L34 */ const webTestRunnerDefaultCliArgs = { config: webTestRunnerInformation.configFileName, }; const webTestRunnerTargetConfiguration = { command: WEB_TEST_RUNNER_COMMAND, ...testTargetConfig, options: { cwd: webTestRunnerInformation.configDir, ...webTestRunnerDefaultCliArgs, ...testTargetConfig.options, }, }; return { projects: { [webTestRunnerInformation.configDir]: { targets: { [testTargetName]: webTestRunnerTargetConfiguration, }, }, }, }; }; function normalizeWebTestRunnerOptions(userOptions) { const normalizedOptions = { testTargetName: exports.DEFAULT_WEB_TEST_RUNNER_TARGET_NAME, testTargetConfig: {}, ...userOptions, }; /** Make sure `testTargetName` is not an empty string. */ if (normalizedOptions.testTargetName === '') { normalizedOptions.testTargetName = exports.DEFAULT_WEB_TEST_RUNNER_TARGET_NAME; } return normalizedOptions; } function extractWebTestRunnerInformation(webTestRunnerConfigPath, workspaceRoot) { const configDir = (0, path_1.dirname)(webTestRunnerConfigPath); return { configFileName: (0, path_1.basename)(webTestRunnerConfigPath), configDir, isProject: isProject(configDir, workspaceRoot), }; } /** * Returns true if the directory contains a `project.json` or `package.json` * file. */ function isProject(directory, workspaceRoot) { return ((0, fs_1.existsSync)((0, path_1.join)(workspaceRoot, directory, 'project.json')) || (0, fs_1.existsSync)((0, path_1.join)(workspaceRoot, directory, 'package.json'))); } //# sourceMappingURL=web-test-runner.plugin.js.map