nrwl / nx

Smart Monorepos · Fast CI
https://nx.dev
MIT License
23.27k stars 2.32k forks source link

Cannot find module in e2e-specs when importing local library #263

Closed louwie17 closed 6 years ago

louwie17 commented 6 years ago

Hi, I have a library that contains some overlapping e2e logic used across multiple apps, when I try to import the library into the e2e spec, I get this error when running the e2e tests:

E/launcher - Error: Error: Cannot find module '@content-platform/test-helpers'
    at Function.Module._resolveFilename (module.js:538:15)
    at Function.Module._load (module.js:468:25)
    at Module.require (module.js:587:17)
    at require (internal/module.js:11:18)

Cli info:

{
      "name": "e2e-test-helpers",
      "root": "libs/e2e-test-helpers/src",
      "test": "../../../test.js",
      "appRoot": ""
}

E2E tsconfig:

{
  "extends": "../../../tsconfig.json",
  "compilerOptions": {
    "outDir": "../../../dist/out-tsc/e2e/home",
    "module": "commonjs",
    "target": "es5",
    "types": [
      "jasmine",
      "jasminewd2",
      "node"
    ]
  },
  "include": [
    "../**/*.ts"
  ],
  "exclude": [
    "**/*.spec.ts"
  ]
}

Main Tsconfig:

{
  "compileOnSave": false,
  "compilerOptions": {
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types",
      "types"
    ],
    "lib": [
      "es2017",
      "dom"
    ],
    "baseUrl": ".",
    "paths": {
      "@content-platform/*": [
        "libs/*"
      ],
      "@apps/*": [
        "apps/*"
      ]
    }
  },
  "exclude": [
    "node_modules",
    "tmp"
  ]
}
listepo-alterpost commented 6 years ago

@vsavkin I would like to know how to properly organize the structure of common e2e tests(for .po.ts and .e2e-spec.ts both together and separately)

vsavkin commented 6 years ago

The errors says "E/launcher - Error: Error: Cannot find module '@content-platform/test-helpers'", but the library name is "e2e-test-helpers". Should not you import:'@content-platform/e2e-test-helpers'?

louwie17 commented 6 years ago

@vsavkin my bad we also have a different library called (unit) test-helpers, so I was testing it with that as well seeing if it was a naming thing, this is the actual (same) error:

[08:33:27] E/launcher - Error: Error: Cannot find module '@content-platform/e2e-test-helpers'
    at Function.Module._resolveFilename (module.js:538:15)
    at Function.Module._load (module.js:468:25)
    at Module.require (module.js:587:17)
    at require (internal/module.js:11:18)

To make it work currently I directly referenced the library like so:

// tslint:disable-next-line:nx-enforce-module-boundaries
import { helpers } from '../../../../libs/e2e-test-helpers';
ThomasBurleson commented 6 years ago

Closing as will not fix.

listepo-alterpost commented 6 years ago

:( relative path very ugly

skydever commented 6 years ago

... very old but I came across this too, and fixed this just recently in a workspace where I re-use things from an e2e helper lib. the thing is, the protractor e2e tests are executed using ts-node. ts-node does not recoginze the path mappings, that is why you need to use relative paths - but there is a node package called tsconfig-paths that can add this feature to ts-node executions. see #294 of ts-node ... if you need details of how to set this up with nx just let me know, I can share my setup using tsconfig-paths with protractor ...

maeri commented 5 years ago

@skydever actually today I came same problem - could you share you config for tsconfig-paths with protractor ?

maeri commented 5 years ago

@skydever I found it , thanks :)

jhua4 commented 5 years ago

@skydever can you share your config please

skydever commented 5 years ago

@jhua4 I did it like this:

I did a require for the main tsConfig to get the list of paths for the register method, at the top of protractor.conf.js:

const tsConfig = require('../../tsconfig.json');

I think that did the trick. Just ping me if you have any problems (I switched to Cypress, so I am not sure if this is up to date, but I guess so...).

jbarrus commented 5 years ago

@skydever Can you post how you got this to work with Cypress?

skydever commented 5 years ago

hi @jbarrus

I do not use paths with cypress at the moment 🤔 maybe this helps for further analysis (not sure when I have the time). My setup looks like this (#883 seems related, wanted to link it):

{
  "extends": "../../tsconfig.json",
  "compilerOptions": {
    "types": ["cypress", "node"]
  },
  "include": ["**/*.ts"]
}
{
  "fileServerFolder": "../../dist/out-tsc/apps/my-app-e2e",
  "fixturesFolder": "../../dist/out-tsc/apps/my-app-e2e/src/fixtures",
  "integrationFolder": "../../dist/out-tsc/apps/my-app-e2e/src/integration",
  "pluginsFile": "../../dist/out-tsc/apps/my-app-e2e/src/plugins/index.js",
  "supportFile": false,
  "video": false,
  "videosFolder": "../../dist/out-tsc/apps/my-app-e2e/videos",
  "screenshotsFolder": "../../dist/out-tsc/apps/my-app-e2e/screenshots"
}
import {
  something,
  anotherThing
} from '../../../../libs/e2e-cypress-helper/src';

... and this is working at my side.

Maybe it helps to be able to use path mappings, would be awesome!

does this work for you too (without paths) or do you have another solution?

skydever commented 5 years ago

related to #1238

github-actions[bot] commented 1 year ago

This issue has been closed for more than 30 days. If this issue is still occuring, please open a new issue with more recent context.