Skip to content

Commit

Permalink
Merge branch 'main' into fix/resolver-return-never
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito committed Nov 4, 2023
2 parents 40f026f + 66fb437 commit a31d097
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 7 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/smoke-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: smoke-test

on:
# Always run smoke tests upon a successful
# "ci" job completion on "main".
workflow_run:
workflows: ['ci']
branches: [main]
types: [completed]
workflow_dispatch:

jobs:
examples:
if: ${{ (github.event_name == 'workflow_run' && github.event.workflow_run.conclusion == 'success') || github.event_name == 'workflow_dispatch' }}
runs-on: macos-latest
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: 18

- name: Set up PNPM
uses: pnpm/action-setup@v2
with:
version: 7.12

- name: Install dependencies
run: pnpm install

- name: Test examples
run: ./config/scripts/smoke-test.sh
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ stats.html
msw-*.tgz
.husky/_
.env
**/test-results
**/test-results

# Smoke test temporary files.
/package.json.copy
/examples
41 changes: 41 additions & 0 deletions config/scripts/smoke-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/bin/bash
set -e

COMMIT_HASH=$(git rev-parse HEAD)
MSW_VERSION="0.0.0-$COMMIT_HASH"
echo "Latest commit: $COMMIT_HASH"
echo "In-progress MSW version: $MSW_VERSION"

PKG_JSON_COPY="package.json.copy"
cp package.json $PKG_JSON_COPY

pnpm version $MSW_VERSION --no-git-tag-version --allow-same-version

echo ""
echo "Packing MSW..."
pnpm pack

EXAMPLES_REPO=https://github.com/mswjs/examples.git
EXAMPLES_DIR=./examples
echo ""
echo "Cloning the examples from "$EXAMPLES_REPO"..."

if [[ -d "$EXAMPLES_DIR" ]]; then
echo "Examples already cloned, skipping..."
else
git clone $EXAMPLES_REPO $EXAMPLES_DIR
fi

echo ""
echo "Installing dependencies..."
cd $EXAMPLES_DIR
pnpm install

echo ""
echo "Linking MSW..."
pnpm add msw --filter="with-*" file:../../../msw-$MSW_VERSION.tgz
pnpm ls msw

echo ""
echo "Running tests..."
CI=1 pnpm test ; (cd ../ && mv $PKG_JSON_COPY ./package.json)
22 changes: 16 additions & 6 deletions src/browser/setupWorker/start/createRequestListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,31 @@ export const createRequestListener = (
* Check that the browser supports that before sending it to the worker.
*/
if (context.supports.readableStreamTransfer) {
const responseStream = response.body
const responseStreamOrNull = response.body

messageChannel.postMessage(
'MOCK_RESPONSE',
{
...responseInit,
body: responseStream,
body: responseStreamOrNull,
},
responseStream ? [responseStream] : undefined,
responseStreamOrNull ? [responseStreamOrNull] : undefined,
)
} else {
// As a fallback, send the response body buffer to the worker.
const responseBuffer = await responseClone.arrayBuffer()
/**
* @note If we are here, this means the current environment doesn't
* support "ReadableStream" as transferable. In that case,
* attempt to read the non-empty response body as ArrayBuffer, if it's not empty.
* @see https://github.com/mswjs/msw/issues/1827
*/
const responseBufferOrNull =
response.body === null
? null
: await responseClone.arrayBuffer()

messageChannel.postMessage('MOCK_RESPONSE', {
...responseInit,
body: responseBuffer,
body: responseBufferOrNull,
})
}

Expand Down

0 comments on commit a31d097

Please sign in to comment.