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

jest and serverless both clash to use -c or --config to specify config file #11

Open
sylvainlegault opened this issue Mar 19, 2020 · 12 comments

Comments

@sylvainlegault
Copy link

Interesting plugin, but sadly I need to specify a jest config file and this seems to conflict with severless that both use the same synthax to specify the file. Would it be possible to provide an overwrite for serverless. I have a feeling that most likely not an issue with your module, but if you could look to see if something possible.

jest --config jest.config.e2e.js
FAIL src/e2e/get.test.ts
● Test suite failed to run

ServerlessError: "service" property is missing in jest.config.e2e.js

Here serverless think that jest.config.e2e.js is the serverless.yml file

@dhm116
Copy link
Contributor

dhm116 commented Mar 19, 2020

Hi @sylvainlegault - specifying a separate Jest config should be no issue whatsoever. In your jest.config.e2e.js, did you remember to specify the following testEnvironment?

"testEnvironment": "jest-environment-serverless"

One other potential issue could be that the serverless.yml needs to be located in the same directory that jest was executed within (in order to configure the Serverless working directories), so I'm not sure if that could be impacting you here or not.

If none of those appear to be related, perhaps you could share your Jest configuration (or relevant portions) to help narrow down the culprit.

@sylvainlegault
Copy link
Author

sylvainlegault commented Mar 19, 2020

thanks for the quick answer, I simplified it now to remove typescript and root and module mapping.
keep in it to the simplest expression I can (we want to separate unit test from e2e test)

module.exports = {
  testEnvironment: 'jest-environment-serverless',
  testMatch: ['**/__e2e__/**/*.js'],
}

but still the same
$ jest -c jest.config.e2e.js
FAIL e2e/scenario.test.js
● Test suite failed to run

ServerlessError: "service" property is missing in jest.config.e2e.js

I execute at the root of the project so all config files are in the current directory.

thanks in advance

@sylvainlegault
Copy link
Author

sylvainlegault commented Mar 19, 2020

i'm using jest 25.1.0 and serverless 1.65.0 ?

@dhm116
Copy link
Contributor

dhm116 commented Mar 19, 2020

@sylvainlegault - Thanks for the update. I'm trying to put together a small environment to try to replicate. In case you'd like to see if it helps out at all, I did create a Jest 25.x branch, so you're welcome to give that a shot in the meantime.

@dhm116
Copy link
Contributor

dhm116 commented Mar 19, 2020

@sylvainlegault - I was able to track down the source of the issue:

Serverless itself is naively parsing the command-line arguments along with Jest, so I just need to check to see if there's a simple way to prevent that from occurring and you should be all set!

I'll keep you posted.

@dhm116
Copy link
Contributor

dhm116 commented Mar 19, 2020

@sylvainlegault I've updated my jest-25.x branch with a simple fix that should resolve your issue. When you get a chance, give it a shot and let me know if it works.

I think if everything looks OK, I'll probably go ahead and publish this out to NPM as a v0.3.0 update.

@sylvainlegault
Copy link
Author

sylvainlegault commented Mar 19, 2020

Hi Doug, great support, thanks for the fix, it is working. 👍

Can I ask one more thing, not sure if it is supported but can we specify the stage ?

If I pass it as param, jest complains that it is not recognized
jest -c jest.config.e2e.js --stage dev
● Unrecognized CLI Parameter:

Unrecognized option "stage".

@dhm116
Copy link
Contributor

dhm116 commented Mar 19, 2020

@sylvainlegault - because both Jest and Serverless are (by default) attempting to parse the command-line arguments, the safest solution was to disable that behavior for Serverless. I've often managed our stages via an environment variable, for example:

provider:
  name: aws
  region: ${env:AWS_REGION, 'us-east-1'}
  runtime: nodejs12.x
  stage: ${env:STAGE, 'dev'}

This would allow you to control the stage easily at runtime with

STAGE=prod jest -c jest.config.e2e.js

or even pre-define them within your package.json:

"scripts": {
    "test": "./node_modules/.bin/jest",
    "test-e2e": "./node_modules/.bin/jest --config jest.config.e2e.js",
    "test-e2e-prod": "STAGE=prod ./node_modules/.bin/jest --config jest.config.e2e.js"
}

@sylvainlegault
Copy link
Author

Sound good, thanks Doug for the help

@syl20lego
Copy link

Hi Doug,

would it be possible to create a new version on npm with this fix ?

thanks
Sylvain

@dhm116
Copy link
Contributor

dhm116 commented Apr 14, 2020

@syl20lego I published this to NPM as v0.3.0 - https://www.npmjs.com/package/jest-environment-serverless

I'll have to look into publishing an update to NPM that points the main repo at my fork, since I don't believe that there is anyone left at FireEye to maintain this project.

@sylvainlegault
Copy link
Author

Perfect thanks I got it.

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