Skip to content

Commit

Permalink
cleanup(testing): clear require cache when loading playwright config
Browse files Browse the repository at this point in the history
  • Loading branch information
AgentEnder committed Dec 7, 2023
1 parent 7264d8c commit 6e2f57b
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,9 @@ export async function configurationGenerator(

if (!hasPlugin) {
addE2eTarget(tree, options);
setupE2ETargetDefaults(tree);
}

setupE2ETargetDefaults(tree);

tasks.push(
await addLinterToPlaywrightProject(tree, {
project: options.project,
Expand Down
29 changes: 27 additions & 2 deletions packages/playwright/src/plugins/plugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ describe('@nx/playwright/plugin', () => {
expect(projects).toMatchInlineSnapshot(`
{
".": {
"projectType": "library",
"root": ".",
"targets": {
"e2e": {
Expand All @@ -68,6 +67,18 @@ describe('@nx/playwright/plugin', () => {
"{projectRoot}/test-results",
],
},
"e2e-ci": {
"cache": true,
"dependsOn": [],
"executor": "nx:noop",
"inputs": [
"default",
"^production",
],
"outputs": [
"{projectRoot}/test-results",
],
},
},
},
}
Expand All @@ -93,7 +104,6 @@ describe('@nx/playwright/plugin', () => {
expect(projects).toMatchInlineSnapshot(`
{
".": {
"projectType": "library",
"root": ".",
"targets": {
"e2e": {
Expand All @@ -113,6 +123,21 @@ describe('@nx/playwright/plugin', () => {
"{projectRoot}/test-results",
],
},
"e2e-ci": {
"cache": true,
"dependsOn": [],
"executor": "nx:noop",
"inputs": [
"default",
"^production",
],
"outputs": [
"{projectRoot}/playwright-report",
"{projectRoot}/test-results/report.json",
"{projectRoot}/test-results/html",
"{projectRoot}/test-results",
],
},
},
},
}
Expand Down
46 changes: 12 additions & 34 deletions packages/playwright/src/plugins/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { readdirSync } from 'fs';
import { readTargetDefaultsForTarget } from 'nx/src/project-graph/utils/project-configuration-utils';
import { basename, dirname, join, relative } from 'path';

import {
Expand Down Expand Up @@ -40,13 +39,11 @@ export const createNodes: CreateNodes<PlaywrightPluginOptions> = [
}

const normalizedOptions = normalizeOptions(options);
const projectName = basename(projectRoot);

return {
projects: {
[projectName]: {
[projectRoot]: {
root: projectRoot,
projectType: 'library',
targets: await buildPlaywrightTargets(
configFilePath,
projectRoot,
Expand All @@ -69,12 +66,6 @@ async function buildPlaywrightTargets(
join(context.workspaceRoot, configFilePath)
);

const targetDefaults = readTargetDefaultsForTarget(
options.targetName,
context.nxJsonConfiguration.targetDefaults,
'nx:run-commands'
);

const namedInputs = getNamedInputs(projectRoot, context);

const targets: Record<string, TargetConfiguration<unknown>> = {};
Expand All @@ -88,43 +79,29 @@ async function buildPlaywrightTargets(

targets[options.targetName] = {
...baseTargetConfig,
cache: targetDefaults?.cache ?? true,
cache: true,
inputs:
targetDefaults?.inputs ?? 'production' in namedInputs
'production' in namedInputs
? ['default', '^production']
: ['default', '^default'],
outputs:
targetDefaults?.outputs ?? getOutputs(projectRoot, playwrightConfig),
options: {
...baseTargetConfig.options,
...targetDefaults?.options,
},
outputs: getOutputs(projectRoot, playwrightConfig),
};

if (options.ciTargetName) {
const ciTargetDefaults = readTargetDefaultsForTarget(
options.ciTargetName,
context.nxJsonConfiguration.targetDefaults,
'nx:run-commands'
);

const ciBaseTargetConfig: TargetConfiguration = {
...baseTargetConfig,
cache: ciTargetDefaults?.cache ?? true,
cache: true,
inputs:
ciTargetDefaults?.inputs ?? 'production' in namedInputs
'production' in namedInputs
? ['default', '^production']
: ['default', '^default'],
outputs:
ciTargetDefaults?.outputs ?? getOutputs(projectRoot, playwrightConfig),
options: {
...baseTargetConfig.options,
...ciTargetDefaults?.options,
},
outputs: getOutputs(projectRoot, playwrightConfig),
};

const testDir =
joinPathFragments(projectRoot, playwrightConfig.testDir) ?? projectRoot;
const testDir = playwrightConfig.testDir
? joinPathFragments(projectRoot, playwrightConfig.testDir)
: projectRoot;

// Playwright defaults to the following pattern.
playwrightConfig.testMatch ??= '**/*.@(spec|test).?(c|m)[jt]s?(x)';

Expand Down Expand Up @@ -250,5 +227,6 @@ function normalizeOptions(options: PlaywrightPluginOptions): NormalizedOptions {
return {
...options,
targetName: options.targetName ?? 'e2e',
ciTargetName: options.ciTargetName ?? 'e2e-ci',
};
}
12 changes: 12 additions & 0 deletions packages/playwright/src/utils/load-config-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ export async function loadPlaywrightConfig(
if (tsConfigPath) {
const unregisterTsProject = registerTsProject(tsConfigPath);
try {
// Require's cache doesn't notice when the file is updated, and
// this function is ran during daemon operation. If the config file
// is updated, we need to read its new contents, so we need to clear the cache.
// We can't just delete the cache entry for the config file, because
// it might have imports that need to be updated as well.
clearRequireCache();
// ts-node doesn't support dynamic import, so we need to use require
module = require(configFilePath);
} finally {
Expand All @@ -34,3 +40,9 @@ export async function loadPlaywrightConfig(
return module.default ?? module;
}
}

function clearRequireCache() {
Object.keys(require.cache).forEach((key) => {
delete require.cache[key];
});
}

0 comments on commit 6e2f57b

Please sign in to comment.