Skip to content

Commit

Permalink
Add tests for self-importing packages (#68070)
Browse files Browse the repository at this point in the history
I've simplified @timneutkens's tests from #66486
The resolver fix is in vercel/turborepo#8820

Closes #66486
Related issue: #66487

---------

Co-authored-by: Tim Neutkens <tim@timneutkens.nl>
  • Loading branch information
mischnic and timneutkens committed Aug 7, 2024
1 parent 9568a4f commit b564855
Show file tree
Hide file tree
Showing 18 changed files with 149 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function Root({ children }) {
return (
<html>
<body>{children}</body>
</html>
)
}
10 changes: 10 additions & 0 deletions test/e2e/app-dir/self-importing-package-monorepo/app/page.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import value from 'internal-pkg'
import localValue from 'next-app'

export default function Home() {
return (
<h1>
Hello world {value} {localValue}
</h1>
)
}
1 change: 1 addition & 0 deletions test/e2e/app-dir/self-importing-package-monorepo/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'index'
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "internal-pkg",
"private": true,
"type": "module",
"exports": {
".": {
"default": "./src/index.js"
},
"./env": {
"default": "./src/env.js"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const env = 'test'
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { env } from 'internal-pkg/env'

export default `${env} abc`
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {}

module.exports = nextConfig
19 changes: 19 additions & 0 deletions test/e2e/app-dir/self-importing-package-monorepo/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "next-app",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint"
},
"dependencies": {
"internal-pkg": "link:./internal-pkg"
},
"exports": {
".": {
"default": "./index.js"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { nextTestSetup } from 'e2e-utils'

describe('self-importing-package-monorepo', () => {
const dependencies = (global as any).isNextDeploy
? // `link` is incompatible with the npm version used when this test is deployed
{
'internal-pkg': 'file:./internal-pkg',
}
: {
'internal-pkg': 'link:./internal-pkg',
}
const { next } = nextTestSetup({
files: __dirname,
dependencies,
packageJson: {
name: 'next-app',
exports: {
'.': {
default: './index.js',
},
},
},
})

it('should resolve self-imports inside a monorepo', async () => {
const $ = await next.render$('/')
expect($('h1').text()).toBe('Hello world test abc index')
})
})
7 changes: 7 additions & 0 deletions test/e2e/app-dir/self-importing-package/app/layout.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function Root({ children }) {
return (
<html>
<body>{children}</body>
</html>
)
}
5 changes: 5 additions & 0 deletions test/e2e/app-dir/self-importing-package/app/page.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import myMessage from 'internal-pkg'

export default function Page() {
return <h1>{myMessage}</h1>
}
Binary file not shown.
13 changes: 13 additions & 0 deletions test/e2e/app-dir/self-importing-package/internal-pkg/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "internal-pkg",
"private": true,
"type": "module",
"exports": {
".": {
"default": "./src/index.js"
},
"./env": {
"default": "./src/env.js"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const env = 'test'
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { env } from 'internal-pkg/env'

export default `${env} abc`
6 changes: 6 additions & 0 deletions test/e2e/app-dir/self-importing-package/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {}

module.exports = nextConfig
6 changes: 6 additions & 0 deletions test/e2e/app-dir/self-importing-package/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"private": true,
"dependencies": {
"my-package": "file:./my-package.tar"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { nextTestSetup } from 'e2e-utils'
import path from 'path'

describe('self-importing-package', () => {
const { next } = nextTestSetup({
files: __dirname,
// This test is skipped when deployed because the local tarball appears corrupted
// It also doesn't seem particularly useful to test when deployed
skipDeployment: true,
dependencies: {
'internal-pkg': `file:${path.join(__dirname, 'internal-pkg.tar')}`,
},
})

it('should resolve self-imports in an external package', async () => {
const $ = await next.render$('/')
expect($('h1').text()).toBe('test abc')
})
})

0 comments on commit b564855

Please sign in to comment.