question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unit test for custom schematics fails with 'cannot find nx.json'

See original GitHub issue

Expected Behavior

I can run unit test for custom schematics, without getting errors that are related to the way I use the schematics testing framework.

Current Behavior

We have some custom schematics for generating apps, libs, components etc. Nothing fancy in particular. I’m in the process of writing unit tests for these schematics, but I fail to succeed on this. While the actually schematic is running fine, I cannot get the unit test to work.

Steps to Reproduce

I have a basic schematic that is adding some sugar to generating a new app. I’m chaining on ‘’@nrwl/schematics’, ‘application’'.

here is my test:

import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing';
import * as path from 'path';

const collectionPath = path.join(__dirname, '../collection.json');

describe('funnel', () => {
  let runner: SchematicTestRunner;
  let workspaceTree: UnitTestTree;

  beforeEach(() => {
    runner = new SchematicTestRunner('funnel', collectionPath);
    workspaceTree = runner.runSchematic('funnel');
  });

it('should have done something', () => {
      const contents = workspaceTree.readContent('/angular.json');

// other content of the test left out
     
    });

it already fails on getting the workspaceTree. I get the following error:

Cannot find nx.json

      at Object.readJsonInTree (../../node_modules/@nrwl/schematics/src/utils/ast-utils.js:415:15)
      at Object.getNpmScope (../../node_modules/@nrwl/schematics/src/utils/cli-config-utils.js:19:24)
      at normalizeOptions (../../node_modules/@nrwl/schematics/src/collection/application/index.js:365:44)
      at ../../node_modules/@nrwl/schematics/src/collection/application/index.js:285:23

Context

  • version of Nx used: 7.8.6
  • version of Angular CLI used: 7.1.0
  • version of Angular DevKit used7.1.0

I’m sure I’m overlooking something 😃. Thanks for any suggestions.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
evtkcommented, Aug 18, 2020

I’m sorry to say that I actually never managed to fix it, so finally decided to remove the unit tests for the schematics.

0reactions
mehrad-rafighcommented, Aug 18, 2020

Somewhat good news. I managed to get passed the cannot find nx.json Problem. But now I get Cannot read property ‘engineHost’ of null which comes from here

Read more comments on GitHub >

github_iconTop Results From Across the Web

I get this error:Cannot find module '@angular-devkit/build- ...
The error states that "Cannot find module '@angular-devkit/build-angular/package.json'" which means you have to have that package added.
Read more >
How to Create A Custom Angular CLI Schematic with Nx Tool
Learn how to create applications, libraries, and custom workspace CLI Angular schematics with enterprise Angular tool Nx.
Read more >
nrwl-nx/community - Gitter
I'm trying to run jest tests in a nx workspace via webstorm and am getting an error where scss is not being sucked...
Read more >
The ultimate migration guide to angular-eslint ...
Configure unit test runner. Nx has built-in support for the Jest and Karma testing frameworks for Angular application and library projects.
Read more >
Migrating an Angular CLI project to Nx
Updating your unit testing configuration ... Nx uses Jest by default. If you have any custom Jest configuration, you need to update the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found