Skip to content

Commit 775e414

Browse files
authored
fix(supertokens): Fix whitespace handling in setup script (#158)
I was upgrading to Vitest 3.x and noticed one of our snapshot tests started failing. Looking into it I realized we weren't handling whitespace properly when setting up Supertokens auth. This PR fixes that. I'm guessing this is the Vitest change that caused the diff in snapshots: vitest-dev/vitest#7400 It doesn't say in the PR description, but the [release notes for Vitest 3.1.0](https://github.com/vitest-dev/vitest/releases/tag/v3.1.0), where it was first included, says > This change can cause small amount of very old snapshots to be updated, but there will be no functional change to how they work.
1 parent aea5f05 commit 775e414

File tree

2 files changed

+24
-30
lines changed

2 files changed

+24
-30
lines changed

packages/auth-providers/supertokens/setup/src/__tests__/setupHandler.test.ts

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ describe('addRoutingLogic', () => {
3535
it('modifies the Routes.{jsx,tsx} file', () => {
3636
vol.fromJSON({
3737
'Routes.tsx':
38-
"// In this file, all Page components from 'src/pages' are auto-imported.\n" +
38+
"// In this file, all Page components from 'src/pages' are auto-imported." +
3939
`
4040
import { Router, Route } from '@cedarjs/router'
4141
@@ -58,37 +58,35 @@ export default Routes
5858
addRoutingLogic.task()
5959

6060
expect(fs.readFileSync('Routes.tsx', 'utf-8')).toMatchInlineSnapshot(`
61-
"// In this file, all Page components from 'src/pages' are auto-imported.
62-
63-
import { canHandleRoute, getRoutingComponent } from 'supertokens-auth-react/ui'
64-
65-
import { Router, Route } from '@cedarjs/router'
61+
"// In this file, all Page components from 'src/pages' are auto-imported.
62+
import { canHandleRoute, getRoutingComponent } from 'supertokens-auth-react/ui'
6663
67-
import { useAuth, PreBuiltUI } from './auth'
64+
import { Router, Route } from '@cedarjs/router'
6865
69-
const Routes = () => {
70-
if (canHandleRoute(PreBuiltUI)) {
71-
return getRoutingComponent(PreBuiltUI)
72-
}
66+
import { useAuth, PreBuiltUI } from './auth'
7367
74-
return (
75-
<Router useAuth={useAuth}>
76-
<Route path="/login" page={LoginPage} name="login" />
77-
<Route path="/signup" page={SignupPage} name="signup" />
78-
<Route notfound page={NotFoundPage} />
79-
</Router>
80-
)
81-
}
68+
const Routes = () => {
69+
if (canHandleRoute(PreBuiltUI)) {
70+
return getRoutingComponent(PreBuiltUI)
71+
}
72+
return (
73+
<Router useAuth={useAuth}>
74+
<Route path="/login" page={LoginPage} name="login" />
75+
<Route path="/signup" page={SignupPage} name="signup" />
76+
<Route notfound page={NotFoundPage} />
77+
</Router>
78+
)
79+
}
8280
83-
export default Routes
84-
"
85-
`)
81+
export default Routes
82+
"
83+
`)
8684
})
8785

8886
it('handles a Routes.{jsx,tsx} file with a legacy setup', () => {
8987
vol.fromJSON({
9088
'Routes.tsx':
91-
"// In this file, all Page components from 'src/pages' are auto-imported.\n" +
89+
"// In this file, all Page components from 'src/pages' are auto-imported." +
9290
`
9391
import SuperTokens from 'supertokens-auth-react'
9492
@@ -119,8 +117,6 @@ export default Routes
119117
expect(fs.readFileSync('Routes.tsx', 'utf-8')).toMatchInlineSnapshot(`
120118
"// In this file, all Page components from 'src/pages' are auto-imported.
121119
122-
123-
124120
import { canHandleRoute, getRoutingComponent } from 'supertokens-auth-react/ui'
125121
126122
import { Router, Route } from '@cedarjs/router'
@@ -132,8 +128,6 @@ export default Routes
132128
return getRoutingComponent(PreBuiltUI)
133129
}
134130
135-
136-
137131
return (
138132
<Router useAuth={useAuth}>
139133
<Route path="/login" page={LoginPage} name="login" />

packages/auth-providers/supertokens/setup/src/setupHandler.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ export const addRoutingLogic = {
4646

4747
// Remove the old setup if it's there.
4848
content = content
49-
.replace("import SuperTokens from 'supertokens-auth-react'", '')
50-
.replace(/if \(SuperTokens.canHandleRoute\(\)\) {[^}]+}/, '')
49+
.replace("import SuperTokens from 'supertokens-auth-react'\n", '')
50+
.replace(/[ \t]*if \(SuperTokens.canHandleRoute\(\)\) {[^}]+}\n/, '')
5151

5252
if (!/\s*if\s*\(canHandleRoute\(PreBuiltUI\)\)\s*\{/.test(content)) {
5353
let hasImportedSuperTokensFunctions = false
@@ -84,7 +84,7 @@ export const addRoutingLogic = {
8484
'const Routes = () => {\n' +
8585
' if (canHandleRoute(PreBuiltUI)) {\n' +
8686
' return getRoutingComponent(PreBuiltUI)\n' +
87-
' }\n\n',
87+
' }\n',
8888
)
8989

9090
fs.writeFileSync(routesPath, content)

0 commit comments

Comments
 (0)