test(cli): fix tests for init command (#11787)

This commit is contained in:
Piotr Kuczynski 2025-11-21 18:51:24 +01:00 committed by GitHub
parent d943a17639
commit 61379daf11
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 21 additions and 24 deletions

View File

@ -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')

View File

@ -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<string> {
const packageJson = JSON.parse(packageJsonContents)
const ourPackageJson = JSON.parse(
await CommandUtils.readFile(`${__dirname}/../package.json`),
)
if (!packageJson.devDependencies) packageJson.devDependencies = {}
packageJson.devDependencies = {

View File

@ -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 () => {