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

Supports NPM 8 and convert tests from Mocha to Jest #1084

Merged
merged 34 commits into from Mar 29, 2022

Conversation

moroine
Copy link
Contributor

@moroine moroine commented Mar 4, 2022

What did you implement:

Closes #1083 #805 #374

How did you implement it:

  • NPM 8 automatically installs peerDependencies causing a lot of WARNINGS: Could not determine version of module XXXX. In this case, we can use directly package-lock.json which has the same shape as the npm ls result.
  • Reduce the number of external modules included by using webpack.optimization.usedExports

How can we verify it:

Todos:

  • Write tests
  • Write documentation
  • Fix linting errors
  • Make sure code coverage hasn't dropped
  • Provide verification config / commands / resources
  • Enable "Allow edits from maintainers" for this PR
  • Update the messages below

Is this ready for review?: YES
Is it a breaking change?: NO

@moroine moroine changed the title Feature/npm 8 Supports NPM 8 Mar 4, 2022
Copy link
Member

@j0k3r j0k3r left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a specific test scenario where NPM 8 is used (or when NPM < 8 is used) like we did for Serverless?

README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
@moroine moroine requested a review from j0k3r March 9, 2022 22:08
@moroine
Copy link
Contributor Author

moroine commented Mar 9, 2022

Could you add a specific test scenario where NPM 8 is used (or when NPM < 8 is used) like we did for Serverless?

@j0k3r I've added tests, unfortunately, I'm not very used to the test framework used (I feel more comfortable with Jest). Also I think it would be nice to have end-to-end testing test cases.

@moroine moroine marked this pull request as ready for review March 9, 2022 22:13
@vicary
Copy link
Member

vicary commented Mar 10, 2022

@moroine For e2e tests you may take a look at the testing tool runServerless used by core. I have written one test at serverless/serverless#10177, it's not as sophisticated as a complete framework like jest, but it's doing a good job emulating the lifecycle events.

@moroine
Copy link
Contributor Author

moroine commented Mar 10, 2022

@vicary in order to install @serverless/test I need to upgrade to mocha@9 but mocha@9 drop supports of NodeJs 10.

As NodeJs 10 is deprecated, do we want to drop the support? I can just create some test projects within tests and try using serverless package, but the tests would be way slower 🤔

@j0k3r
Copy link
Member

j0k3r commented Mar 10, 2022

I tried to add some integration test but didn't succeed as of now: #1019

I used the previous release of serverless/test which sur mocha@8

@moroine
Copy link
Contributor Author

moroine commented Mar 10, 2022

@j0k3r @vicary I managed to add end-to-end tests. I think we can use example folder as fixtures files 🤔

We can see the difference when using the package-lock.json. We could in future iteration try to use lock version when possible to prevent unexpected side-effects

@@ -0,0 +1,21 @@
'use strict';
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In order to be able to run the fixture in tests, we need to tweak some files (especially because they are copied over a temporary directory)

Comment on lines +30 to +47
async function runServerless(options) {
const runServerlessOptions = {
command: 'package'
};

const servicePath = await setupFixture(options.fixture);

runServerlessOptions.cwd = servicePath;
const SERVERLESS_DIR = path.join(servicePath, 'node_modules', 'serverless');
try {
const result = await originalRunServerless(SERVERLESS_DIR, runServerlessOptions);
result.servicePath = servicePath;
return result;
} catch (error) {
error.servicePath = servicePath;
throw error;
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is very similar to @serverless/test but the issue with the original one is they:

  • run npm install
  • execute _setup

This cause 2 issues:

  • Cannot fix relative paths
  • Cannot use yarn

@moroine
Copy link
Contributor Author

moroine commented Mar 13, 2022

@j0k3r @vicary I've fixed (I think) the end-to-end tests. @serverless/test doesn't fullfill our use case. Might be good to PR them but I don't have time to do it as of now.

@moroine
Copy link
Contributor Author

moroine commented Mar 17, 2022

@j0k3r @vicary after a lot of tries & failures I managed to make the tests finally work but at a cost of huge changes...

This PR adds end-to-end tests, but I had a lot of issues due to mocking modules in tests (fs, glob, ...) but for e2e we won't want to mock. It was working when running alone but not with the entire suite test. To fix this I had to remove mocha and use jest which has better module mock support in isolation...

Not sure if you're ok with this PR changing so much at once 🤔

@vicary
Copy link
Member

vicary commented Mar 18, 2022

@moroine Huge thanks for the effort!

I personally don't mind huge changes, it just takes time to review, largely making sure existing tests are correctly ported. I'll take some time in the weekend for this.

Note: We want to make sure coverage is not dropped, we temporarily disabled the drop protection for the v3 PR but for most cases it is still enforced.

Copy link
Member

@j0k3r j0k3r left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good job, really!

I'm not against switch from Mocha to Jest, I use jest on a daily basis.

__mocks__ folder should be added to .npmignore

jest.config.js Show resolved Hide resolved
jest.config.js Outdated Show resolved Hide resolved
lib/packExternalModules.js Outdated Show resolved Hide resolved
lib/runPluginSupport.js Outdated Show resolved Hide resolved
tests/e2e/e2e.test.js Outdated Show resolved Hide resolved
@moroine moroine requested a review from j0k3r March 18, 2022 11:03
package.json Show resolved Hide resolved
@moroine moroine requested a review from j0k3r March 21, 2022 08:13
@j0k3r j0k3r requested a review from vicary March 21, 2022 19:37
@j0k3r j0k3r added this to the 5.7.0 milestone Mar 24, 2022
@j0k3r
Copy link
Member

j0k3r commented Mar 24, 2022

We merged some PRs today.
Can you rebase against the master to fix conflicts?

@moroine
Copy link
Contributor Author

moroine commented Mar 24, 2022

@j0k3r I've rebase the changes

@j0k3r j0k3r closed this Mar 24, 2022
@j0k3r j0k3r reopened this Mar 24, 2022
@j0k3r
Copy link
Member

j0k3r commented Mar 24, 2022

Closed/reopen to re-trigger GitHub Actions

Copy link
Member

@vicary vicary left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I've got a busy weekend, just a few tweaks and we are good to go.

README.md Show resolved Hide resolved
tests/packagers/index.test.js Outdated Show resolved Hide resolved
@moroine moroine requested a review from vicary March 28, 2022 19:18
@j0k3r j0k3r linked an issue Mar 29, 2022 that may be closed by this pull request
@j0k3r
Copy link
Member

j0k3r commented Mar 29, 2022

Why did you link the issue #374?

@j0k3r j0k3r changed the title Supports NPM 8 Supports NPM 8 and convert tests from Mocha to Jest Mar 29, 2022
@moroine
Copy link
Contributor Author

moroine commented Mar 29, 2022

Why did you link the issue #374?

Because with isUsedExports we only includes externals present in the final build. Tree-shaking will removed unused dependencies.

@moroine
Copy link
Contributor Author

moroine commented Mar 29, 2022

Are we good to merge?

@j0k3r j0k3r linked an issue Mar 29, 2022 that may be closed by this pull request
@j0k3r j0k3r merged commit 89c629e into serverless-heaven:master Mar 29, 2022
@moroine moroine deleted the feature/npm-8 branch March 29, 2022 08:53
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

Successfully merging this pull request may close these issues.

Support NPM 8 Serverless: WARNING: Could not determine version of module xxxxx Optimize Package
4 participants