Skip to content

Commit

Permalink
Supports NPM 8 and convert tests from Mocha to Jest (#1084)
Browse files Browse the repository at this point in the history
  • Loading branch information
moroine committed Mar 29, 2022
1 parent ce7ebc2 commit 89c629e
Show file tree
Hide file tree
Showing 48 changed files with 41,088 additions and 13,087 deletions.
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
```
#### 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';

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 });
};

0 comments on commit 89c629e

Please sign in to comment.