nrwl / nx

Smart Monorepos · Fast CI
https://nx.dev
MIT License
23.28k stars 2.32k forks source link

Plugin end-to-end tests do not work out-of-the-box #10084

Closed gnuechtel closed 2 years ago

gnuechtel commented 2 years ago

I started plugin development with Nx and end-to-end tests do not work out-of-the box.

There were two hurdles to jump over:

I will provide a solution/work-around here. But I think it should be included in the end-to-end test setup or project setup.

Furthermore, I believe the package name is not created as intended or I miss something by reading the documentation.

Steps to Reproduce

Initialize a new Nx plugins repository

We create a new plugin "my-plugin" with a new monorepo "my-org":

PS D:\Development\workspaces\git> npx create-nx-plugin@14.0.5 my-org --pluginName my-plugin
Need to install the following packages:
  create-nx-plugin@14.0.5
Ok to proceed? (y) y
Creating a sandbox with Nx...
new my-org "--pluginName" "my-plugin" --preset=empty --collection=@nrwl/workspace
CREATE my-org/nx.json
CREATE my-org/package.json
CREATE my-org/README.md
CREATE my-org/tools/generators/.gitkeep
CREATE my-org/tools/tsconfig.tools.json
CREATE my-org/tsconfig.base.json
CREATE my-org/.editorconfig
CREATE my-org/.gitignore
CREATE my-org/.prettierignore
CREATE my-org/.vscode/extensions.json
CREATE my-org/workspace.json
CREATE my-org/.prettierrc
CREATE my-org/apps/.gitkeep
CREATE my-org/libs/.gitkeep

added 406 packages, and audited 407 packages in 20s

42 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities
warning: LF will be replaced by CRLF in .editorconfig.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in .gitignore.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in .prettierignore.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in .prettierrc.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in .vscode/extensions.json.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in README.md.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in nx.json.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in package-lock.json.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in package.json.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in tools/tsconfig.tools.json.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in tsconfig.base.json.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in workspace.json.
The file will have its original line endings in your working directory
Successfully initialized git.

added 4 packages, and audited 411 packages in 5s

42 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities
nx generate @nrwl/nx-plugin:plugin my-plugin --importPath=my-org/my-plugin
CREATE packages/my-plugin/package.json
CREATE packages/my-plugin/README.md
CREATE packages/my-plugin/src/index.ts
CREATE packages/my-plugin/tsconfig.json
CREATE packages/my-plugin/tsconfig.lib.json
CREATE packages/my-plugin/project.json
UPDATE workspace.json
UPDATE tsconfig.base.json
CREATE .eslintrc.json
UPDATE .vscode/extensions.json
UPDATE package.json
CREATE packages/my-plugin/.eslintrc.json
CREATE jest.config.ts
CREATE jest.preset.ts
CREATE packages/my-plugin/jest.config.ts
CREATE packages/my-plugin/tsconfig.spec.json
CREATE packages/my-plugin/executors.json
CREATE packages/my-plugin/generators.json
CREATE packages/my-plugin/src/generators/my-plugin/files/src/index.ts__template__
CREATE packages/my-plugin/src/generators/my-plugin/generator.spec.ts
CREATE packages/my-plugin/src/generators/my-plugin/generator.ts
CREATE packages/my-plugin/src/generators/my-plugin/schema.d.ts
CREATE packages/my-plugin/src/generators/my-plugin/schema.json
CREATE packages/my-plugin/src/executors/build/executor.spec.ts
CREATE packages/my-plugin/src/executors/build/executor.ts
CREATE packages/my-plugin/src/executors/build/schema.d.ts
CREATE packages/my-plugin/src/executors/build/schema.json
CREATE e2e/my-plugin-e2e/tests/my-plugin.spec.ts
CREATE e2e/my-plugin-e2e/tsconfig.json
CREATE e2e/my-plugin-e2e/project.json
CREATE e2e/my-plugin-e2e/jest.config.ts
CREATE e2e/my-plugin-e2e/tsconfig.spec.json

added 90 packages, changed 1 package, and audited 501 packages in 8s

61 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities

That works.

Invoke end-to-end-tests

Now we want to invoke the end-to-end tests for the just created plugin.

First try


PS D:\Development\workspaces\git\my-org> npx nx e2e my-plugin-e2e

 FAIL   my-plugin-e2e  e2e/my-plugin-e2e/tests/my-plugin.spec.ts (90.159 s)
  my-plugin e2e
    × should create my-plugin (33383 ms)
    --directory
      × should create src in the specified directory (27338 ms)
    --tags
      × should add tags to the project (28159 ms)

  ● my-plugin e2e › should create my-plugin

    Command failed: npm install

      at runPackageManagerInstall (../../packages/nx-plugin/src/utils/testing-utils/nx-project.ts:54:27)
      at newNxProject (../../packages/nx-plugin/src/utils/testing-utils/nx-project.ts:74:3)
      at ensureNxProject (../../packages/nx-plugin/src/utils/testing-utils/nx-project.ts:86:3)
      at tests/my-plugin.spec.ts:11:20
      at ../../node_modules/tslib/tslib.js:118:75
      at Object.__awaiter (../../node_modules/tslib/tslib.js:114:16)

  ● my-plugin e2e › --directory › should create src in the specified directory

    Command failed: npm install

      at runPackageManagerInstall (../../packages/nx-plugin/src/utils/testing-utils/nx-project.ts:54:27)
      at newNxProject (../../packages/nx-plugin/src/utils/testing-utils/nx-project.ts:74:3)
      at ensureNxProject (../../packages/nx-plugin/src/utils/testing-utils/nx-project.ts:86:3)
      at tests/my-plugin.spec.ts:21:22
      at ../../node_modules/tslib/tslib.js:118:75
      at Object.__awaiter (../../node_modules/tslib/tslib.js:114:16)

  ● my-plugin e2e › --tags › should add tags to the project

    Command failed: npm install

      at runPackageManagerInstall (../../packages/nx-plugin/src/utils/testing-utils/nx-project.ts:54:27)
      at newNxProject (../../packages/nx-plugin/src/utils/testing-utils/nx-project.ts:74:3)
      at ensureNxProject (../../packages/nx-plugin/src/utils/testing-utils/nx-project.ts:86:3)
      at tests/my-plugin.spec.ts:34:22
      at ../../node_modules/tslib/tslib.js:118:75
      at Object.__awaiter (../../node_modules/tslib/tslib.js:114:16)

Test Suites: 1 failed, 1 total
Tests:       3 failed, 3 total
Snapshots:   0 total
Time:        91.523 s
Ran all test suites.

 ————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————

 >  NX   Ran target e2e for project my-plugin-e2e (2m)

    ×    1/1 failed
    √    0/1 succeeded [0 read from cache]

Second try

So npm install failed in the end-to-end tests. Fortunately, we can investigate the created tmp directory:

PS D:\Development\workspaces\git\my-org\tmp\nx-e2e\proj> npm install
npm ERR! code EINVALIDPACKAGENAME
npm ERR! Invalid package name "my-org/my-plugin": name can only contain URL-friendly characters

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\gnuechtel\AppData\Local\npm-cache\_logs\2022-05-01T11_45_57_526Z-debug.log

Now we search 'my-org/my-plugin' in repo's directory and replace it by '@my-org/my-plugin'.

The following files should be changed:

But only one test passes:

PS D:\Development\workspaces\git\my-org> npx nx e2e my-plugin-e2e

> nx run my-plugin-e2e:e2e

Compiling TypeScript files for project "my-plugin"...
Done compiling TypeScript files for project "my-plugin".
 FAIL   my-plugin-e2e  e2e/my-plugin-e2e/tests/my-plugin.spec.ts (46.762 s)
  my-plugin e2e
    √ should create my-plugin (45402 ms)
    --directory
      × should create src in the specified directory (2 ms)
    --tags
      × should add tags to the project

  ● my-plugin e2e › --directory › should create src in the specified directory

    EBUSY: resource busy or locked, rmdir 'D:\Development\workspaces\git\my-org/tmp/nx-e2e/proj'

      at rmdirSync (../../node_modules/fs-extra/lib/remove/rimraf.js:264:13)
      at rimrafSync (../../node_modules/fs-extra/lib/remove/rimraf.js:243:7)
      at cleanup (../../packages/nx-plugin/src/utils/testing-utils/utils.ts:125:13)
      at newNxProject (../../packages/nx-plugin/src/utils/testing-utils/nx-project.ts:71:10)
      at ensureNxProject (../../packages/nx-plugin/src/utils/testing-utils/nx-project.ts:86:3)
      at tests/my-plugin.spec.ts:21:22
      at ../../node_modules/tslib/tslib.js:118:75

  ● my-plugin e2e › --tags › should add tags to the project

    EBUSY: resource busy or locked, rmdir 'D:\Development\workspaces\git\my-org/tmp/nx-e2e/proj'

      at rmdirSync (../../node_modules/fs-extra/lib/remove/rimraf.js:264:13)
      at rimrafSync (../../node_modules/fs-extra/lib/remove/rimraf.js:243:7)
      at cleanup (../../packages/nx-plugin/src/utils/testing-utils/utils.ts:125:13)
      at newNxProject (../../packages/nx-plugin/src/utils/testing-utils/nx-project.ts:71:10)
      at ensureNxProject (../../packages/nx-plugin/src/utils/testing-utils/nx-project.ts:86:3)
      at tests/my-plugin.spec.ts:34:22
      at ../../node_modules/tslib/tslib.js:118:75

Test Suites: 1 failed, 1 total
Tests:       2 failed, 1 passed, 3 total
Snapshots:   0 total
Time:        47.529 s, estimated 91 s
Ran all test suites.

 ————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————

 >  NX   Ran target e2e for project my-plugin-e2e (52s)

    ×    1/1 failed
    √    0/1 succeeded [0 read from cache]

Third try (with solution)

So our second try failed, because of locked resources. The most common reason for locked resources are running processes. So our first suspect is Nx daemon, which fortunately reveals itself in task manager.

So the solution is to stop the Nx daemon.

First kill the processes:

image

Then set the env variable:

$env:NX_DAEMON="false"

Now everything works fine:

PS D:\Development\workspaces\git\my-org> npx nx e2e my-plugin-e2e

> nx run my-plugin-e2e:e2e

Compiling TypeScript files for project "my-plugin"...
Done compiling TypeScript files for project "my-plugin".
 PASS   my-plugin-e2e  e2e/my-plugin-e2e/tests/my-plugin.spec.ts (116.177 s)
  my-plugin e2e
    √ should create my-plugin (42629 ms)
    --directory
      √ should create src in the specified directory (36259 ms)
    --tags
      √ should add tags to the project (36219 ms)

Test Suites: 1 passed, 1 total
Tests:       3 passed, 3 total
Snapshots:   0 total
Time:        116.933 s
Ran all test suites.

Example repository (with fix of package names in the second commit): https://github.com/gnuechtel/nx-plugin-example

Environment

PS D:\Development\workspaces\git\my-org> npx nx report

 >  NX   Report complete - copy this into the issue template

   Node : 16.13.2
   OS   : win32 x64
   npm  : 8.1.2

   nx : 14.0.5
   @nrwl/angular : Not Found
   @nrwl/cypress : Not Found
   @nrwl/detox : Not Found
   @nrwl/devkit : 14.0.5
   @nrwl/eslint-plugin-nx : 14.0.5
   @nrwl/express : Not Found
   @nrwl/jest : 14.0.5
   @nrwl/js : 14.0.5
   @nrwl/linter : 14.0.5
   @nrwl/nest : Not Found
   @nrwl/next : Not Found
   @nrwl/node : Not Found
   @nrwl/nx-cloud : Not Found
   @nrwl/nx-plugin : 14.0.5
   @nrwl/react : Not Found
   @nrwl/react-native : Not Found
   @nrwl/schematics : Not Found
   @nrwl/storybook : Not Found
   @nrwl/web : Not Found
   @nrwl/workspace : 14.0.5
   typescript : 4.6.4
   rxjs : 6.6.7
   ---------------------------------------
   Community plugins:
AgentEnder commented 2 years ago

Interesting! The daemon errors make sense, but the lack of an @ sign for the generated plugin is intriguing. This doesn't occur if you generate a second plugin, or were to generate the first plugin in an existing workspace.

AgentEnder commented 2 years ago

Looks like the import path is set on that function, and we used to not obey it very well. Fixing that issue, caused the new error which is evidently not covered by a test. I'll look into the daemon stuff and get this pushed out.

github-actions[bot] commented 1 year ago

This issue has been closed for more than 30 days. If this issue is still occuring, please open a new issue with more recent context.