Skip to content

Commit

Permalink
chore(#zimic): upgrade msw to v2.7.0 (#423)
Browse files Browse the repository at this point in the history
  • Loading branch information
diego-aquino authored Jan 25, 2025
1 parent c5b95cf commit 2a60673
Show file tree
Hide file tree
Showing 6 changed files with 484 additions and 182 deletions.
5 changes: 0 additions & 5 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,6 @@ updates:
- dependency-name: chalk
update-types:
- version-update:semver-major
- dependency-name: msw
update-types:
- version-update:semver-major
- version-update:semver-minor
- version-update:semver-patch

- package-ecosystem: github-actions
directories:
Expand Down
2 changes: 2 additions & 0 deletions examples/with-jest-jsdom/tests/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class TestEnvironment extends JSDOMEnvironment {
this.global.TextEncoder = TextEncoder;
this.global.TextDecoder = TextDecoder;
this.global.ReadableStream = ReadableStream;
this.global.TransformStream = TransformStream;
this.global.BroadcastChannel = BroadcastChannel;
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/zimic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@
"chalk": "4.1.2",
"execa": "9.5.2",
"isomorphic-ws": "5.0.0",
"msw": "2.4.3",
"msw": "2.7.0",
"openapi-typescript": "7.5.2",
"ws": "8.18.0",
"yargs": "17.7.2"
Expand Down
24 changes: 20 additions & 4 deletions packages/zimic/scripts/__tests__/postinstall.node.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
import mswPackageJSON from 'msw/package.json';
import filesystem from 'fs/promises';
import path from 'path';
import { describe, expect, it } from 'vitest';

import { postinstallPromise } from '../postinstall';
import { MSW_PACKAGE_PATH, MSW_BROWSER_DIRECTORY, MSWPackage, postinstallPromise } from '../postinstall';

describe('Post-install script', () => {
it('should remove the default MSW export limitations after installation', async () => {
it('should patch msw/package.json exports', async () => {
await postinstallPromise;

const { exports } = mswPackageJSON;
const mswPackageContentAsString = await filesystem.readFile(MSW_PACKAGE_PATH, 'utf-8');
const mswPackageContent = JSON.parse(mswPackageContentAsString) as MSWPackage;

const { exports } = mswPackageContent;

expect(exports['./browser'].node).toEqual(exports['./node'].node);
expect(exports['./node'].browser).toEqual(exports['./browser'].browser);
});

it.each(['index.js', 'index.mjs'])(
'should patch a missing undefined check in msw/browser/%s',
async (indexFileName) => {
await postinstallPromise;

const mswBrowserPath = path.join(MSW_BROWSER_DIRECTORY, indexFileName);
const mswBrowserContent = await filesystem.readFile(mswBrowserPath, 'utf-8');

expect(mswBrowserContent).toContain('if (!request || responseJson.type?.includes("opaque")) {');
},
);
});
36 changes: 29 additions & 7 deletions packages/zimic/scripts/postinstall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import path from 'path';

import { Override } from '@/types/utils';

type MSWPackage = typeof mswPackage;
type MSWExports = MSWPackage['exports'];
export type MSWPackage = typeof mswPackage;
export type MSWExports = MSWPackage['exports'];

async function patchMSWExports() {
const mswRootDirectory = path.join(require.resolve('msw'), '..', '..', '..');
const mswPackagePath = path.join(mswRootDirectory, 'package.json');
export const MSW_ROOT_DIRECTORY = path.join(require.resolve('msw'), '..', '..', '..');
export const MSW_PACKAGE_PATH = path.join(MSW_ROOT_DIRECTORY, 'package.json');
export const MSW_BROWSER_DIRECTORY = path.join(MSW_ROOT_DIRECTORY, 'lib', 'browser');

const mswPackageContentAsString = await filesystem.readFile(mswPackagePath, 'utf-8');
async function patchMSWExports() {
const mswPackageContentAsString = await filesystem.readFile(MSW_PACKAGE_PATH, 'utf-8');
const mswPackageContent = JSON.parse(mswPackageContentAsString) as MSWPackage;

const browserExports = mswPackageContent.exports['./browser'] as Override<
Expand All @@ -28,11 +29,32 @@ async function patchMSWExports() {
nodeExports.browser = browserExports.browser;

const patchedMSWPackageContentAsString = JSON.stringify(mswPackageContent, null, 2);
await filesystem.writeFile(mswPackagePath, patchedMSWPackageContentAsString);
await filesystem.writeFile(MSW_PACKAGE_PATH, patchedMSWPackageContentAsString);
}

// This is a temporary workaround. Once https://github.com/mswjs/msw/issues/2146 is fixed, we'll be able to remove it.
async function patchMSWBrowserEntry() {
for (const indexFileName of ['index.js', 'index.mjs']) {
const mswBrowserPath = path.join(MSW_BROWSER_DIRECTORY, indexFileName);
const mswBrowserContent = await filesystem.readFile(mswBrowserPath, 'utf-8');

const patchedMSWBrowserContent = mswBrowserContent.replace(
`if (responseJson.type?.includes("opaque")) {
return;
}`,

`if (!request || responseJson.type?.includes("opaque")) {
return;
}`,
);

await filesystem.writeFile(mswBrowserPath, patchedMSWBrowserContent);
}
}

async function postinstall() {
await patchMSWExports();
await patchMSWBrowserEntry();
}

export const postinstallPromise = postinstall();
Loading

0 comments on commit 2a60673

Please sign in to comment.