-
-
Notifications
You must be signed in to change notification settings - Fork 85
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
Problem installing spec reporter with Angular 12 and mocha #838
Comments
Cool. Thanks for posting the issue! I'll try to fix it ASAP next days. In the worst case the next weekend. |
Btw, do you have an example/ article how to configure mocha for angular? |
Not sure about any articles, but I can give you what we have! We actually use @angular-builders/custom-webpack to provide a custom karma config to Here is, I believe, the bare essentials needed in the const karmaMochaPlugin = require('karma-mocha');
const karmaMochaReporterPlugin = require('karma-mocha-reporter');
const karmaChromeLauncherPlugin = require('karma-chrome-launcher');
const karmaAngularDevkitPlugin = require('@angular-devkit/build-angular/plugins/karma');
module.exports = (config) => {
config.set({
frameworks: ['mocha', '@angular-devkit/build-angular'],
browsers: ['Chrome'],
plugins: [
karmaMochaPlugin,
karmaMochaReporterPlugin, // I'm not sure if the reporter plugin is actually necessary
karmaChromeLauncherPlugin,
karmaAngularDevkitPlugin,
],
reporters: ['mocha'],
});
}; And then we provide it to {
"projects": {
"project": {
"architect": {
"test": {
"builder": "@angular-builders/custom-webpack:karma",
"karmaConfig": "./karma.conf.js"
}
}
}
}
}
} |
Took a crack at this. Made the following changes to const installMochaReporter = () => {
try {
mocha.setup({
rootHooks: {
afterAll: stackPop,
beforeAll: () => stackPush('root'),
afterEach: stackPop,
beforeEach: () => stackPush('test'),
},
});
return true;
} catch (err) {
return false;
}
};
const install = () => {
if (!ngMocksUniverse.global.has('reporter-stack-install')) {
let installed = false;
installed = installJasmineReporter() || /* istanbul ignore next */ installed;
installed = installJestCircus() || /* istanbul ignore next */ installed;
installed = installMochaReporter() || /* istanbul ignore next */ installed;
// istanbul ignore if
if (!installed) {
messageCoreChecker();
}
ngMocksUniverse.global.set('reporter-stack-install', true);
}
return ngMocksUniverse.global.has('reporter-stack-install');
}; All of our tests run, and pass! It doesn't look like mocha exposes suite-level reporting, so not sure if it'll be a deal-breaker to not have that granulatiy. I'm happy to make a PR with those changes - that said, I'm not super confident it's correct. I tried our test suite without adding any "reporter stack" at all... I created a file with the following and imported it at the beginning of the test bundle file (before any other imports of ng-mocks): import ngMocksUniverse from 'ng-mocks/cjs/lib/common/ng-mocks-universe';
ngMocksUniverse.global.set('reporter-stack-install', true); All of our tests also run and pass, despite many calls to |
Hi, thank you for the update. The changes look good to me, feel free to create a PR, a contribution is very welcome. Regarding the report stack and its usage, I also agree with you. I was thinking about the issue last days, and found out that it is a stupid limitation for people who don't use it, or reset everything properly in Because of that, I have an idea to add a way to shift the stack manually. So people, who use unsupported test runners could still rely on this behavior. And the error would be throws only if they forgot to reset the things, so it wouldn't be a blocker anymore. // adds a stack level
beforeAll(() => ngMocks.rememberStack());
// adds a stack level
beforeEach(() => ngMocks.rememberStack());
// ...
// resets beforeEach level
afterEach(() => ngMocks.clearStack());
// resets beforeAll level
afterAll(() => ngMocks.clearStack()); What do you think, would it work for you? |
The things I would like to ask upfront:
|
As I was poking around, I had the same thought - it seemed like this would've been fairly easy for me to have integrated myself if the methods in Perhaps you could take the approach that sinon does, where you simply have to call |
Add mocha runner support to the internal stack. Closes help-me-mom#838
Hi @dpraul, might you provide an example of unit test you have? |
v12.4.0 has been released and contains a fix for the issue. Feel free to reopen the issue or to submit a new one if you meet any problems. |
Sure thing! It's fairly simple after the karma configuration above, but the biggest gotcha is that mocha doesn't provide any assertion library, so that needs to be added separately. We use chai. Here's an example with chai: import { expect } from 'chai';
describe('suite', () => {
it('should be true', () => {
expect(true).to.be.true;
});
}); That said, if you don't want to add an assertion library just for CI you can also write tests using describe('suite', () => {
it('should be true', (done) => {
if (true) {
done();
} else {
done('test failed!');
}
});
}); Hope that helps! |
Aha, thanks. I'll give it a try. I think in my case it was failing with an error about missing webpack... |
Hi there! I am in the process of moving our application from Angular 11 to Angular 12. Since updating, we now get the following error from ng-mocks when I try to run our unit-testing suite:
Looking through ng-mocks-stack, I suspect this is because we use mocha as our test reporter, whereas ng-mocks is trying to install reporters for either Jasmine or Jest.
We are running ng-mocks 12.3.1. Here's the output from ng version:
The text was updated successfully, but these errors were encountered: