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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
3f864d9
feat: support NPM 8 peerDependencies
moroine Mar 4, 2022
f379b59
feat: include only usedExports
moroine Mar 4, 2022
2762e31
chore: update readme
moroine Mar 9, 2022
7e1b84d
chore: add tests
moroine Mar 9, 2022
db51861
chore: fix eslint
moroine Mar 10, 2022
8f70f26
fix: windows path delimiter
moroine Mar 10, 2022
ee4ecfc
fix: path sepeartor
moroine Mar 10, 2022
7418423
chore: add end-to-end testing
moroine Mar 10, 2022
1874a77
fix: e2e tests
moroine Mar 13, 2022
c5dd060
fix: tests
moroine Mar 13, 2022
0672d43
fix: setup file
moroine Mar 13, 2022
b6ac38f
fix: service names
moroine Mar 13, 2022
bd105b1
fix: tests
moroine Mar 14, 2022
1ace71a
fix: test
moroine Mar 14, 2022
41ff294
fix: skip e2e tests for sls@1 & sls@2
moroine Mar 15, 2022
91a3b7b
fix: tests
moroine Mar 15, 2022
1ca124f
fix: tests
moroine Mar 15, 2022
6f87d14
fix: add missing return
moroine Mar 15, 2022
1c84de5
chore: remove console.log
moroine Mar 15, 2022
7fbf371
chore: replace mocha by jest
moroine Mar 17, 2022
86d9f6c
chore: fix lint
moroine Mar 17, 2022
438eb7c
chore: fix skip
moroine Mar 17, 2022
708021a
chore: fix window tests
moroine Mar 17, 2022
bc13070
chore: fix window tests
moroine Mar 17, 2022
d35e19b
chore: fix window tests
moroine Mar 17, 2022
fb7dfb1
chore: fix window tests
moroine Mar 17, 2022
a1b37f3
chore: fix window tests
moroine Mar 17, 2022
9d895ea
chore: fix window tests
moroine Mar 17, 2022
442957b
chore: address j0k3r feedbacks
moroine Mar 18, 2022
16dd6ff
chore: remove indentation
moroine Mar 18, 2022
8dec6d1
chore: fix prettier
moroine Mar 18, 2022
a6e58cf
fix: merge conflicts
moroine Mar 24, 2022
fcdcd94
chore: address PR comments
moroine Mar 28, 2022
d71cd49
Merge remote-tracking branch 'serverless-heaven/master' into feature/…
moroine Mar 28, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
env:
es6: true
node: true
mocha: true
jest: true
extends:
- eslint:recommended
- plugin:lodash/recommended
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ coverage
.idea
/.nyc_output
.history
.vscode
.DS_Store
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ coverage
examples
tests
*.test.js
__mocks__
.nyc_output
.github
yarn.lock
Expand Down
21 changes: 18 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,17 @@ custom:
nodeModulesRelativeDir: '../../' # relative path to current working directory.
```

When using NPM 8, `peerDependencies` are automatically installed by default. In order to avoid adding all transitive dependencies to your `package.json`, we will use the `package-lock.json` when possible. If your project is included in a monorepo, you can specify the path to the `package-lock.json`:

```yaml
# serverless.yml
custom:
webpack:
includeModules:
nodeModulesRelativeDir: '../../' # relative path to current working directory.
packagerOptions:
lockFile: '../../package-lock.json' # relative path to package-lock.json
vicary marked this conversation as resolved.
Show resolved Hide resolved
```
#### Runtime dependencies

If a runtime dependency is detected that is found in the `devDependencies` section and
Expand Down Expand Up @@ -372,9 +383,13 @@ you should use any version `<5.5 >=5.7.1` as the versions in-between have some n

The NPM packager supports the following `packagerOptions`:

| Option | Type | Default | Description |
| ------------------ | ---- | ------- | --------------------------------------------------- |
| noInstall | bool | false | Do not run `npm install` (assume install completed) |
| Option | Type | Default | Description |
| ------------------ | ------ | --------------------- | --------------------------------------------------- |
| noInstall | bool | false | Do not run `npm install` (assume install completed) |
| lockFile | string | ./package-lock.json | Relative path to lock file to use |

When using NPM version `>= 7.0.0`, we will use the `package-lock.json` file instead of modules installed in `node_modules`. This improves the
supports of NPM `>= 8.0.0` which installs `peer-dependencies` automatically. The plugin will be able to detect the correct version.

##### Yarn

Expand Down
13 changes: 13 additions & 0 deletions __mocks__/fs-extra.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';

/**
* Mock object for fs
*/

module.exports = {
copy: jest.fn().mockResolvedValue(),
pathExists: jest.fn().mockResolvedValue(true),
pathExistsSync: jest.fn().mockReturnValue(false),
removeSync: jest.fn().mockReturnValue(),
remove: jest.fn()
};
27 changes: 27 additions & 0 deletions __mocks__/fs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';

/**
* Mock object for fs
*/

const streamMock = {
on: jest.fn()
};

const statMock = {
isDirectory: jest.fn()
};

const actual = jest.requireActual('fs');

module.exports = {
...actual,
createWriteStream: jest.fn().mockReturnValue(streamMock),
readFileSync: jest.fn(),
statSync: jest.fn().mockReturnValue(statMock),
writeFileSync: jest.fn(),
copyFileSync: jest.fn(),

_streamMock: streamMock,
_statMock: statMock
};
9 changes: 9 additions & 0 deletions __mocks__/glob.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

/**
* Mock object for glob
*/

module.exports = {
sync: jest.fn()
};
32 changes: 32 additions & 0 deletions __mocks__/webpack.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict';

const statsMock = {
compilation: {
errors: [],
compiler: {
outputPath: 'statsMock-outputPath'
},
modules: []
},
toString: jest.fn().mockReturnValue('testStats'),
hasErrors() {
return Boolean(this.compilation.errors.length);
}
};

const compilerMock = {
run: jest.fn().mockImplementation(cb => cb(null, statsMock)),
watch: jest.fn().mockImplementation(cb => cb(null, statsMock)),
hooks: {
beforeCompile: {
tapPromise: jest.fn()
}
},
plugin: jest.fn().mockImplementation((name, cb) => cb(null, {}))
};

const mock = jest.fn().mockReturnValue(compilerMock);
mock.compilerMock = compilerMock;
mock.statsMock = statsMock;

module.exports = mock;
23 changes: 23 additions & 0 deletions examples/include-external-npm-packages-lock-file/_setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'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)


const path = require('path');

module.exports = async (originalFixturePath, fixturePath, utils) => {
const pluginPath = path.resolve(originalFixturePath, '..', '..');

const SLS_CONFIG_PATH = path.join(fixturePath, 'serverless.yml');
const WEBPACK_CONFIG_PATH = path.join(fixturePath, 'webpack.config.js');
const PACKAGE_JSON_PATH = path.join(fixturePath, 'package.json');
const LOCK_PATH = path.join(fixturePath, 'package-lock.json');

await Promise.all([
utils.replaceInFile(SLS_CONFIG_PATH, '- serverless-webpack', `- ${pluginPath}`),
utils.replaceInFile(WEBPACK_CONFIG_PATH, "'serverless-webpack'", `'${pluginPath}'`),
utils.replaceInFile(PACKAGE_JSON_PATH, 'file:../..', `file:${pluginPath}`),
utils.replaceInFile(LOCK_PATH, '../..', `${pluginPath}`)
]);

const command = /^win/.test(process.platform) ? 'npm.cmd' : 'npm';

return utils.spawnProcess(command, ['install'], { cwd: __dirname });
};
17 changes: 17 additions & 0 deletions examples/include-external-npm-packages-lock-file/handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Should keep side-effects scripts
import 'dotenv/config';
// Should be included as fbgraph is not marked as sideEffect free
import { fbgraph } from 'fbgraph';
// Should keep named imports
import { toUpper } from 'lodash';
// Should keep default imports
import isEqual from 'lodash.isequal';

function getMessage() {
return isEqual(true, false) ? 'noop' : toUpper('hello fb & aws');
}

export const hello = function (event, context, cb) {
const message = getMessage();
cb(null, { message, event });
};