Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(devkit): expose TempFs via @nx/devkit/testing #30229

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions docs/shared/recipes/generators/local-generators.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,49 @@ Nx uses the paths from `tsconfig.base.json` when running plugins locally, but us
## Generator Utilities

The [`@nx/devkit` package](/nx-api/devkit/documents/nx_devkit) provides many utility functions that can be used in generators to help with modifying files, reading and updating configuration files, and working with an Abstract Syntax Tree (AST).

## Unit Testing Generators

Generators will be created with a default test suite. You can add additional tests to this test suite to ensure that your generator is working as expected.

If your generator makes use of or composes generators that make use of Inferred Tasks or Inference Plugins, you will need additional setup in your test suite to ensure that any plugins registered in your current Nx Workspace do not interfere with the unit tests.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like this. 😕

This is indeed the case.. but how are developers supposed to know that generators they're importing are using inferences plugins or not?

Can we explore creating a TempFs when we create a tree? 👀 Which is already public testing API.. and used.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don’t currently require tear down logic for when unit testing with create tree, because it’s all in-memory.

To change that API to start creating TempFs would mean countless unit tests in countless projects would start creating real temp dirs on real machines, that would not be cleaned up.

As for discoverability of whether the generator uses inference or not, that’s a separate issue.
But they should at least have an escape hatch right now if they run into it.

currently they have nothing

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could create an entirely new createTreeWithTempFs utility that could be used with the caveat that tear down logic should be run to prevent build up of temp dirs

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have an idea 🤔

The `@nx/devkit/testing` packages provides a useful utility for this purpose called `TempFs`.

You can use it to create a temporary directory and set it as the current working directory for your tests.
You should also ensure that you change the process's current working directory to the temporary directory and revert it back to the original directory after your tests have completed.

```typescript {% fileName="my-generator.spec.ts" %}
import { createTreeWithEmptyWorkspace, TempFs } from '@nx/devkit/testing';
import { myGeneratorGenerator } from './my-generator';

describe('my-generator generator', () => {
let tree: Tree;
let tempFs: TempFs;

// Note that original cwd is stored to be restored after the test
const cwd = process.cwd();

beforeEach(() => {
tree = createTreeWithEmptyWorkspace();
tempFs = new TempFs('test');
tempFs.createFileSync('nx.json', `{}`);
tree.write('nx.json', `{}`);
// Note that tree's root is set to the temp directory
tree.root = tempFs.tempDir;
// Note that process.cwd() is set to the temp directory
process.chdir(tree.root);
});

afterEach(() => {
tempFs.reset();
// Note that process.cwd() is restored to the original directory
process.chdir(cwd);
});

// Replace with your test cases
it('should run the generator', async () => {
await myGeneratorGenerator(tree, { ...opts });
expect(true).toBe(true);
});
});
```
2 changes: 1 addition & 1 deletion packages/angular/src/plugins/plugin.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { CreateNodesContextV2 } from '@nx/devkit';
import { mkdirSync, rmdirSync } from 'node:fs';
import { TempFs } from 'nx/src/internal-testing-utils/temp-fs';
import { TempFs } from '@nx/devkit/testing';
import { createNodesV2, type AngularProjectConfiguration } from './plugin';

jest.mock('nx/src/utils/cache-directory', () => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
updateNxJson,
writeJson,
} from '@nx/devkit';
import { TempFs } from '@nx/devkit/internal-testing-utils';
import { TempFs } from '@nx/devkit/testing';
import { join } from 'node:path';

let fs: TempFs;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ProjectGraph, readNxJson, type Tree, updateNxJson } from '@nx/devkit';
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
import { TempFs } from 'nx/src/internal-testing-utils/temp-fs';
import { TempFs } from '@nx/devkit/testing';
import addE2eCiTargetDefaults from './add-e2e-ci-target-defaults';

let projectGraph: ProjectGraph;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
updateNxJson,
} from '@nx/devkit';
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
import { TempFs } from 'nx/src/internal-testing-utils/temp-fs';
import { TempFs } from '@nx/devkit/testing';

let projectGraph: ProjectGraph;
jest.mock('@nx/devkit', () => ({
Expand Down
2 changes: 1 addition & 1 deletion packages/cypress/src/plugins/plugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { CreateNodesContext } from '@nx/devkit';
import { defineConfig } from 'cypress';

import { createNodesV2 } from './plugin';
import { TempFs } from 'nx/src/internal-testing-utils/temp-fs';
import { TempFs } from '@nx/devkit/testing';
import { resetWorkspaceContext } from 'nx/src/utils/workspace-context';
import { join } from 'path';
import { nxE2EPreset } from '../../plugins/cypress-preset';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
type Tree,
writeJson,
} from '@nx/devkit';
import { TempFs } from '@nx/devkit/internal-testing-utils';
import { TempFs } from '@nx/devkit/testing';
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
import { join } from 'node:path';
import { getRelativeProjectJsonSchemaPath } from 'nx/src/generators/utils/project-configuration';
Expand Down
1 change: 0 additions & 1 deletion packages/devkit/internal-testing-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@
export * from 'nx/src/internal-testing-utils/assert-valid-migrations';
export * from 'nx/src/internal-testing-utils/run-migration-against-this-workspace';
export * from 'nx/src/internal-testing-utils/with-environment';
export * from 'nx/src/internal-testing-utils/temp-fs';
export { setCwd } from './src/generators/artifact-name-and-directory-utils';
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createTreeWithEmptyWorkspace } from 'nx/src/devkit-testing-exports';
import { type Tree, readNxJson, updateNxJson } from 'nx/src/devkit-exports';
import { TempFs } from 'nx/src/internal-testing-utils/temp-fs';
import { TempFs } from 'nx/src/generators/testing-utils/temp-fs';
import { getE2EWebServerInfo } from './e2e-web-server-info-utils';

describe('getE2EWebServerInfo', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { createTreeWithEmptyWorkspace } from 'nx/src/devkit-testing-exports';
import { readNxJson, updateNxJson, type Tree } from 'nx/src/devkit-exports';
import { TempFs } from 'nx/src/internal-testing-utils/temp-fs';
import { TempFs } from 'nx/src/generators/testing-utils/temp-fs';
import { addE2eCiTargetDefaults } from './target-defaults-utils';

describe('target-defaults-utils', () => {
describe('addE2eCiTargetDefaults', () => {
let tree: Tree;
Expand Down
2 changes: 1 addition & 1 deletion packages/devkit/src/utils/add-plugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { readJson, writeJson } from 'nx/src/generators/utils/json';
import type { PackageJson } from 'nx/src/utils/package-json';
import { CreateNodesV2 } from 'nx/src/project-graph/plugins';
import { ProjectGraph } from 'nx/src/devkit-exports';
import { TempFs } from 'nx/src/internal-testing-utils/temp-fs';
import { TempFs } from 'nx/src/generators/testing-utils/temp-fs';

import { addPlugin, generateCombinations } from './add-plugin';

Expand Down
2 changes: 1 addition & 1 deletion packages/devkit/src/utils/convert-nx-executor.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TempFs } from '../../internal-testing-utils';
import { TempFs } from '../../testing';
import { convertNxExecutor } from './convert-nx-executor';

describe('Convert Nx Executor', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type Tree, readNxJson, updateNxJson } from 'nx/src/devkit-exports';
import { TempFs } from 'nx/src/internal-testing-utils/temp-fs';
import { TempFs } from 'nx/src/generators/testing-utils/temp-fs';
import { createTreeWithEmptyWorkspace } from 'nx/src/devkit-testing-exports';
import { findPluginForConfigFile } from './find-plugin-for-config-file';

Expand Down
3 changes: 2 additions & 1 deletion packages/eslint/src/executors/lint/lint.impl.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ExecutorContext } from '@nx/devkit';
import { TempFs } from '@nx/devkit/internal-testing-utils';
import { TempFs } from '@nx/devkit/testing';
import * as fs from 'fs';
import { resolve } from 'path';
import type { Schema } from './schema';
Expand All @@ -24,6 +24,7 @@ class MockESLint {
loadFormatter = mockLoadFormatter;
isPathIgnored = mockIsPathIgnored;
lintFiles = mockLintFiles;

calculateConfigForFile(file: string) {
return { file: file };
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
type ProjectGraph,
type Tree,
} from '@nx/devkit';
import { TempFs } from '@nx/devkit/internal-testing-utils';
import { TempFs } from '@nx/devkit/testing';
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
import { join } from 'node:path';
import {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { readJson, writeJson, type Tree } from '@nx/devkit';
import { TempFs } from '@nx/devkit/internal-testing-utils';
import { TempFs } from '@nx/devkit/testing';
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
import migration from './update-typescript-eslint-v8-13-0';

Expand Down
2 changes: 1 addition & 1 deletion packages/eslint/src/plugins/plugin.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CreateNodesContextV2 } from '@nx/devkit';
import { minimatch } from 'minimatch';
import { TempFs } from 'nx/src/internal-testing-utils/temp-fs';
import { TempFs } from '@nx/devkit/testing';
import { createNodesV2, EslintPluginOptions } from './plugin';
import { mkdirSync, rmdirSync } from 'fs';

Expand Down
2 changes: 1 addition & 1 deletion packages/eslint/src/utils/config-file.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TempFs } from '@nx/devkit/internal-testing-utils';
import { TempFs } from '@nx/devkit/testing';
import { join } from 'path';
import {
ESLINT_FLAT_CONFIG_FILENAMES,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
type Tree,
writeJson,
} from '@nx/devkit';
import { TempFs } from '@nx/devkit/internal-testing-utils';
import { TempFs } from '@nx/devkit/testing';
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
import { join } from 'node:path';
import { getRelativeProjectJsonSchemaPath } from 'nx/src/generators/utils/project-configuration';
Expand Down
2 changes: 1 addition & 1 deletion packages/gradle/plugin.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CreateNodesContext } from '@nx/devkit';
import { TempFs } from '@nx/devkit/internal-testing-utils';
import { TempFs } from '@nx/devkit/testing';
import { createNodesV2 } from './plugin';
import { type GradleReport } from './src/utils/get-gradle-report';

Expand Down
2 changes: 1 addition & 1 deletion packages/gradle/src/plugin/nodes.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CreateNodesContext } from '@nx/devkit';

import { TempFs } from 'nx/src/internal-testing-utils/temp-fs';
import { TempFs } from '@nx/devkit/testing';
import { type GradleReport } from '../utils/get-gradle-report';

let gradleReport: GradleReport;
Expand Down
2 changes: 1 addition & 1 deletion packages/gradle/src/utils/exec-gradle.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TempFs } from 'nx/src/internal-testing-utils/temp-fs';
import { TempFs } from '@nx/devkit/testing';
import { findGraldewFile } from './exec-gradle';

describe('exec gradle', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/gradle/src/utils/split-config-files.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TempFs } from 'nx/src/internal-testing-utils/temp-fs';
import { TempFs } from '@nx/devkit/testing';
import { splitConfigFiles } from './split-config-files';

describe('split config files', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
type ProjectGraph,
type Tree,
} from '@nx/devkit';
import { TempFs } from '@nx/devkit/internal-testing-utils';
import { TempFs } from '@nx/devkit/testing';
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
import { join } from 'node:path';
import {
Expand Down
2 changes: 1 addition & 1 deletion packages/jest/src/plugins/plugin.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CreateNodesContext } from '@nx/devkit';
import { TempFs } from 'nx/src/internal-testing-utils/temp-fs';
import { TempFs } from '@nx/devkit/testing';
import { join } from 'path';
import { createNodesV2 } from './plugin';

Expand Down
2 changes: 1 addition & 1 deletion packages/js/src/plugins/typescript/plugin.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { detectPackageManager, type CreateNodesContext } from '@nx/devkit';
import { TempFs } from '@nx/devkit/internal-testing-utils';
import { TempFs } from '@nx/devkit/testing';
import picomatch = require('picomatch');
import { mkdirSync, rmdirSync } from 'node:fs';
// eslint-disable-next-line @typescript-eslint/no-restricted-imports
Expand Down
2 changes: 1 addition & 1 deletion packages/js/src/utils/buildable-libs-utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DependencyType, ProjectGraph, TaskGraph } from '@nx/devkit';
import { TempFs } from '@nx/devkit/internal-testing-utils';
import { TempFs } from '@nx/devkit/testing';
import { readFileSync } from 'fs';
import {
calculateDependenciesFromTaskGraph,
Expand Down
2 changes: 1 addition & 1 deletion packages/js/src/utils/npm-config.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ExecException } from 'child_process';
import { TempFs } from 'nx/src/internal-testing-utils/temp-fs';
import { TempFs } from '@nx/devkit/testing';
import { PackageJson } from 'nx/src/utils/package-json';
import { join } from 'path';
import { getNpmRegistry, getNpmTag, parseRegistryOptions } from './npm-config';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
type Tree,
writeJson,
} from '@nx/devkit';
import { TempFs } from '@nx/devkit/internal-testing-utils';
import { TempFs } from '@nx/devkit/testing';
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
import { join } from 'node:path';
import { getRelativeProjectJsonSchemaPath } from 'nx/src/generators/utils/project-configuration';
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/plugins/plugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { CreateNodesContext } from '@nx/devkit';
import type { NextConfig } from 'next';

import { createNodesV2 } from './plugin';
import { TempFs } from '@nx/devkit/internal-testing-utils';
import { TempFs } from '@nx/devkit/testing';

describe('@nx/next/plugin', () => {
let createNodesFunction = createNodesV2[1];
Expand Down
2 changes: 1 addition & 1 deletion packages/nuxt/src/plugins/plugin.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CreateNodesContext } from '@nx/devkit';
import { createNodes } from './plugin';
import { TempFs } from 'nx/src/internal-testing-utils/temp-fs';
import { TempFs } from '@nx/devkit/testing';

jest.mock('@nx/devkit/src/utils/config-utils', () => ({
loadConfigFile: jest.fn().mockImplementation(() => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { join } from 'path';
import { ProjectFileMap, ProjectGraph } from '../../../config/project-graph';
import { TempFs } from '../../../internal-testing-utils/temp-fs';
import { TempFs } from '../../../generators/testing-utils/temp-fs';
import { createNxReleaseConfig } from './config';

expect.addSnapshotSerializer({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { readFileSync } from 'fs';
import { join } from 'path';
import { TempFs } from '../../../internal-testing-utils/temp-fs';
import { TempFs } from '../../../generators/testing-utils/temp-fs';
import { IMPLICIT_DEFAULT_RELEASE_GROUP } from './config';
import { ReleaseGroupWithName } from './filter-release-groups';
import {
Expand Down Expand Up @@ -1150,6 +1150,7 @@ describe('version-plans', () => {
});

let createdOnAccumulator = 1;

function versionPlan({
name,
content,
Expand Down
2 changes: 1 addition & 1 deletion packages/nx/src/config/to-project-name.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { toProjectName } from './to-project-name';
import { TempFs } from '../internal-testing-utils/temp-fs';
import { TempFs } from '../generators/testing-utils/temp-fs';
import { withEnvironmentVariables } from '../internal-testing-utils/with-environment';
import { retrieveProjectConfigurations } from '../project-graph/utils/retrieve-workspace-files';
import { readNxJson } from './configuration';
Expand Down
1 change: 1 addition & 0 deletions packages/nx/src/devkit-testing-exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export {
createTreeWithEmptyV1Workspace,
} from './generators/testing-utils/create-tree-with-empty-workspace';
export { createTree } from './generators/testing-utils/create-tree';
export { TempFs } from './generators/testing-utils/temp-fs';
Loading