Skip to content

Commit

Permalink
feat(tests) Add expo go config changes tests
Browse files Browse the repository at this point in the history
I also moved around the expo go configuration and `findAndUpdateDependencyVersions()` function so that it would be easier to test.
  • Loading branch information
markrickert committed Oct 3, 2023
1 parent b812278 commit 7bd6908
Show file tree
Hide file tree
Showing 4 changed files with 966 additions and 1,055 deletions.
34 changes: 7 additions & 27 deletions src/commands/new.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { EOL } from "os"
import { GluegunToolbox } from "../types"
import {
isAndroidInstalled,
Expand All @@ -24,7 +25,10 @@ import {
import type { ValidationsExports } from "../tools/validations"
import { boolFlag } from "../tools/flag"
import { cache } from "../tools/cache"
import { EOL } from "os"
import {
expoGoCompatExpectedVersions,
findAndUpdateDependencyVersions,
} from "../tools/expoGoCompatibility"

type Workflow = "expo" | "prebuild" | "manual"

Expand Down Expand Up @@ -527,19 +531,10 @@ module.exports = {
.replace(/start --ios/g, "run:ios")
} else {
// Expo Go workflow, swap back to compatible Expo Go versions of modules
const expoGoCompat = {
"@react-native-async-storage/async-storage": "1.18.2",
"expo-application": "~5.3.0",
"expo-device": "~5.4.0",
"expo-file-system": "~15.4.4",
"expo-font": "~11.4.0",
"expo-localization": "~14.3.0",
}

log("Changing some dependencies for Expo Go compatibility...")
log(JSON.stringify(expoGoCompat))
log(JSON.stringify(expoGoCompatExpectedVersions))

packageJsonRaw = findAndUpdateDependencyVersions(packageJsonRaw, expoGoCompat)
packageJsonRaw = findAndUpdateDependencyVersions(packageJsonRaw, expoGoCompatExpectedVersions)
}

// - Then write it back out.
Expand Down Expand Up @@ -822,18 +817,3 @@ function buildCliCommand(args: {

return cliCommand
}

function findAndUpdateDependencyVersions(
packageJsonRaw: string,
dependencies: Record<string, string>,
): string {
let updatedPackageJson = packageJsonRaw

Object.keys(dependencies).forEach((depName) => {
const desiredVersion = dependencies[depName]
const regex = new RegExp(`"${depName}"\\s*:\\s*"[^"]+"`, "g")
updatedPackageJson = updatedPackageJson.replace(regex, `"${depName}": "${desiredVersion}"`)
})

return updatedPackageJson
}
31 changes: 31 additions & 0 deletions src/tools/expoGoCompatibility.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// The expoGoCompat object specifies version overrides for packages that are not
// compatible with the Expo Go app. We use this object to specify the latest compatible
// version of each package when generating a new project using Expo Go.
// New ignited apps using prebuild will have more up-to-date versions of these packages.
export const expoGoCompatExpectedVersions = {
"@react-native-async-storage/async-storage": "1.18.2",
"expo-application": "~5.3.0",
"expo-device": "~5.4.0",
"expo-file-system": "~15.4.4",
"expo-font": "~11.4.0",
"expo-localization": "~14.3.0",
}

// This function takes a package.json file as a string and updates the versions of the
// dependencies specified in expoGoCompatExpectedVersions to the values in that object
// and returns the updated package.json as a string.
// This function is used when generating a new project using Expo Go.
export function findAndUpdateDependencyVersions(
packageJsonRaw: string,
dependencies: Record<string, string>,
): string {
let updatedPackageJson = packageJsonRaw

Object.keys(dependencies).forEach((depName) => {
const desiredVersion = dependencies[depName]
const regex = new RegExp(`"${depName}"\\s*:\\s*"[^"]+"`, "g")
updatedPackageJson = updatedPackageJson.replace(regex, `"${depName}": "${desiredVersion}"`)
})

return updatedPackageJson
}
17 changes: 17 additions & 0 deletions test/vanilla/ignite-new.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { filesystem } from "gluegun"
import * as tempy from "tempy"
import { runIgnite, runError, run } from "../_test-helpers"
import { expoGoCompatExpectedVersions } from "../../src/tools/expoGoCompatibility"

const APP_NAME = "Foo"
const originalDir = process.cwd()
Expand Down Expand Up @@ -79,6 +80,14 @@ describe("ignite new", () => {
expect(igniteJSON.scripts.ios).toBe("npx expo start --ios")
})

it("should have modified the package.json to have versions that work with expo go", () => {
const igniteJSON = filesystem.read(`${appPath}/package.json`, "json")
expect(igniteJSON).toHaveProperty("dependencies")
Object.keys(expoGoCompatExpectedVersions).forEach((key) => {
expect(igniteJSON.dependencies[key]).toBe(expoGoCompatExpectedVersions[key])
})
})

it("should have created app.tsx with default export and RootStore", () => {
const appJS = filesystem.read(`${appPath}/app/app.tsx`)
expect(appJS).toContain("export default App")
Expand Down Expand Up @@ -316,6 +325,14 @@ describe("ignite new", () => {
expect(igniteJSON.scripts.android).toBe("npx expo run:android")
expect(igniteJSON.scripts.ios).toBe("npx expo run:ios")
})

it("should NOT have modified the package.json to have versions that work with expo go", () => {
const igniteJSON = filesystem.read(`${appPath}/package.json`, "json")
expect(igniteJSON).toHaveProperty("dependencies")
Object.keys(expoGoCompatExpectedVersions).forEach((key) => {
expect(igniteJSON.dependencies[key]).not.toBe(expoGoCompatExpectedVersions[key])
})
})
})
})

Expand Down
Loading

0 comments on commit 7bd6908

Please sign in to comment.