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

Node app generator unitTestRunner=none option throws error #9763

Closed
Ionaru opened this issue Apr 9, 2022 · 5 comments · Fixed by #9765
Closed

Node app generator unitTestRunner=none option throws error #9763

Ionaru opened this issue Apr 9, 2022 · 5 comments · Fixed by #9765

Comments

@Ionaru
Copy link

Ionaru commented Apr 9, 2022

Current Behavior

Using --unitTestRunner=none in nx generate @nrwl/node:application throws the error: Cannot convert undefined or null to object.
Full error below.

This makes it impossible to generate an app without unittest runner ( which I suppose is a good thing, but still... 😉 ).

Expected Behavior

An app should be created without unittest runner and without error.

Steps to Reproduce

Run the following:

npx create-nx-workspace test-test-runner --preset=core --nxCloud=false
cd test-test-runner
npm install -D @nrwl/node
npx nx generate @nrwl/node:application my-app --unitTestRunner=none

Failure Logs

With npx nx generate @nrwl/node:application my-app --unitTestRunner=none

Cannot convert undefined or null to object
TypeError: Cannot convert undefined or null to object
    at C:\Users\Jeroen\Projects\test-test-runner\node_modules\@nrwl\node\src\generators\init\init.js:11:21
    at updateJson (C:\Users\Jeroen\Projects\test-test-runner\node_modules\@nrwl\devkit\src\utils\json.js:45:26)
    at updateDependencies (C:\Users\Jeroen\Projects\test-test-runner\node_modules\@nrwl\node\src\generators\init\init.js:10:29)
    at C:\Users\Jeroen\Projects\test-test-runner\node_modules\@nrwl\node\src\generators\init\init.js:30:35
    at Generator.next (<anonymous>)
    at C:\Users\Jeroen\Projects\test-test-runner\node_modules\tslib\tslib.js:117:75
    at new Promise (<anonymous>)
    at Object.__awaiter (C:\Users\Jeroen\Projects\test-test-runner\node_modules\tslib\tslib.js:113:16)
    at initGenerator (C:\Users\Jeroen\Projects\test-test-runner\node_modules\@nrwl\node\src\generators\init\init.js:23:20)
    at C:\Users\Jeroen\Projects\test-test-runner\node_modules\@nrwl\node\src\generators\application\application.js:124:57

With npx nx generate @nrwl/web:application my-app --style=css --e2eTestRunner=none --unitTestRunner=none

Cannot convert undefined or null to object
TypeError: Cannot convert undefined or null to object
    at C:\Users\Jeroen\Projects\test-test-runner\node_modules\@nrwl\web\src\generators\init\init.js:13:21
    at updateJson (C:\Users\Jeroen\Projects\test-test-runner\node_modules\@nrwl\devkit\src\utils\json.js:45:26)
    at updateDependencies (C:\Users\Jeroen\Projects\test-test-runner\node_modules\@nrwl\web\src\generators\init\init.js:12:29)
    at C:\Users\Jeroen\Projects\test-test-runner\node_modules\@nrwl\web\src\generators\init\init.js:44:29
    at Generator.next (<anonymous>)
    at C:\Users\Jeroen\Projects\test-test-runner\node_modules\tslib\tslib.js:117:75
    at new Promise (<anonymous>)
    at Object.__awaiter (C:\Users\Jeroen\Projects\test-test-runner\node_modules\tslib\tslib.js:113:16)
    at webInitGenerator (C:\Users\Jeroen\Projects\test-test-runner\node_modules\@nrwl\web\src\generators\init\init.js:33:20)
    at C:\Users\Jeroen\Projects\test-test-runner\node_modules\@nrwl\web\src\generators\application\application.js:107:59

Environment

   Node : 16.14.0
   OS   : win32 x64
   npm  : 8.6.0

   nx : 13.10.1
   @nrwl/angular : Not Found
   @nrwl/cypress : Not Found
   @nrwl/detox : Not Found
   @nrwl/devkit : 13.10.1
   @nrwl/eslint-plugin-nx : Not Found
   @nrwl/express : Not Found
   @nrwl/jest : 13.10.1
   @nrwl/js : 13.10.1
   @nrwl/linter : 13.10.1
   @nrwl/nest : Not Found
   @nrwl/next : Not Found
   @nrwl/node : 13.10.1
   @nrwl/nx-cloud : Not Found
   @nrwl/nx-plugin : Not Found
   @nrwl/react : Not Found
   @nrwl/react-native : Not Found
   @nrwl/schematics : Not Found
   @nrwl/storybook : Not Found
   @nrwl/web : Not Found
   @nrwl/workspace : 13.10.1
   typescript : 4.6.3
   rxjs : 6.6.7
@Ionaru
Copy link
Author

Ionaru commented Apr 9, 2022

The same happens using @nrwl/web:application, but only when both unitTestRunner and e2eTestRunner are none. Separately they cause no issues.

@gioragutt
Copy link
Contributor

After doing some research, this is what I found:

function updateDependencies(tree: Tree) {
  updateJson(tree, 'package.json', (json) => {
    delete json.dependencies['@nrwl/node'];
    return json;
  });
  ...
}

This is a function declared in packages/node/src/generators/init/init.ts. A similar one is declared in packages/web/src/generators/init/init.ts with @nrwl/web instead of @nrwl/node.

let jestInstall: GeneratorCallback;
if (options.unitTestRunner === 'jest') {
  jestInstall = await jestInitGenerator(tree, {});
}
const installTask = await updateDependencies(tree);
if (!schema.unitTestRunner || schema.unitTestRunner === 'jest') {
  const jestTask = jestInitGenerator(tree, {});
  tasks.push(jestTask);
}
if (!schema.e2eTestRunner || schema.e2eTestRunner === 'cypress') {
  const cypressTask = cypressInitGenerator(tree, {});
  tasks.push(cypressTask);
}
const installTask = updateDependencies(tree);

These are snippets from the same files I specified above, respectively.
Looking at cypressInitGenerator and jestInitGenerator, they both initialize package.json["dependencies"] to have a value.

So, what happens here in practice, is that you probably do not have a "dependencies" key in your package.json, and the delete json.dependencies['@nrwl/node']; statement fails.

I did a quick check with node's repl, and got the same error message.

▶ node
Welcome to Node.js v16.14.0.
Type ".help" for more information.
> let json = {}
undefined
> delete json.dependencies.hello
Uncaught TypeError: Cannot convert undefined or null to object

Can you validate this?

@gioragutt
Copy link
Contributor

gioragutt commented Apr 9, 2022

In any case, I've validated it with unit tests, I'm now opening a PR to fix this issue 👍🏻
Good catch on the edge case in @nrwl/web, it made it very easy to compare the cases and find the case 💪🏻

@Ionaru
Copy link
Author

Ionaru commented Apr 9, 2022

Can you validate this?

Your findings seem correct, I did not have a dependencies field in my package.json, only devDependencies as it was a new project. After I install something as a regular dependency, both commands function normally.

Thank you so much for the quick response and fix!

@github-actions
Copy link

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 22, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
2 participants