From 61379daf11b49875b02ca6c34a01d1e99e683ff9 Mon Sep 17 00:00:00 2001 From: Piotr Kuczynski Date: Fri, 21 Nov 2025 18:51:24 +0100 Subject: [PATCH] test(cli): fix tests for init command (#11787) --- .github/workflows/tests.yml | 4 ++-- src/commands/InitCommand.ts | 11 +++++----- test/github-issues/8975/issue-8975.ts | 30 ++++++++++++--------------- 3 files changed, 21 insertions(+), 24 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index d9a1011e0..ff79589fa 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -96,7 +96,7 @@ jobs: strategy: fail-fast: false matrix: - node-version: [18, 20] + node-version: [20, 24] uses: ./.github/workflows/tests-linux.yml with: node-version: ${{matrix.node-version}} @@ -106,7 +106,7 @@ jobs: needs: [detect-changes, build] uses: ./.github/workflows/tests-windows.yml with: - node-version: 20 + node-version: 22 coverage: if: contains(needs.detect-changes.outputs.changes, 'src-or-tests') diff --git a/src/commands/InitCommand.ts b/src/commands/InitCommand.ts index 95f654336..db017fb0f 100644 --- a/src/commands/InitCommand.ts +++ b/src/commands/InitCommand.ts @@ -6,8 +6,6 @@ import { TypeORMError } from "../error" import { PlatformTools } from "../platform/PlatformTools" import { CommandUtils } from "./CommandUtils" -import ourPackageJson from "../../package.json" - /** * Generates a new project with TypeORM. */ @@ -117,7 +115,7 @@ export class InitCommand implements yargs.CommandModule { ) await CommandUtils.createFile( basePath + "/package.json", - InitCommand.appendPackageJson( + await InitCommand.appendPackageJson( packageJsonContents, database, isExpress, @@ -673,13 +671,16 @@ Steps to run this project: /** * Appends to a given package.json template everything needed. */ - protected static appendPackageJson( + protected static async appendPackageJson( packageJsonContents: string, database: string, express: boolean, projectIsEsm: boolean /*, docker: boolean*/, - ): string { + ): Promise { const packageJson = JSON.parse(packageJsonContents) + const ourPackageJson = JSON.parse( + await CommandUtils.readFile(`${__dirname}/../package.json`), + ) if (!packageJson.devDependencies) packageJson.devDependencies = {} packageJson.devDependencies = { diff --git a/test/github-issues/8975/issue-8975.ts b/test/github-issues/8975/issue-8975.ts index 991fd8774..8175f534e 100644 --- a/test/github-issues/8975/issue-8975.ts +++ b/test/github-issues/8975/issue-8975.ts @@ -1,10 +1,9 @@ import { expect } from "chai" import { exec } from "child_process" -import { chmod, readFile, rm, unlink, writeFile } from "fs/promises" -import { dirname } from "path" +import { readFile, rm, unlink, writeFile } from "fs/promises" describe("cli init command", () => { - const cliPath = `${dirname(dirname(dirname(__dirname)))}/src/cli.js` + const cliPath = `${__dirname}/../../../src/cli.js` const databaseOptions = [ "mysql", "mariadb", @@ -20,21 +19,18 @@ describe("cli init command", () => { const builtSrcDirectory = "build/compiled/src" before(async () => { - const copyPackageJson = async () => { - // load package.json from the root of the project - const packageJson = JSON.parse( - await readFile("./package.json", "utf8"), - ) - packageJson.version = `0.0.0` // install no version but - packageJson.installFrom = `file:../${builtSrcDirectory}` // use the built src directory - // write the modified package.json to the build directory - await writeFile( - `./${builtSrcDirectory}/package.json`, - JSON.stringify(packageJson, null, 4), - ) - } + // load package.json from the root of the project + const packageJson = JSON.parse(await readFile("./package.json", "utf8")) - await Promise.all([chmod(cliPath, 0o755), copyPackageJson()]) + // init command is taking typeorm version from package.json + // so ensure we are working against local build + packageJson.version = `file:../${builtSrcDirectory}` + + // write the modified package.json to the build directory + await writeFile( + `./${builtSrcDirectory}/package.json`, + JSON.stringify(packageJson, null, 4), + ) }) after(async () => {