1
0
mirror of synced 2025-12-25 02:00:48 -05:00
Files
blitz/packages/cli/test/commands/new.test.ts
Justin Hall b3814fc7c0 Standardize prettier options across all Blitz code bases (#703)
Co-authored-by: Brandon Bayer <b@bayer.ws> (meta)
2020-06-19 09:33:57 +07:00

103 lines
2.9 KiB
TypeScript

import {New} from "../../src/commands/new"
import {getLatestVersion} from "@blitzjs/generator/src/utils/get-latest-version"
import * as fs from "fs"
import * as path from "path"
import * as os from "os"
import fetch from "node-fetch"
import nock from "nock"
import rimraf from "rimraf"
jest.setTimeout(120 * 1000)
const blitzCliPackageJson = require("../../package.json")
async function getBlitzDistTags() {
const response = await fetch("https://registry.npmjs.org/-/package/blitz/dist-tags")
return await response.json()
}
describe("`new` command", () => {
describe("when scaffolding new project", () => {
jest.setTimeout(120 * 1000)
async function whileStayingInCWD(task: () => PromiseLike<void>) {
const oldCWD = process.cwd()
await task()
process.chdir(oldCWD)
}
async function withNewApp(test: (dirName: string, packageJson: any) => Promise<void> | void) {
function makeTempDir() {
const tmpDirPath = path.join(os.tmpdir(), "blitzjs-test-")
return fs.mkdtempSync(tmpDirPath)
}
const tempDir = makeTempDir()
await whileStayingInCWD(() => New.run([tempDir, "--skip-install"]))
const packageJsonFile = fs.readFileSync(path.join(tempDir, "package.json"), {
encoding: "utf8",
flag: "r",
})
const packageJson = JSON.parse(packageJsonFile)
await test(tempDir, packageJson)
rimraf.sync(tempDir)
}
it("pins Blitz to the current version", async () =>
await withNewApp(async (_, packageJson) => {
const {
dependencies: {blitz: blitzVersion},
} = packageJson
const {latest, canary} = await getBlitzDistTags()
if (blitzCliPackageJson.version.includes("canary")) {
expect(blitzVersion).toEqual(canary)
} else {
expect(blitzVersion).toEqual(latest)
}
}))
it("fetches latest version from template", async () => {
const expectedVersion = "3.0.0"
const templatePackage = {name: "eslint-plugin-react-hooks", version: "3.x"}
const scope = nock("https://registry.npmjs.org")
scope
.get(`/${templatePackage.name}`)
.reply(200, {versions: {"4.0.0": {}, "3.0.0": {}}})
.persist()
scope
.get(`/-/package/${templatePackage.name}/dist-tags`)
.reply(200, {
latest: "4.0.0",
})
.persist()
const {value: latestVersion} = await getLatestVersion(
templatePackage.name,
templatePackage.version,
)
expect(latestVersion).toBe(expectedVersion)
})
describe("with network trouble", () => {
it("uses template versions", async () => {
nock("https://registry.npmjs.org").get(/.*/).reply(500).persist()
await withNewApp(async (_, packageJson) => {
const {dependencies} = packageJson
expect(dependencies.blitz).toBe("latest")
})
nock.restore()
})
})
})
})