Skip to content
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

Support App.tsx files in expo-cli #206

Open
wants to merge 5 commits into
base: v52/next
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## [Unreleased]

### Fixed

- (expo-cli) Support App.txs files in `bugsnag-expo-cli` [#206](https://github.com/bugsnag/bugsnag-expo/pull/206)

## [52.0.0] - 2024-11-28

This release adds support for Expo 52
Expand Down
4 changes: 3 additions & 1 deletion packages/expo-cli/lib/insert.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ module.exports = async (projectRoot) => {

if (checkFileExists('App.ts')) {
return await writeBugsnagImport('App.ts')
} else if (checkFileExists('App.tsx')) {
return await writeBugsnagImport('App.tsx')
} else if (checkFileExists('App.js')) {
return await writeBugsnagImport('App.js')
} else {
throw new Error(`Couldn’t find App.js or App.ts in "${projectRoot}". Is this the root of your Expo project?`)
throw new Error(`Couldn’t find App.js or App.ts/tsx in "${projectRoot}". Is this the root of your Expo project?`)
}
}

Expand Down
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as below

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import Bugsnag from '@bugsnag/expo'
import React from 'react'
import { StyleSheet, Text, View } from 'react-native'

Bugsnag.start()

export default class App extends React.Component {
render () {
return (
<View style={styles.container}>
<Text>Hello Expo!</Text>
</View>
)
}
}

const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center'
}
})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a tsx file - is that correct?

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const Bugsnag = require('@bugsnag/expo')
const React = require('react')
const { StyleSheet, Text, View } = require('react-native')

Bugsnag.start()

export default class App extends React.Component {
render () {
return (
<View style={styles.container}>
<Text>Hello Expo!</Text>
</View>
)
}
}

const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center'
}
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import Bugsnag from '@bugsnag/expo'
import React from 'react'
import { StyleSheet, Text, View } from 'react-native'

Bugsnag.start()

export default class App extends React.Component {
render () {
return (
<View style={styles.container}>
<Text>Hello Expo!</Text>
</View>
)
}
}

const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center'
}
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"expo": {
"name": "Bugsnag test fixture",
"slug": "bugsnag-test-fixture",
"privacy": "unlisted",
"sdkVersion": "32.0.0",
"platforms": [
"ios",
"android"
],
"version": "1.0.0",
"orientation": "portrait",
"icon": "./assets/icon.png",
"splash": {
"image": "./assets/splash.png",
"resizeMode": "contain",
"backgroundColor": "#ffffff"
},
"updates": {
"fallbackToCacheTimeout": 0
},
"assetBundlePatterns": [
"**/*"
],
"extra": {
"bugsnag": {
"apiKey": "XoXoXoXoXoXo"
}
},
"hooks": {
"postPublish": [
{
"file": "@bugsnag/expo/hooks/post-publish.js",
"config": {}
}
]
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "already-configured-ts-import",
"private": "true",
"version": "0.0.0",
"dependencies": {
"@bugsnag/expo": "7.0.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const Bugsnag = require('@bugsnag/expo')
const React = require('react')
const { StyleSheet, Text, View } = require('react-native')

Bugsnag.start()

export default class App extends React.Component {
render () {
return (
<View style={styles.container}>
<Text>Hello Expo!</Text>
</View>
)
}
}

const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center'
}
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"expo": {
"name": "Bugsnag test fixture",
"slug": "bugsnag-test-fixture",
"privacy": "unlisted",
"sdkVersion": "32.0.0",
"platforms": [
"ios",
"android"
],
"version": "1.0.0",
"orientation": "portrait",
"icon": "./assets/icon.png",
"splash": {
"image": "./assets/splash.png",
"resizeMode": "contain",
"backgroundColor": "#ffffff"
},
"updates": {
"fallbackToCacheTimeout": 0
},
"assetBundlePatterns": [
"**/*"
],
"extra": {
"bugsnag": {
"apiKey": "XoXoXoXoXoXo"
}
},
"hooks": {
"postPublish": [
{
"file": "@bugsnag/expo/hooks/post-publish.js",
"config": {}
}
]
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "already-configured-ts-require",
"private": "true",
"version": "0.0.0",
"dependencies": {
"@bugsnag/expo": "7.0.0"
}
}
21 changes: 21 additions & 0 deletions packages/expo-cli/lib/test/fixtures/blank-tsx/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import React from 'react'
import { StyleSheet, Text, View } from 'react-native'

export default class App extends React.Component {
render () {
return (
<View style={styles.container}>
<Text>Hello Expo!</Text>
</View>
)
}
}

const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center'
}
})
26 changes: 26 additions & 0 deletions packages/expo-cli/lib/test/fixtures/blank-tsx/app.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"expo": {
"name": "Bugsnag test fixture",
"slug": "bugsnag-test-fixture",
"privacy": "unlisted",
"sdkVersion": "32.0.0",
"platforms": [
"ios",
"android"
],
"version": "1.0.0",
"orientation": "portrait",
"icon": "./assets/icon.png",
"splash": {
"image": "./assets/splash.png",
"resizeMode": "contain",
"backgroundColor": "#ffffff"
},
"updates": {
"fallbackToCacheTimeout": 0
},
"assetBundlePatterns": [
"**/*"
]
}
}
5 changes: 5 additions & 0 deletions packages/expo-cli/lib/test/fixtures/blank-tsx/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "bugsnag-test-fixture-blank-ts-and-js",
"private": "true",
"version": "0.0.0"
}
36 changes: 34 additions & 2 deletions packages/expo-cli/lib/test/insert.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ describe('expo-cli: insert', () => {
})
})

it('should work on a fresh .tsx project', async () => {
await withFixture('blank-tsx', async (projectRoot) => {
const msg = await insert(projectRoot)
expect(msg).toBe(undefined)

const appJs = await readFile(`${projectRoot}/App.tsx`, 'utf8')
expect(appJs).toMatch(/^import Bugsnag from '@bugsnag\/expo';\sBugsnag.start\(\);\s/)
})
})

it('shouldn’t insert if @bugsnag/expo is already imported (import, js)', async () => {
await withFixture('already-configured-js-import', async (projectRoot) => {
const appJsBefore = await readFile(`${projectRoot}/App.js`, 'utf8')
Expand Down Expand Up @@ -70,9 +80,31 @@ describe('expo-cli: insert', () => {
})
})

it('should provide a reasonable error when there is no App.js or App.ts', async () => {
it('shouldn’t insert if @bugsnag/expo is already imported (import, tsx)', async () => {
await withFixture('already-configured-tsx-import', async (projectRoot) => {
const appTsBefore = await readFile(`${projectRoot}/App.tsx`, 'utf8')
const msg = await insert(projectRoot)
expect(msg).toMatch(/already/)

const appTsAfter = await readFile(`${projectRoot}/App.tsx`, 'utf8')
expect(appTsAfter).toBe(appTsBefore)
})
})

it('shouldn’t insert if @bugsnag/expo is already imported (require, tsx)', async () => {
await withFixture('already-configured-tsx-require', async (projectRoot) => {
const appTsBefore = await readFile(`${projectRoot}/App.tsx`, 'utf8')
const msg = await insert(projectRoot)
expect(msg).toMatch(/already/)

const appTsAfter = await readFile(`${projectRoot}/App.tsx`, 'utf8')
expect(appTsAfter).toBe(appTsBefore)
})
})

it('should provide a reasonable error when there is no App.js or App.ts/tsx', async () => {
await withFixture('empty-00', async (projectRoot) => {
await expect(insert(projectRoot)).rejects.toThrow(/^Couldn’t find App\.js or App\.ts in/)
await expect(insert(projectRoot)).rejects.toThrow(/^Couldn’t find App\.js or App\.ts\/tsx in/)
})
})

Expand Down
Loading