hirezio / given

A monorepo for jasmine / jest addons that help you clean up your microtests by breaking them into a Given / When / Then structure.
MIT License
21 stars 4 forks source link

Error Message Returned for Given is Not Helpful - ng13 #6

Closed jamesstacyjones closed 2 years ago

jamesstacyjones commented 2 years ago

Handling of Error message ... here is my package.json

package.json.txt

Basically in the first describe with given like so:

describe(`DashboardComponent`, () => {
    let dashboardComponent: DashboardComponent;
    let ownerService: Spy<OwnerService>

    Given(() => {
        TestBed.configureTestingModule({
            providers: [
                DashboardComponent,
                provideAutoSpy(OwnerService),
            ],
        });
        dashboardComponent = TestBed.inject(DashboardComponent);
    });

Then in the constructor of the component is this

this.ownerService.getOwnerIdToOwnerMap().pipe(
                tap(idMap => {
                    this.validOwnerIds = new Set<number>(idMap.keys());
                    this.clearInvalidOwnerSelections();
                }),
                takeUntil(this.destroyed$)).subscribe();

Then the error message looks like this for Given

 Error: An error was thrown in Given(): 
        DashboardComponent@main.js:55709:25
        DashboardComponent_Factory@ng:///DashboardComponent/ɵfac.js:5:10
        hydrate@vendor.js:529570:29
        ... rest of stacktrace      

If you change it to beforeEach then you get this error:

TypeError: this.ownerService.getOwnerIdToOwnerMap() is undefined in main.js line 318 > srcScript (line 55709)
        <Jasmine>
        DashboardComponent_Factory@ng:///DashboardComponent/ɵfac.js:5:10
        <Jasmine>
        throwErrorWithContext@node_modules/@hirez_io/jasmine-given/dist/jasmine-given.js:318:9
        ... rest of stacktrace

So I tracked it down in the jasmine-given.js ... and this is the error message give by the given: Screen Shot 2022-02-21 at 9 15 52 AM

** here is the code for given function throwErrorWithContext

function throwErrorWithContext(originFunctionName, error) {
    var errorMessage = '';

    if (typeof error === 'string') {
      errorMessage = error + "\n" + NO_STACK_ERROR;
    } else if (error.stack) {
      errorMessage = error.stack;
    } else {
      errorMessage = error.toString();
    }

    throw new Error(CONTEXT_FOR_GWT_ERROR + " " + originFunctionName + "(): \n  " + errorMessage);
  }

My thinking is that you could probably check for error.message and modify accordingly but of course since I am not sure why the promise is resolving with a different error message that would be my first thought. I know that is not the greatest context but hopefully it will lead you in the right direction.

WynieCronje commented 2 years ago

When I look at the source code (snippet below). @shairez I think we can use error.toString() combined with ${CONTEXT_FOR_GWT_ERROR} ${originFunctionName}():

Therefore:

throw new Error(`${CONTEXT_FOR_GWT_ERROR} ${originFunctionName}(): \n${error}`);

What error.toString() looks like inside

image

WynieCronje commented 2 years ago

After trying to implement. I made some changes deviating from first proposal. Now we should see original stack trace and error message. (this will help alot with debugging where the issue actually is) image

shairez commented 2 years ago

Thanks for the report @jamesstacyjones and thanks for figuring how to solve it @WynieCronje

appreciate it!

I'll check out the PR now, thanks