Skip to content

Commit

Permalink
Merge pull request #35 from pianomansam/enabled-flag
Browse files Browse the repository at this point in the history
Enabled flag
  • Loading branch information
pianomansam authored Aug 22, 2022
2 parents 5b06787 + aaf6715 commit e07ee7f
Show file tree
Hide file tree
Showing 12 changed files with 4,746 additions and 5,224 deletions.
29 changes: 19 additions & 10 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
module.exports = {
parser: 'babel-eslint',
parser: '@babel/eslint-parser',
extends: ['eslint-config-airbnb-base', 'plugin:prettier/recommended'],
parserOptions: {
requireConfigFile: false,
},
env: {
jest: true,
},
Expand All @@ -10,13 +13,19 @@ module.exports = {
'no-await-in-loop': 0,
'no-return-assign': 0,
'class-methods-use-this': 0,
"import/no-extraneous-dependencies": ["error", {
"devDependencies": true
}],
"prettier/prettier": ["error", {
"singleQuote": true,
"bracketSpacing": true,
"trailingComma": "all"
}]
'import/no-extraneous-dependencies': [
'error',
{
devDependencies: true,
},
],
'prettier/prettier': [
'error',
{
singleQuote: true,
bracketSpacing: true,
trailingComma: 'all',
},
],
},
};
};
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,24 @@ An existing serverless framework project.

## Installation
```
npm install serverless-offline-http-mock
npm install serverless-offline-http-mock
```
OR
```
yarn add serverless-offline-http-mock
```

## Upgrade to v1.0.0
Please note that if you installed a version of this prior to v1.0.0, you will need to follow step #2 below and add a truthy `serverless-offline-http-mock-enabled` value in order for your mocks to be loaded.

## Usage
1. Within the `serverless.yml` file, enable the plugin by placing an `serverless-offline-http-mock` entry in the plugins section.
1. Within the `serverless.yml` file, enable the plugin by placing an `serverless-offline-http-mock` entry in the plugins section.
**If using Serverless Offline, make sure it is placed above the `serverless-offline` plugin**

2. Create a `serverless-offline-http-mock` entry in the `custom` section.
3. For each host, create an entry containing hostname, a list of JS files to load, and an optional directory. See `serverless.yml` example below.
4. In each JS file, export a function that accepts the nock library and hostname as arguments. Within that function, implement nock to handle the HTTP(S) requests. See `example.js` example below.
2. Create a `serverless-offline-http-mock-enabled` entry in the `custom` section with a truthy (true, 1, etc) or falsy value (false, 0, etc). You can also use environment variables (for example, `${env:MOCK_ENABLED}`).
3. Create a `serverless-offline-http-mock` entry in the `custom` section.
4. For each host, create an entry containing hostname, a list of JS files to load, and an optional directory. See `serverless.yml` example below.
5. In each JS file, export a function that accepts the nock library and hostname as arguments. Within that function, implement nock to handle the HTTP(S) requests. See `example.js` example below.


## Example
Expand All @@ -37,6 +41,7 @@ serverless.yml:
```yaml
...
custom:
serverless-offline-http-mock-enabled: 1
serverless-offline-http-mock:
- hostname: http://www.example.com
directory: 'mocks' # Optional
Expand Down
2 changes: 1 addition & 1 deletion __test__/integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ describe('test local invoke', () => {

expect(result.status).toEqual(0);
});
});
});
5 changes: 1 addition & 4 deletions __test__/integration/mocks/example.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
const mocks = (nock, hostname) => {
nock(hostname)
.persist()
.get('/example')
.reply(200, 'success!');
nock(hostname).persist().get('/example').reply(200, 'success!');
};

module.exports = mocks;
8 changes: 4 additions & 4 deletions __test__/integration/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
"main": "index.js",
"license": "MIT",
"dependencies": {
"axios": "^0.18.1",
"serverless": "^1.41.1",
"serverless-offline": "^4.10.0",
"serverless-offline-http-mock": "file:../../"
"axios": "^0.27.2",
"serverless": "^3.22.0",
"serverless-offline": "^9.2.6",
"serverless-offline-http-mock": "file:../.."
}
}
3 changes: 2 additions & 1 deletion __test__/integration/serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ service: serverless-offline-http-mock-test

provider:
name: aws
runtime: nodejs8.10
runtime: nodejs16.x

functions:
example:
Expand All @@ -13,6 +13,7 @@ functions:
method: get

custom:
serverless-offline-http-mock-enabled: 1
serverless-offline-http-mock:
- hostname: http://www.example.com
directory: 'mocks' # Optional
Expand Down
Loading

0 comments on commit e07ee7f

Please sign in to comment.