How to make ng test to fail on warnings
Asked Answered
I

5

13

I've just finished fixing several warnings on my unit/component tests of my Angular app, that run using ng test (with Karma/Jasmine).

This process can be quite time-consuming as it is not always obvious to spot which test case(s) is actually causing the warning(s).

So now that I don't have any more warnings, I'm wondering if there is a way to make ng test to automatically fail whenever there are any warnings.

Indoor answered 6/3, 2020 at 15:22 Comment(1)
Did you find a solution?Featly
M
5

Maybe something like this in test.ts or someplace where the tests get initialized.

console.warn = (message) => throw new Error(message);

I don't know if I would recommend this because warnings are just that warnings, but errors are errors and should be dealt with immediately. Where you would place this for just the tests can be tricky too.

Mella answered 6/3, 2020 at 15:31 Comment(0)
C
2

Define an extra file in your workspace root - called karma.globals.js for example which looks like:

// add globals for karma test runs
console.warn = (message) => { throw new Error(message); };

Now include it in your root karma.conf.js like:

const { join } = require('path');

module.exports = () => {
  return {
    
    // ...

    files: [
      join(__dirname, 'karma.globals.js')
    ]

    // ...
  };
};
Connection answered 6/11, 2020 at 8:59 Comment(0)
S
1

A simple and easy fix is a beforeAll block in your test.ts file. It replaces all console methods with an error function.

That way the tests will fail if you call console.log and you will know which ones.

If you still wanna use console.log within a unit being tested, then you could spy on it. But that's also bad due to shared global state.

Here's an example:

const errorFn = () => { throw new Error('No console logs!'); };
beforeAll(() => Object.keys(console).forEach((fn: string) => (console as any)[fn] = errorFn));

Safir answered 6/3, 2020 at 15:34 Comment(0)
S
0

In test.ts I monkey patched it like so to print also the stack trace in case of an error:

console.error = function (message?: any, ...optionalParams: any[]): void {
  const messages = [message, ...optionalParams];
  const output = messages?.map(x => x.hasOwnProperty('stack') ? x.stack : x);
  fail(`Test contained console error:\n\n${output.join('\n')}`);
};

console.warn = function (message?: any, ...optionalParams: any[]): void {
  console.error(message, ...optionalParams);
};
Sigridsigsmond answered 15/9, 2021 at 11:38 Comment(0)
R
0

Where in the file? just at the bottom after karma.start()?

Racial answered 15/6, 2023 at 9:31 Comment(2)
this should be a comment and not an answer to the questionIndoor
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Drops

© 2022 - 2024 — McMap. All rights reserved.