aurelia / testing

Simplifies the testing of UI components by providing an elegant, fluent interface for arranging test setups along with a number of runtime debug/test helpers.
MIT License
40 stars 27 forks source link

Jest aurelia-router mock breaks StageComponent #90

Closed michaelw85 closed 5 years ago

michaelw85 commented 5 years ago

I'm submitting a bug report

Please tell us about your environment:

Current behavior: Error occurs when using StageComponent in combination with a aurelia-router mock.

Cannot find module '<ROOT>\test\unit\aurelia-templating-router' from 'aurelia-loader-nodejs.js'

      at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:221:17)
      at advancedRequire (node_modules/aurelia-loader-nodejs/dist/commonjs/aurelia-loader-nodejs.js:72:28)
      at NodeJsLoader.<anonymous> (node_modules/aurelia-loader-nodejs/dist/commonjs/aurelia-loader-nodejs.js:192:46)
      at step (node_modules/aurelia-loader-nodejs/dist/commonjs/aurelia-loader-nodejs.js:37:23)
      at Object.next (node_modules/aurelia-loader-nodejs/dist/commonjs/aurelia-loader-nodejs.js:18:53)

Steps to reproduce 1) Create a new project using latest cli, choose custom and select a TS+Jest setup 2) Create folder __mocks__ in the root of the project 3) Add empty file aurelia-router.ts 4) Replace app.spec.ts with the following test:

import { ComponentTester, StageComponent } from 'aurelia-testing';
import { bootstrap } from 'aurelia-bootstrapper';

describe('this', () => { 
  it('will throw an error', async () => {
      const component: ComponentTester = StageComponent.withResources('app').inView('<app></app>');
      await component.create(bootstrap);
  });
});

5) run tests au jest

To successfully run the "test" rename the aurelia-router.ts to anything else and rerun.

Expected/desired behavior: I should be able to use StageComponent and have a aurelia-router mock in place. This mock file should not affect this test unless I specifically want to use the mock.

zewa666 commented 5 years ago

But isn't the approach with mocking a dependency via the mocks folder that they are automatically loaded. In that case it would make sense since the mock file is empty and thus leading to an error since nothing is exported

michaelw85 commented 5 years ago

@zewa666 Thanks for the info, I was under the impression mock are not automatically loaded since automock is false by default. Reading the documentation you are linking states differently for node modules. This gives me some new insight and I will have another look.

P.S I also had a complete mock with content and it also failed so I figured it was not related.

michaelw85 commented 5 years ago

@zewa666 This documentation pointed me into the right direction!

To get StageComponent (quickly) working again I added: jest.unmock('aurelia-router');

The jest docs really confused me I was not expecting automatic mocks which made me think something went wrong in the StageComponent module.

zewa666 commented 5 years ago

Yeah Jest docs are massive but somewhat hard to digest. Anyway Im glad you found the issue

michaelw85 commented 5 years ago

Thanks allot for your help this resolved multiple issues (seemingly unrelated) I was struggling with. I finished a PR to the jest docs today adding a note to the automock setting.