Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(metro-plugin-duplicates-checker): address pnpm issues #2797

Merged
merged 2 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/rich-adults-mix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@rnx-kit/metro-plugin-duplicates-checker": patch
---

Fix error message not containing the path we're trying to resolve
21 changes: 1 addition & 20 deletions packages/cli/test/__mocks__/fs.js
Original file line number Diff line number Diff line change
@@ -1,20 +1 @@
const fs = jest.createMockFromModule("fs");

const { vol } = require("memfs");

/** @type {(newMockFiles: { [filename: string]: string }) => void} */
fs.__setMockFiles = (files) => {
vol.reset();
vol.fromJSON(files);
};

fs.__toJSON = () => vol.toJSON();

fs.lstat = (...args) => Promise.resolve(vol.lstat(...args));
fs.lstatSync = (...args) => vol.lstatSync(...args);
fs.mkdirSync = (...args) => vol.mkdirSync(...args);
fs.readFileSync = (...args) => vol.readFileSync(...args);
fs.stat = (...args) => Promise.resolve(vol.stat(...args));
fs.statSync = (...args) => vol.statSync(...args);

module.exports = fs;
module.exports = require("@rnx-kit/metro-plugin-duplicates-checker/test/__mocks__/fs");
2 changes: 1 addition & 1 deletion packages/metro-plugin-duplicates-checker/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@
"@types/node": "^18.0.0",
"eslint": "^8.0.0",
"jest": "^29.2.1",
"memfs": "^4.0.0",
"metro": "^0.76.5",
"metro-source-map": "^0.76.8",
"pkg-dir": "^5.0.0",
"prettier": "^3.0.0",
"typescript": "^5.0.0"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function normalizePath(p: string): string {
export function resolveModule(source: string): ModuleInfo {
const pkg = findPackageDir(source);
if (!pkg) {
throw new Error(`Unable to find package '${pkg}'`);
throw new Error(`Unable to resolve module '${source}'`);
}

const { name, version } = readPackage(pkg);
Expand Down
31 changes: 15 additions & 16 deletions packages/metro-plugin-duplicates-checker/test/__mocks__/fs.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
const fs = jest.createMockFromModule("fs");
const actualFs = jest.requireActual("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";
const { vol } = require("memfs");

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);
}
/** @type {(newMockFiles: { [filename: string]: string }) => void} */
fs.__setMockFiles = (files) => {
vol.reset();
vol.fromJSON(files);
};

fs.statSync = actualFs.statSync; // Used by 'pkg-dir'
fs.__toJSON = () => vol.toJSON();

fs.lstat = (...args) => vol.lstat(...args);
fs.lstatSync = (...args) => vol.lstatSync(...args);
fs.mkdirSync = (...args) => vol.mkdirSync(...args);
fs.readFileSync = (...args) => vol.readFileSync(...args);
fs.realpathSync = (...args) => vol.realpathSync(...args);
fs.realpathSync.native = (...args) => vol.realpathSync(...args);
fs.stat = (...args) => vol.stat(...args);
fs.statSync = (...args) => vol.statSync(...args);

module.exports = fs;

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,49 +8,16 @@ import {
} from "../src/checkForDuplicatePackages";
import {
bundleGraph,
bundleGraphFS,
bundleGraphWithDuplicates,
bundleGraphWithDuplicatesFS,
bundleSourceMap,
bundleSourceMapFS,
bundleSourceMapWithDuplicates,
repoRoot,
bundleSourceMapWithDuplicatesFS,
} from "./testData";

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

// Under normal circumstances, this extra copy of '@react-native/polyfills'
// should not be installed.
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);
}
});

const defaultOptions: Options = {
ignoredModules: [],
Expand Down Expand Up @@ -163,6 +130,8 @@ describe("detectDuplicatePackages()", () => {
});

describe("checkForDuplicateDependencies()", () => {
const fs = require("fs");

const consoleErrorSpy = jest.spyOn(global.console, "error");
const consoleWarnSpy = jest.spyOn(global.console, "warn");

Expand All @@ -172,17 +141,22 @@ describe("checkForDuplicateDependencies()", () => {
});

afterAll(() => {
fs.__setMockFiles();
jest.clearAllMocks();
});

test("checkForDuplicateDependencies", () => {
fs.__setMockFiles(bundleGraphFS);

expect(checkForDuplicateDependencies(bundleGraph)).toEqual({
banned: 0,
duplicates: 0,
});
expect(consoleErrorSpy).not.toHaveBeenCalled();
expect(consoleWarnSpy).not.toHaveBeenCalled();

fs.__setMockFiles(bundleGraphWithDuplicatesFS);

expect(checkForDuplicateDependencies(bundleGraphWithDuplicates)).toEqual({
banned: 0,
duplicates: 1,
Expand All @@ -193,11 +167,20 @@ describe("checkForDuplicateDependencies()", () => {
});

describe("checkForDuplicatePackages()", () => {
const fs = require("fs");

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

test("checkForDuplicatePackages", () => {
fs.__setMockFiles(bundleSourceMapFS);

expect(checkForDuplicatePackages(bundleSourceMap)).toEqual({
banned: 0,
duplicates: 0,
});

fs.__setMockFiles(bundleSourceMapWithDuplicatesFS);

expect(checkForDuplicatePackages(bundleSourceMapWithDuplicates)).toEqual({
banned: 0,
duplicates: 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@ import {
normalizePath,
resolveModule,
} from "../src/gatherModules";
import { bundleGraph, bundleSourceMap } from "./testData";
import {
bundleGraph,
bundleGraphFS,
bundleSourceMap,
bundleSourceMapFS,
} from "./testData";

jest.mock("fs");

describe("normalizePath()", () => {
test("trims Webpack URLs", () => {
Expand All @@ -23,23 +30,58 @@ describe("normalizePath()", () => {
describe("resolveModule()", () => {
test("throws if a package is not found", () => {
expect(() => resolveModule("/this-package-does-not-exist")).toThrow(
"Unable to find package"
"Unable to resolve module"
);
});
});

describe("gatherModulesFromGraph()", () => {
const fs = require("fs");

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

test("builds module map from a basic source map", () => {
fs.__setMockFiles(bundleGraphFS);

const modules = gatherModulesFromGraph(bundleGraph, {});
expect(Object.keys(modules).sort()).toMatchSnapshot();

expect(Object.keys(modules).sort()).toEqual(["react-native"]);
});
});

describe("gatherModulesFromSourceMap()", () => {
const fs = require("fs");

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

test("builds module map from a basic source map", () => {
fs.__setMockFiles(bundleSourceMapFS);

const modules = gatherModulesFromSources(bundleSourceMap.sources, {});

expect(Object.keys(modules).sort()).toMatchSnapshot();
expect(Object.keys(modules).sort()).toEqual([
"@babel/runtime",
"abort-controller",
"anser",
"base64-js",
"event-target-shim",
"invariant",
"metro",
"nullthrows",
"object-assign",
"pretty-format",
"promise",
"prop-types",
"react",
"react-devtools-core",
"react-is",
"react-native",
"react-refresh",
"regenerator-runtime",
"scheduler",
"stacktrace-parser",
"whatwg-fetch",
]);

Object.keys(modules).forEach((name) => {
expect(Object.keys(modules[name]).length).toBe(1);
Expand Down
Loading