Skip to content

Commit

Permalink
test(esbuild-bundle-analyzer): address pnpm issues
Browse files Browse the repository at this point in the history
  • Loading branch information
tido64 committed Nov 7, 2023
1 parent bd1b59a commit 6897292
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 69 deletions.
2 changes: 2 additions & 0 deletions .changeset/proud-keys-lay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
1 change: 1 addition & 0 deletions incubator/esbuild-bundle-analyzer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"@rnx-kit/jest-preset": "*",
"@rnx-kit/scripts": "*",
"@rnx-kit/tools-node": "^2.0.0",
"@types/jest": "^29.2.1",
"@types/node": "^18.0.0",
"@types/yargs": "^16.0.0",
"eslint": "^8.0.0",
Expand Down
29 changes: 8 additions & 21 deletions incubator/esbuild-bundle-analyzer/test/__mocks__/fs.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,11 @@
// Copied from metro-plugin-duplicates-checker/test/__mocks__/fs.js
const fs = jest.createMockFromModule("node:fs");
const actualFs = jest.requireActual("node:fs");

// Under normal circumstances, this extra copy of '@react-native/polyfills'
// should not be installed.
const extraPolyfills =
"/packages/test-app/node_modules/@react-native/polyfills";

fs.readFileSync = actualFs.readFileSync;

fs.realpathSync = actualFs.realpathSync;
const actualRealpathSyncNative = actualFs.realpathSync.native;
fs.realpathSync.native = (path, ...args) => {
if (path.replace(/\\/g, "/").endsWith(extraPolyfills)) {
return path;
} else {
return actualRealpathSyncNative(path, ...args);
}
const fs = require("@rnx-kit/metro-plugin-duplicates-checker/test/__mocks__/fs.js");
const fsActual = jest.requireActual("fs");

const readFileSync = fs.readFileSync;
fs.readFileSync = (p, options) => {
return p.includes("__fixtures__")
? fsActual.readFileSync(p, options)
: readFileSync(p, options);
};

fs.statSync = actualFs.statSync; // Used by 'pkg-dir'

module.exports = fs;
63 changes: 17 additions & 46 deletions incubator/esbuild-bundle-analyzer/test/duplicates.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,50 +10,13 @@ import {
file50Path,
firstDuplicate,
inputWithDuplicates,
inputWithDuplicatesFS,
inputWithoutDuplicates,
repoRoot,
} from "./testData";

const fixturePath = path.join(process.cwd(), "test", "__fixtures__");
const fixturePath = path.join(__dirname, "__fixtures__");

jest.mock("fs");
jest.mock("@rnx-kit/tools-node/package");

// Under normal circumstances, this extra copy of '@react-native/polyfills'
// should not be installed.
// Copied from metro-plugin-duplicates-checker/test/checkForDuplicatePackages.test.ts
const extraPolyfills = `${repoRoot.replace(
/\\/g,
"/"
)}/packages/test-app/node_modules/@react-native/polyfills`;
require("@rnx-kit/tools-node/package").findPackageDir = jest
.fn()
.mockImplementation((startDir) => {
switch (startDir) {
// Under normal circumstances, this extra copy of '@react-native/polyfills'
// should not be installed.
case `${extraPolyfills}/index.js`:
return extraPolyfills;
default:
return jest
.requireActual("@rnx-kit/tools-node/package")
.findPackageDir(startDir);
}
});
require("@rnx-kit/tools-node/package").readPackage = jest
.fn()
.mockImplementation((path) => {
if (path.replace(/\\/g, "/").includes(extraPolyfills)) {
return {
name: "@react-native/polyfills",
version: "1.0.0",
};
} else {
return jest
.requireActual("@rnx-kit/tools-node/package")
.readPackage(path);
}
});

describe("generateGraph()", () => {
test("generateGraph", () => {
Expand Down Expand Up @@ -135,13 +98,21 @@ describe("getWhyDuplicatesInBundle()", () => {
});

describe("getDuplicates()", () => {
expect(getDuplicates(inputWithoutDuplicates, "")).toEqual({
banned: 0,
duplicates: 0,
});
const fs = require("fs");

afterAll(() => fs.__setMockFiles());

expect(getDuplicates(inputWithDuplicates, "")).toEqual({
banned: 0,
duplicates: 1,
test("finds duplicates", () => {
fs.__setMockFiles(inputWithDuplicatesFS);

expect(getDuplicates(inputWithoutDuplicates, "")).toEqual({
banned: 0,
duplicates: 0,
});

expect(getDuplicates(inputWithDuplicates, "")).toEqual({
banned: 0,
duplicates: 1,
});
});
});
17 changes: 15 additions & 2 deletions incubator/esbuild-bundle-analyzer/test/testData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ import * as path from "node:path";
import type { Input } from "../src/metafile";
import type { Result } from "../src/types";

const pkgDir = jest.requireActual("pkg-dir");
export const repoRoot = pkgDir.sync(path.resolve(__dirname, "..", ".."));
export const repoRoot = path.resolve(__dirname, "..", "..", "..");

function makeManifest(name, version = "0.0.0") {
return JSON.stringify({ name, version });
}

export const inputWithDuplicates: Record<string, Input> = {};
inputWithDuplicates[
Expand Down Expand Up @@ -92,6 +95,16 @@ inputWithDuplicates[`${repoRoot}/node_modules/chalk/source/index.js`] = {
format: "cjs",
};

export const inputWithDuplicatesFS: Record<string, string> = {
"repo/node_modules/@apollo/client/package.json":
makeManifest("@apollo/client"),
"repo/packages/ra-ui-materialui/package.json":
makeManifest("ra-ui-materialui"),
[`${repoRoot}/node_modules/@rnx-kit/build/node_modules/chalk/package.json`]:
makeManifest("chalk"),
[`${repoRoot}/node_modules/chalk/package.json`]: makeManifest("chalk"),
};

export const inputWithoutDuplicates: Record<string, Input> = {
"repo/node_modules/@apollo/client/utilities/globals/fix-graphql.js": {
bytes: 237,
Expand Down
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3623,6 +3623,7 @@ __metadata:
"@rnx-kit/metro-plugin-duplicates-checker": ^2.1.2
"@rnx-kit/scripts": "*"
"@rnx-kit/tools-node": ^2.0.0
"@types/jest": ^29.2.1
"@types/node": ^18.0.0
"@types/yargs": ^16.0.0
chalk: ^4.1.0
Expand Down

0 comments on commit 6897292

Please sign in to comment.