Skip to content

Commit

Permalink
Add integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
berniegp committed Oct 20, 2024
1 parent 2f0d22f commit 5e75e27
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 6 deletions.
21 changes: 15 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ on:

env:
BUILD_ARTIFACT_NAME: build_artifact
BUILD_ARTIFACT_FILENAME: build_artifact.tgz

jobs:
build:
runs-on: ubuntu-latest

outputs:
tarball: ${{ steps.tarball.outputs.filename }}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
Expand All @@ -22,12 +22,13 @@ jobs:
- run: npm ci
- run: npm run test:ci
- name: Make build tarball
run: mv "$(npm pack | tail -1)" $BUILD_ARTIFACT_FILENAME
id: tarball
run: echo "filename=$(npm pack | tail -1)" >> $GITHUB_OUTPUT
- name: Archive build dist
uses: actions/upload-artifact@v4
with:
name: ${{ env.BUILD_ARTIFACT_NAME }}
path: ${{ env.BUILD_ARTIFACT_FILENAME }}
path: ${{ steps.tarball.outputs.filename }}

packageTest:
needs: build
Expand All @@ -47,15 +48,23 @@ jobs:
# - 22
# - 23

env:
BUILD_ARTIFACT_FILENAME: ${{ needs.build.outputs.tarball }}

steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
# node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: cd integration_tests
- name: Download the build artifact
uses: actions/download-artifact@v4
with:
name: ${{ env.BUILD_ARTIFACT_NAME }}
- run: npm install $BUILD_ARTIFACT_FILENAME --only=prod
- run: ls node_modules
- name: Integration test - esm
run: |
cd esm && \
ls .. && \
npm install "../$BUILD_ARTIFACT_FILENAME" --only=prod && \
npm run test
33 changes: 33 additions & 0 deletions integration_tests/esm/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { assert } from 'node:assert';
import { newServer } from 'mock-xmlhttprequest';

Check failure on line 2 in integration_tests/esm/index.js

View workflow job for this annotation

GitHub Actions / build

Unable to resolve path to module 'mock-xmlhttprequest'

function functionToTest() {
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();

Check failure on line 6 in integration_tests/esm/index.js

View workflow job for this annotation

GitHub Actions / build

'XMLHttpRequest' is not defined
xhr.open('GET', '/my/url');
xhr.onload = () => resolve(JSON.parse(xhr.response));
xhr.onerror = () => reject(xhr.statusText);
xhr.send();
});
}

const server = newServer({
get: ['/my/url', {
// status: 200 is the default
headers: { 'Content-Type': 'application/json' },
body: '{ "message": "Success!" }',
}],
});

async function integrationTest() {
try {
server.install();
const result = await functionToTest();
assert.equal(result.message, 'Success!');
} finally {
// Restore the original XMLHttpRequest
server.remove();
}
}

integrationTest();
11 changes: 11 additions & 0 deletions integration_tests/esm/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "mock-xmlhttprequest-integration-test-esm",
"description": "Integration test for XMLHttpRequest in a Node.js module project",
"type": "module",
"scripts": {
"test": "node index.js"
},
"engines": {
"node": ">=16.0.0"
}
}

0 comments on commit 5e75e27

Please sign in to comment.