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 12, 2023
1 parent 8d13692 commit 05ab1cc
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 40 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
31 changes: 29 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 All @@ -132,6 +157,7 @@ describe('@nx/playwright/plugin', () => {
'tests/run-me-2.spec.ts': '',
'tests/skip-me.spec.ts': '',
'tests/ignored/run-me.spec.ts': '',
'not-tests/run-me.spec.ts': '',
});

const { projects } = await createNodesFunction(
Expand Down Expand Up @@ -202,6 +228,7 @@ describe('@nx/playwright/plugin', () => {
`);
expect(targets['e2e-ci--tests/skip-me.spec.ts']).not.toBeDefined();
expect(targets['e2e-ci--tests/ignored/run-me.spec.ts']).not.toBeDefined();
expect(targets['e2e-ci--not-tests/run-me.spec.ts']).not.toBeDefined();
});
});

Expand Down
58 changes: 22 additions & 36 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 @@ -177,7 +154,9 @@ async function forEachTestFile(
opts.path
);
const matcher = createMatcher(opts.config.testMatch);
const ignoredMatcher = createMatcher(opts.config.testIgnore);
const ignoredMatcher = opts.config.testIgnore
? createMatcher(opts.config.testIgnore)
: () => false;
for (const file of files) {
if (matcher(file) && !ignoredMatcher(file)) {
cb(file);
Expand All @@ -192,7 +171,13 @@ function createMatcher(pattern: string | RegExp | Array<string | RegExp>) {
} else if (pattern instanceof RegExp) {
return (path: string) => pattern.test(path);
} else {
return (path: string) => minimatch(path, pattern);
return (path: string) => {
try {
return minimatch(path, pattern);
} catch (e) {
throw new Error(`Error matching ${path} with ${pattern}: ${e.message}`);
}
};
}
}

Expand Down Expand Up @@ -250,5 +235,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 05ab1cc

Please sign in to comment.