allenhwkim / ngentest

Angular6+ Unit Test Generator For Components, Directive, Services, and Pipes
https://ngentest.github.io
MIT License
144 stars 60 forks source link

does not work with nrwl nx #42

Closed alfredoperez closed 3 years ago

alfredoperez commented 4 years ago

I tried to generate a test in a project that uses nrwl and is not able to find modules

Error: Cannot find module '@scwx/utils-alerts'
Require stack:
- 
- /usr/local/lib/node_modules/ngentest/index.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:1080:15)
    at Function.Module._load (internal/modules/cjs/loader.js:923:27)
    at Module.require (internal/modules/cjs/loader.js:1140:19)
    at require (internal/modules/cjs/helpers.js:75:18)
    at Object.<anonymous> (<anonymous>:13:24)
    at Module._compile (internal/modules/cjs/loader.js:1251:30)
    at requireFromString (/usr/local/lib/node_modules/ngentest/node_modules/require-from-string/index.js:28:4)
    at run (/usr/local/lib/node_modules/ngentest/index.js:151:21)
    at Object.<anonymous> (/usr/local/lib/node_modules/ngentest/index.js:220:3)
    at Module._compile (internal/modules/cjs/loader.js:1251:30) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ '', '/usr/local/lib/node_modules/ngentest/index.js' ]
}
allenhwkim commented 4 years ago

Hi

This happens when the test generator, which has an isolated environment, tries to compile your code. Which means it can't find your module.

As you seen in README.md, try using replacements. This does not mean replacing your code. It just temporarily replace the code in memory.

replacements: There are some codes, which causes error without proper environment. You need to replace these codes. You can specify from value with regular expression and to value with string. e.g.

replacements: [
  { from: '^\\S+\\.define\\(.*\\);', to: ''}`
]

For example,

  // faking import for errorenous lib.
  replacements: [
    { from: '@scwx\\/utils-alerts, to: '@angular/core' }
  ]