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.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Jest aurelia-router mock breaks StageComponent

michaelw85 opened this issue · comments

I'm submitting a bug report

  • Library Version:
    "aurelia-testing": "^1.0.0"

Please tell us about your environment:

  • Operating System:
    Windows [10]

  • Node Version:
    v10.15.1

  • NPM Version:
    6.4.1

  • Webpack
    "webpack": "^4.27.0",

  • Browser:
    N/A

  • Language:
    TypeScript 3.3.1

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);
  });
});
  1. 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.

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

@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.

@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.

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

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.