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.

Error: Missing Mocha.js (mocha-patch / zone.js)

See original GitHub issue

Trying to run a test with mocha:

declare global {
	namespace NodeJS {
			interface Global {
				document: Document;
				window: Window;
				navigator: Navigator;
		}
	}
}
require('zone.js/dist/mocha-patch');
require('core-js/es6')
require('core-js/es7/reflect')
require('zone.js/dist/zone-node')
require('zone.js/dist/long-stack-trace-zone')
require('zone.js/dist/proxy')
require('zone.js/dist/sync-test')
require('zone.js/dist/async-test')
require('zone.js/dist/fake-async-test')
require('zone.js/dist/proxy')
const testing = require('@angular/core/testing')
const browser = require('@angular/platform-browser-dynamic/testing')
testing.TestBed.initTestEnvironment(
  browser.BrowserDynamicTestingModule,
  browser.platformBrowserDynamicTesting()
)
import { JSDOM } from 'jsdom';
const { window } = new JSDOM('<!doctype html><html><body></body></html>');
global.document = window.document;
global.window = window;

import * as assert from 'assert';
import { TestBed, async } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { AppComponent } from '../../../client/app/app.component';

describe('AppComponent', function() {

	beforeEach(async(() => {
		TestBed.configureTestingModule({
			imports: [
				RouterTestingModule
			],
			declarations: [
				AppComponent
			],
		}).compileComponents();
	}));

	it('should create the app', function() {
		// Test
		const fixture = TestBed.createComponent(AppComponent);
		const app = fixture.debugElement.componentInstance;
		// Assert
		assert.notStrictEqual(app, undefined);
	});

	it(`should have as title 'test-ang-app'`, function() {
		// Test
		const fixture = TestBed.createComponent(AppComponent);
		const app = fixture.debugElement.componentInstance;
		// Assert
		assert.strictEqual(app.title, 'test-ang-app');
	});

	it('should render title in a h1 tag', function() {
		// Test
		const fixture = TestBed.createComponent(AppComponent);
		fixture.detectChanges();
		const compiled = fixture.debugElement.nativeElement;
		// Assert
		assert((compiled.querySelector('h1').textContent).includes('Welcome to test-ang-app!'));
	});
	
});

package.json:

{
    "name": "mop",
    "version": "0.0.0",
    "scripts": {
        "lint": "npx ng lint && npx eslint . --ext js,ts",
        "style": "npx prettier --check \"**/*.ts\"",
        "style_format": "npx prettier \"**/*.ts\"",
        "devc": "npx nodemon --config ./nodemon.client.json"
    },
    "private": true,
    "dependencies": {
        "@angular/animations": "^7.2.13",
        "@angular/cdk": "^7.3.7",
        "@angular/common": "~7.2.0",
        "@angular/compiler": "~7.2.0",
        "@angular/core": "~7.2.0",
        "@angular/forms": "^7.2.15",
        "@angular/material": "^7.3.7",
        "@angular/platform-browser": "^7.2.14",
        "@angular/platform-browser-dynamic": "^7.2.14",
        "@angular/router": "^7.2.14",
        "@ngrx/effects": "^7.4.0",
        "@ngrx/entity": "^7.4.0",
        "@ngrx/router-store": "^7.4.0",
        "@ngrx/schematics": "^7.4.0",
        "@ngrx/store": "^7.4.0",
        "@ngrx/store-devtools": "^7.4.0",
        "core-js": "^2.5.4",
        "express-ws": "^4.0.0",
        "guid-typescript": "^1.0.9",
        "hammerjs": "^2.0.8",
        "keycloak-angular": "^6.1.0",
        "moment": "^2.24.0",
        "ng-connection-service": "^1.0.4",
        "ngrx-store-freeze": "^0.2.4",
        "rxjs": "~6.3.3",
        "tslib": "^1.9.0",
        "ws": "^7.1.0",
        "zone.js": "^0.10.0"
    },
    "devDependencies": {
        "@angular-devkit/build-angular": "~0.13.0",
        "@angular/cli": "~7.3.6",
        "@angular/compiler-cli": "^7.2.14",
        "@angular/language-service": "^7.2.14",
        "@istanbuljs/nyc-config-typescript": "^0.1.3",
        "@types/cypress": "^1.1.3",
        "@types/mocha": "^5.2.7",
        "@types/node": "^12.6.8",
        "@types/sinon": "^7.0.13",
        "@typescript-eslint/eslint-plugin": "^1.11.0",
        "@typescript-eslint/parser": "^1.11.0",
        "cobertura-merge": "^0.3.1",
        "codelyzer": "~4.5.0",
        "cypress": "^3.4.0",
        "eslint": "^6.0.1",
        "eslint-config-angular": "^0.5.0",
        "eslint-config-cypress": "^0.28.0",
        "eslint-config-prettier": "^6.0.0",
        "eslint-plugin-angular": "^4.0.1",
        "eslint-plugin-mocha": "^6.0.0",
        "eslint-plugin-prettier": "^3.1.0",
        "increase-memory-limit": "^1.0.7",
        "istanbul-lib-coverage": "^2.0.5",
        "jsdom": "^15.1.1",
        "junit-merge": "^2.0.0",
        "mocha": "^6.1.4",
        "mocha-junit-reporter": "^1.23.0",
        "nodemon": "^1.19.1",
        "nyc": "^14.1.1",
        "prettier": "1.18.2",
        "protractor": "~5.4.0",
        "run-script-os": "^1.0.7",
        "source-map-support": "^0.5.12",
        "start-server-and-test": "^1.9.1",
        "ts-node": "^8.3.0",
        "tslint": "~5.11.0",
        "typescript": "~3.2.2"
    }
}

tsconfig.client.json:

{
    "extends": "./tsconfig.json",
    "compilerOptions": {
        "removeComments": true,
        "module": "es2015",
        "moduleResolution": "node",
        "target": "es5",
        "lib": ["es2018", "dom"],
        "types": ["node"],
        "outDir": "./build/client/"
    },
    "include": [
        "./client/**/*.ts"
    ]
}

tsconfig.json:

{
    "compilerOptions": {
        "strict": true,
        "noImplicitAny": false,
        "strictNullChecks": false,
        "alwaysStrict": true,
        "sourceMap": true,
        "declaration": false,
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "importHelpers": true,
        "baseUrl": "./",
        "types": ["node", "dom"],
        "typeRoots": ["node_modules/@types"]
    },
    "include": []
}

Error:

> npx nyc --nycrc-path=./.nycrc.client.json mocha ./build/tests/client/**/*.spec.js --reporter spec

/home/karl/dev/MOP/node_modules/mocha/node_modules/yargs/yargs.js:1163
      else throw err
           ^

Error: Missing Mocha.js
    at /home/karl/dev/MOP/node_modules/zone.js/dist/mocha-patch.js:21:19
    at /home/karl/dev/MOP/node_modules/zone.js/dist/mocha-patch.js:141:7
    at /home/karl/dev/MOP/node_modules/zone.js/dist/mocha-patch.js:8:9
    at Object.<anonymous> (/home/karl/dev/MOP/node_modules/zone.js/dist/mocha-patch.js:9:2)
    at Module._compile (internal/modules/cjs/loader.js:776:30)
    at Module.replacementCompile (/home/karl/dev/MOP/node_modules/append-transform/index.js:58:13)
    at Module._extensions..js (internal/modules/cjs/loader.js:787:10)
    at Object.<anonymous> (/home/karl/dev/MOP/node_modules/append-transform/index.js:62:4)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Module.require (internal/modules/cjs/loader.js:690:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at Object.<anonymous> (/home/karl/dev/MOP/build/tests/client/unit/app.component.spec.js:1:3866)
    at Module._compile (internal/modules/cjs/loader.js:776:30)
    at Module.replacementCompile (/home/karl/dev/MOP/node_modules/append-transform/index.js:58:13)
    at Module._extensions..js (internal/modules/cjs/loader.js:787:10)
    at Object.<anonymous> (/home/karl/dev/MOP/node_modules/append-transform/index.js:62:4)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Module.require (internal/modules/cjs/loader.js:690:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at /home/karl/dev/MOP/node_modules/mocha/lib/mocha.js:334:36
    at Array.forEach (<anonymous>)
    at Mocha.loadFiles (/home/karl/dev/MOP/node_modules/mocha/lib/mocha.js:331:14)
    at Mocha.run (/home/karl/dev/MOP/node_modules/mocha/lib/mocha.js:811:10)
    at Object.exports.singleRun (/home/karl/dev/MOP/node_modules/mocha/lib/cli/run-helpers.js:108:16)
    at exports.runMocha (/home/karl/dev/MOP/node_modules/mocha/lib/cli/run-helpers.js:142:13)
    at Object.exports.handler.argv [as handler] (/home/karl/dev/MOP/node_modules/mocha/lib/cli/run.js:292:3)
    at Object.runCommand (/home/karl/dev/MOP/node_modules/mocha/node_modules/yargs/lib/command.js:242:26)
    at Object.parseArgs [as _parseArgs] (/home/karl/dev/MOP/node_modules/mocha/node_modules/yargs/yargs.js:1087:28)
    at Object.parse (/home/karl/dev/MOP/node_modules/mocha/node_modules/yargs/yargs.js:566:25)
    at Object.exports.main (/home/karl/dev/MOP/node_modules/mocha/lib/cli/cli.js:68:6)
    at Object.<anonymous> (/home/karl/dev/MOP/node_modules/mocha/bin/mocha:154:29)
    at Module._compile (internal/modules/cjs/loader.js:776:30)
    at Module.replacementCompile (/home/karl/dev/MOP/node_modules/append-transform/index.js:58:13)
    at Module._extensions..js (internal/modules/cjs/loader.js:787:10)
    at Object.<anonymous> (/home/karl/dev/MOP/node_modules/append-transform/index.js:62:4)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:829:12)
    at runMain (/home/karl/.node-spawn-wrap-8104-8e8e0ab41346/node:68:10)
    at Function.<anonymous> (/home/karl/.node-spawn-wrap-8104-8e8e0ab41346/node:171:5)
    at Object.<anonymous> (/home/karl/dev/MOP/node_modules/nyc/bin/wrap.js:27:4)
    at Module._compile (internal/modules/cjs/loader.js:776:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:829:12)
    at /home/karl/.node-spawn-wrap-8104-8e8e0ab41346/node:178:8
    at Object.<anonymous> (/home/karl/.node-spawn-wrap-8104-8e8e0ab41346/node:181:3)
    at Module._compile (internal/modules/cjs/loader.js:776:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:829:12)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
ghostcommented, Aug 13, 2019

Or from an architectural standpoint, it saves more time to find a yeoman angular related Repo with Jasmin, Karma/Jest just baked in.

Because the community is using Jasmin because of better mocking framework than mocha-sinon. And all the suggested solutions in GitHub and Angular official docs are in jasmin Cheers

0reactions
angular-automatic-lock-bot[bot]commented, Jun 13, 2020

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Converting newly created angular cli project to use mocha ...
The problem is that zone.js/dist/zone-testing assumes you are using ... it is importing the mocha-patch instead of the jasmine-patch .
Read more >
errors.js - Documentation - Mocha.js
Each distinct message is only displayed once. * Ignores empty messages. ... Creates an error object to be thrown when an argument is...
Read more >
resolve.fallback: { "crypto": false } angular - You.com | The search ...
Error when attempting to include Crypto module: Can't resolve 'crypto' ... that's all, this should work and the crypto-js warning should no longer...
Read more >
Automated Malware Analysis Report for f_00002a
Disables application error messsages (SetErrorMode) ... No Antivirus matches ... \Local\Temp\5estxtag.xt5\extension\node_modules\zone.js\dist\mocha-patch.js.
Read more >
Angular Testing with Webpack, Mocha, Chai and Sinon.
Angular Testing with Webpack, Mocha, Chai and Sinon. ... Error in . ... Please note that now, Zone.js, provide a mocha-patch.js, ...
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