Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: Cannot find module 'undefined/path/to/functions' #135

Open
olaven opened this issue Aug 23, 2020 · 3 comments
Open

Error: Cannot find module 'undefined/path/to/functions' #135

olaven opened this issue Aug 23, 2020 · 3 comments

Comments

@olaven
Copy link

olaven commented Aug 23, 2020

Hi 👋
I am very new to Serverless, so I may be skipping something relatively obvious.

I have set up a test-template/test as per this guide. The test function generated by the template looks like it is passing the correct values to mochaPlugin, however the tests do not run and the following is printed out in the console:

# NOTE: PATH_TO_MY_PROJECT added by me
Error: Cannot find module 'undefined/src/functions/testFunction.js'
Require stack:
- /Users/olaven/PATH_TO_MY_PROJECT/node_modules/serverless-mocha-plugin/index.js
- /Users/olaven/PATH_TO_MY_PROJECT/src/tests/testFunction.js
- /Users/olaven/PATH_TO_MY_PROJECT/node_modules/mocha/lib/esm-utils.js
- /Users/olaven/PATH_TO_MY_PROJECT/node_modules/mocha/lib/mocha.js
- /Users/olaven/PATH_TO_MY_PROJECT/node_modules/mocha/lib/cli/one-and-dones.js
- /Users/olaven/PATH_TO_MY_PROJECT/node_modules/mocha/lib/cli/options.js
- /Users/olaven/PATH_TO_MY_PROJECT/node_modules/mocha/bin/mocha
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:966:15)

Is this a known issue?

Have a great day 😄

  • olaven
@olaven
Copy link
Author

olaven commented Aug 23, 2020

it seems like process.env.SERVERLESS_TEST_ROOT is not defined when reaching this line. Manually changing this line to the full module path made the test pass. Just removing the SERVERLESS_TEST_ROOT-prefix did not. 🤔

I.e. myModule.runScripts('preTestCommands') never gets called, and the code setting the environment variable is never run. Is it supposed to? 😄

@salvadornava01
Copy link

it seems like process.env.SERVERLESS_TEST_ROOT is not defined when reaching this line. Manually changing this line to the full module path made the test pass. Just removing the SERVERLESS_TEST_ROOT-prefix did not. 🤔

I.e. myModule.runScripts('preTestCommands') never gets called, and the code setting the environment variable is never run. Is it supposed to? 😄

Thanks for your comment, I've ran on the same issue and changed the line const mod = require(process.env.SERVERLESS_TEST_ROOT + modPath); to const mod = require(process.env.PWD + modPath); in "index.js" located in /nodemodules/serverless-mocha/index.js and it worked for me!

I've tested on Windows and soon I'll test it on Mac to see if it works, and if it works properly maybe commit the change and wait for a PR to be merged 😊

@camunity
Copy link

camunity commented Mar 7, 2021

In my case I had to switch this line in my test file

let wrapped = mochaPlugin.getWrapper('hello', '/hello.js', 'hello');

to this:

let wrapped = mochaPlugin.getWrapper('hello', '/index.js', 'hello');

Where my function is named hello and it's handler is a file called index.js. I had called sls create test --function hello at first and it configured the test directory and test file fine but I kept receiving the OP's error until I changed that second directory path.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants