This module provides a set of Jest mocks for the EdgeWorkers API. In the Akamai EdgeWorkers execution environment, there are a set of modules and objects provided; since equivalent packages are not available in npm or node, Jest mocks are provided here to be able to execute JS written against the EdgeWorkers API in Node.js for the purpose of testing.
These mocks are provided using manual mocks and allow for user control of their functions.
This isn't a perfect solution, as:
- Mocks provided here will not perfectly replicate the API.
- The APIs provided may not be available in some event handlers, refer to the API docs to see what is available.
- Tests are executed in Node; while both Node and EdgeWorkers run on top of V8, some features are explicitly disabled for EdgeWorkers, there are execution limits for EdgeWorkers (time and memory), and developers need to be careful not to pull in Node APIs.
Additional documentation for EdgeWorkers can be found in Akamai TechDocs.
The EdgeWorker has the following structure:
-
src
- the location of your main.js and bundle.json. All additional modules should go here. -
test
- unit tests
Start a new project and execute the command below :
npm init
Here we are going to cover getting the node modules needed installed, as well as config file setup.
The mocks for this project are published as the node module edgeworkers-jest-mocks. You can install that by running the following in your project directory:
npm install --save-dev edgeworkers-jest-mocks
Make sure you have the following configured in the package.json
file:
- Set the test script to jest:
"scripts": { "test": "jest" },
- Configure Jest so that the EdgeWorkers API mocks are easier to import
"jest": { "testEnvironment": "node", "transformIgnorePatterns": [ "node_modules/(?!edgeworkers-jest-mocks/__mocks__)" ], "moduleDirectories": [ "node_modules", "node_modules/edgeworkers-jest-mocks/__mocks__" ] }
Babel is included as a dependency to fill in for the newer version of EcmaScript used by Akamai EdgeWorkers. To configure this correctly, add the following as a babel.config.json
file (create a json file and name it as babel.config.json
if it does not exists):
{
"presets": ["@babel/preset-env"],
"plugins": [
["@babel/transform-runtime"]
]
}
After importing an edgeworker or its functions from the main.js file, you can write any kind of tests you need. Tests written against EdgeWorker event handlers require creating a Request or Response mock and then calling the event handler function with that mock.
Here is a quick example of a test written in Jest against an EdgeWorker found in src/main.js:
import * as edgeworker from "../src/main.js";
import Request from 'request';
describe('Simple Example', () => {
beforeEach(() => {
jest.clearAllMocks();
});
test("functional test against onClientRequest", async () => {
let requestMock = new Request();
edgeworker.onClientRequest(requestMock);
expect(requestMock.respondWith).toHaveBeenCalledTimes(1);
expect(requestMock.respondWith).toHaveBeenCalledWith(200, {}, "<html><body><h1>Test Page</h1></body></html>");
});
test("unit test an edgeworker function", async () => {
let result = edgeworker.someFunction(42);
expect(result != null);
});
});
More example tests are available under the test/examples folder. If needed, a good overview of using Jest is available from Flaviocopes.
Testing is provided via the Jest framework. To run your unit tests, execute the following command from the command line:
npm test
This will run all tests in the test
directory following the Jest
conventions. If you want more tests, name them in the pattern *.test.js.
Example EdgeWorkers can be found in the Akamai EdgeWorkers Examples repo. Example tests written against these EdgeWorkers are available under the test/examples folder.
When contributing to the repository please describe the change or examples in detail in the pull request.
- Create a pull request.
- Once the pull request is created, If the contributing user has not previously signed a Contributor License Agreement (CLA), they must complete the CLA signing steps as indicated in the pull request. The CLA signature will be stored in an Akamai private repository in Github.
- A code review will be performed by multiple Akamai members of the edgeworkers-unittest repository. The code review must receive at least 2 approvals.
- The pull request will be merged once all of the above criteria have been met.
For more information on EdgeWorkers and EdgeKV, refer to the following resources:
- EdgeWorkers Developer Page
- EdgeWorkers User Guide
- EdgeWorkers API Guide
- Akamai CLI for EdgeWorkers
- EdgeKV Getting Started Guide
If you experience any issues with these code samples, please raise a GitHub issue. Or create a pull request with fixes, suggestions, or your own contributed example.