From the action.yml file we can see that this action will take a single input - the name of the repository and execute the index.js
file inside the dist
directory. This index.js
file is produced by compiling the index.ts. On running index.ts
the run
function is executed which fetches the full name of the repo name we gave as input to the action and prints it. It also sets the output to some custom data gotten from a 3rd party api.
Without log file
npm test
With log file
ACT_LOG=true npm test
The above produces a log file called javascript.log
While testing we don't want to hit the actual github api or the 3rd party api because it might have strict rate limits or might return dynamic data which will change the result of our test each time we run it. So, we are going to test this by running this custom action in a workflow inside a local git repository and mock the api calls during the workflow run. Steps (refer to action.test.ts for implementation):
- Create a workflow file that utilizes this action. Refer to action-test.yml
- Before running the test make sure to compile
index.ts
(handled by a pre script when usingnpm test
) - Create a local git repository using
mock-github
and initialize it with action.yml, dist/index.js and the workflow file we created in step 1 - Create
Moctokit
andMockapi
instances frommock-github
. Set the schema forMockapi
- Use
act-js
to run the workflow inside the local git repository. Make sure to set themockApi
field and pass in the mocks usingMoctokit
andMockapi
.
Note: Notice how we needed to set a proxy for Octokit
but not axios
in our index.ts
file. Some clients respect the proxy env variable used by act-js
to mock apis during workflow runs. For clients that don't respect these env variables, you can write a simple wrapper to use the proxy just like we did here.