Testing: Cannot find module

I’m getting this error:

Error: Cannot find module ‘…\test\unit\childCustomElement\childCustomElement’ from ‘aurelia-loader-nodejs.js’.

parentComponent.html

<template>
  <require from="./childCustomElement/childCustomElement"></require>
  <child-custom-element if.bind="somethingTrue"></child-custom-element >
</template>

parentComponent.spec.ts

describe(‘parentComponent’, () => {
let component;
beforeEach(() => {
component = StageComponent
.withResources(
PLATFORM.moduleName(‘…/…/src/views/parentComponent/parentComponent’))
.inView(‘<parent-component></parent-component>’);
});

It normaly finds and renders “ChildCustomElement” when I run application, but for some reason my test fails. Why is trying to find “ChildCustomElement” at “…test/unit/” path?

1 Like

Is your triple dots a typo (copy-paste)? I suppose they are double dots.

Our jest setup has this in your package.json jest config:

"modulePaths": [
      "<rootDir>/src",
      "<rootDir>/node_modules"
    ],

That means you can use absolute module path like

PLATFORM.moduleName('views/parentComponent/parentComponent')

Jest will search the module at your local src/ folder, you don’t need to use long relative path (our aurelia-loader-nodejs probably is confused about it, but I didn’t double check).

2 Likes

Thank you. I changed path in HTML file to absolute. Works perfect now.

2 Likes