Skip to content

[feature] Package dependencies #2

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ syncPreload({ babel: sampleBabelConfig, eslint: sampleEslintConfig });
- Object with the following properties:
- `babel`: [BabelConfig](https://github.com/upgradejs/plugin-preloader/blob/66d1433eae5dc09fdd47ef92f5b2423e2ce8b4f2/src/types/index.ts#L19) object (optional)
- `eslint`: [ESLintConfig](https://github.com/upgradejs/plugin-preloader/blob/66d1433eae5dc09fdd47ef92f5b2423e2ce8b4f2/src/types/index.ts#L23) object (optional)
- `withPackageDependencies`: `boolean` (optional) - if set to `true`, the library will also install the dependencies that are defined in the `package.json` file to prevent the accidental uninstallation of the required packages
- Returns: `void`
- Description: Installs the required packages synchronously based on the Babel and ESLint configurations.

Expand All @@ -81,6 +82,7 @@ syncPreload({ babel: sampleBabelConfig, eslint: sampleEslintConfig });
- Object with the following properties:
- `babel`: [BabelConfig](https://github.com/upgradejs/plugin-preloader/blob/66d1433eae5dc09fdd47ef92f5b2423e2ce8b4f2/src/types/index.ts#L19) object (optional)
- `eslint`: [ESLintConfig](https://github.com/upgradejs/plugin-preloader/blob/66d1433eae5dc09fdd47ef92f5b2423e2ce8b4f2/src/types/index.ts#L23) object (optional)
- `withPackageDependencies`: `boolean` (optional) - if set to `true`, the library will also install the dependencies that are defined in the `package.json` file to prevent the accidental uninstallation of the required packages
- Returns: `Promise<void>`
- Description: Installs the required packages asynchronously based on the Babel and ESLint configurations.

Expand Down
30 changes: 30 additions & 0 deletions __mock__/packages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const packageJsonEmpty = {
name: "test-project",
version: "1.0.0",
};

const packageJsonComplete = {
...packageJsonEmpty,
dependencies: {
lodash: "^4.17.21",
},
devDependencies: {
jest: "^27.0.6",
},
};

const packageJsonOnlyDep = {
...packageJsonEmpty,
dependencies: {
lodash: "^4.17.21",
},
};

const packageJsonOnlyDev = {
...packageJsonEmpty,
devDependencies: {
jest: "^27.0.6",
},
};

export { packageJsonComplete, packageJsonOnlyDep, packageJsonOnlyDev, packageJsonEmpty };
16 changes: 8 additions & 8 deletions __tests__/getEntitiesMapWithVersions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ describe("getEntitiesMapWithVersionsSync", () => {
},
"plugin:@typescript-eslint/recommended": {
nameInRegistry: "@typescript-eslint/eslint-plugin",
currentVersion: "5.56.0",
desiredVersion: "5.56.0",
currentVersion: "5.58.0",
desiredVersion: "5.58.0",
},
"@typescript-eslint/parser": {
nameInRegistry: "@typescript-eslint/parser",
currentVersion: "5.56.0",
desiredVersion: "5.56.0",
currentVersion: "5.58.0",
desiredVersion: "5.58.0",
},
});
});
Expand Down Expand Up @@ -107,13 +107,13 @@ describe("getEntitiesMapWithVersionsAsync", () => {
},
"plugin:@typescript-eslint/recommended": {
nameInRegistry: "@typescript-eslint/eslint-plugin",
currentVersion: "5.56.0",
desiredVersion: "5.56.0",
currentVersion: "5.58.0",
desiredVersion: "5.58.0",
},
"@typescript-eslint/parser": {
nameInRegistry: "@typescript-eslint/parser",
currentVersion: "5.56.0",
desiredVersion: "5.56.0",
currentVersion: "5.58.0",
desiredVersion: "5.58.0",
},
});
});
Expand Down
44 changes: 44 additions & 0 deletions __tests__/getPackageDependencies.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import getPackageDependencies from "@core/getPackageDependencies";
import {
packageJsonComplete,
packageJsonEmpty,
packageJsonOnlyDep,
packageJsonOnlyDev,
} from "__mock__/packages";

describe("getPackageDependencies", () => {
afterEach(() => {
jest.resetModules();
});

it("should return an empty array if no dependencies or devDependencies are present", () => {
jest.mock(`${process.cwd() + "/package.json"}`, () => packageJsonEmpty);

const result = getPackageDependencies();
expect(result).toEqual([]);
});

it("should return the correct dependencies and devDependencies", () => {
jest.mock(`${process.cwd() + "/package.json"}`, () => packageJsonComplete);

const result = getPackageDependencies();
expect(result).toEqual([
{ nameInRegistry: "lodash", desiredVersion: "^4.17.21" },
{ nameInRegistry: "jest", desiredVersion: "^27.0.6" },
]);
});

it("should handle the case when only dependencies are present", () => {
jest.mock(`${process.cwd() + "/package.json"}`, () => packageJsonOnlyDep);

const result = getPackageDependencies();
expect(result).toEqual([{ nameInRegistry: "lodash", desiredVersion: "^4.17.21" }]);
});

it("should handle the case when only devDependencies are present", () => {
jest.mock(`${process.cwd() + "/package.json"}`, () => packageJsonOnlyDev);

const result = getPackageDependencies();
expect(result).toEqual([{ nameInRegistry: "jest", desiredVersion: "^27.0.6" }]);
});
});
47 changes: 30 additions & 17 deletions __tests__/plugin-preloader.spec.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
import { asyncPreload, syncPreload } from "@core/index";
import * as wrappers from "@core/utils/wrappers";
import { sampleBabelConfig, sampleEslintConfig } from "__mock__/configs";
import { packageJsonComplete } from "__mock__/packages";

const asyncExecMock = wrappers.asyncExec as jest.Mock;
const syncExecMock = wrappers.syncExec as jest.Mock;

jest.mock("@core/utils/wrappers");

const expectedBabelDependencies =
"@babel/preset-react@7.0.0 @babel/plugin-proposal-decorators@latest @babel/plugin-proposal-private-methods@8.0.0 @babel/preset-env@latest";
"@babel/preset-react@7.0.0 @babel/plugin-transform-runtime@latest @babel/plugin-proposal-decorators@latest @babel/plugin-proposal-private-methods@8.0.0 @babel/preset-env@latest";
const expectedEslintDependencies =
"@typescript-eslint/eslint-plugin@latest eslint-plugin-abcsize@latest @typescript-eslint/parser@latest";

describe("asyncPreload - checking wrappers.asyncExec calls", () => {
beforeEach(() => {
asyncExecMock.mockClear();
asyncExecMock.mockImplementation(() => {
return { stdout: "Mocked stdout" };
});
});

afterEach(() => {
asyncExecMock.mockRestore();
asyncExecMock.mockClear();
});

test("should call asyncExec with the correct command for Babel config", async () => {
asyncExecMock.mockImplementation(async () => {
return { stdout: "Mocked stdout" };
});
await asyncPreload({ babel: sampleBabelConfig });
expect(asyncExecMock).toHaveBeenCalled();
expect(asyncExecMock.mock.calls[0][0]).toContain(
Expand All @@ -38,24 +38,33 @@ describe("asyncPreload - checking wrappers.asyncExec calls", () => {
});

test("should call asyncExec with the correct command for ESLint config", async () => {
asyncExecMock.mockImplementation(async () => {
return { stdout: "Mocked stdout" };
});
await asyncPreload({ eslint: sampleEslintConfig });
expect(asyncExecMock).toHaveBeenCalled();
expect(asyncExecMock.mock.calls[0][0]).toContain(
`npm install ${expectedEslintDependencies} --no-save --no-audit`
);
});

test("should call asyncExec with the correct command when withPackageDependencies is true", async () => {
jest.mock(`${process.cwd() + "/package.json"}`, () => packageJsonComplete);
await asyncPreload({ babel: sampleBabelConfig, withPackageDependencies: true });
expect(asyncExecMock).toHaveBeenCalled();
expect(asyncExecMock.mock.calls[0][0]).toContain(
`npm install lodash@^4.17.21 jest@^27.0.6 ${expectedBabelDependencies} --no-save --no-audit`
);
jest.resetModules();
});
});

describe("syncPreload - checking wrappers.syncExec calls", () => {
beforeEach(() => {
syncExecMock.mockClear();
syncExecMock.mockImplementation(() => {
return "Mocked stdout";
});
});

afterEach(() => {
syncExecMock.mockRestore();
syncExecMock.mockClear();
});

test("should not call syncExecMock for ESLint config", async () => {
Expand All @@ -64,9 +73,6 @@ describe("syncPreload - checking wrappers.syncExec calls", () => {
});

test("should call syncExec with the correct command for Babel config", () => {
syncExecMock.mockImplementation(() => {
return "Mocked stdout";
});
syncPreload({ babel: sampleBabelConfig });
expect(syncExecMock).toHaveBeenCalled();
expect(syncExecMock.mock.calls[0][0]).toContain(
Expand All @@ -75,13 +81,20 @@ describe("syncPreload - checking wrappers.syncExec calls", () => {
});

test("should call syncExec with the correct command for ESLint config", () => {
syncExecMock.mockImplementation(() => {
return "Mocked stdout";
});
syncPreload({ eslint: sampleEslintConfig });
expect(syncExecMock).toHaveBeenCalled();
expect(syncExecMock.mock.calls[0][0]).toContain(
`npm install ${expectedEslintDependencies} --no-save --no-audit`
);
});

test("should call syncExec with the correct command when withPackageDependencies is true", () => {
jest.mock(`${process.cwd() + "/package.json"}`, () => packageJsonComplete);
syncPreload({ babel: sampleBabelConfig, withPackageDependencies: true });
expect(syncExecMock).toHaveBeenCalled();
expect(syncExecMock.mock.calls[0][0]).toContain(
`npm install lodash@^4.17.21 jest@^27.0.6 ${expectedBabelDependencies} --no-save --no-audit`
);
jest.resetModules();
});
});
Loading