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

Add tests for self-importing packages #66486

Closed
Closed
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "@repo/internal-pkg",
"private": true,
"type": "module",
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.js",
"require": "./dist/index.cjs"
},
"./env": {
"types": "./dist/env.d.ts",
"import": "./dist/env.js",
"require": "./dist/env.cjs"
}
},
"scripts": {
"build": "bunchee",
"typecheck": "tsc --noEmit"
},
"dependencies": {
"bunchee": "^5.1.5",
"typescript": "5.4.2"
}
}
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 './env'

export default `${env} abc`
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type { Metadata } from 'next'

export const metadata: Metadata = {
title: 'Create Next App',
description: 'Generated by create next app',
}

export default function RootLayout({
children,
}: Readonly<{
children: React.ReactNode
}>) {
return (
<html lang="en">
<body>{children}</body>
</html>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import value from '@repo/internal-pkg'

export default function Home() {
return <h1>Hello world {value}</h1>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "next-app",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint"
},
"dependencies": {
"@repo/internal-pkg": "workspace:*",
"next": "15.0.0-canary.2",
"react": "19.0.0-rc-f994737d14-20240522",
"react-dom": "19.0.0-rc-f994737d14-20240522"
},
"devDependencies": {
"@types/node": "^20",
"@types/react": "^18",
"@types/react-dom": "^18",
"typescript": "^5"
}
}
7 changes: 7 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,7 @@
{
"name": "turbo-self-referencing",
"private": true,
"devDependencies": {
"@types/node": "^20"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
packages:
- 'next-app'
- 'internal-pkg'
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { nextTestSetup } from 'e2e-utils'

describe('self-importing-package-monorepo', () => {
const { next } = nextTestSetup({
files: __dirname,
buildCommand: 'pnpm run build',
startCommand: 'pnpm run start',
devCommand: 'pnpm run dev',
packageJson: {
scripts: {
build:
'pnpm install && pnpm --filter=internal-pkg run build && pnpm --filter=next-app run build',
dev: 'pnpm install && pnpm --filter=internal-pkg run build && pnpm --filter=next-app run dev',
start: 'pnpm --filter=next-app run start',
},
},
})

it('should work using cheerio', async () => {
const $ = await next.render$('/')
expect($('h1').text()).toBe('Hello world test abc')
})
})
7 changes: 7 additions & 0 deletions test/e2e/app-dir/self-importing-package/app/layout.js
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>
)
}
6 changes: 6 additions & 0 deletions test/e2e/app-dir/self-importing-package/app/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { myMessage } from 'my-package'
import '@repo/internal-pkg'

export default function Page() {
return <h1>{myMessage}</h1>
}
Binary file not shown.
36 changes: 36 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,36 @@
{
"name": "@repo/internal-pkg",
"private": true,
"type": "module",
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.js",
"require": "./dist/index.cjs"
},
"./env": {
"types": "./dist/env.d.ts",
"import": "./dist/env.js",
"require": "./dist/env.cjs"
}
},
"scripts": {
"build": "bunchee",
"dev": "bunchee -w --no-clean",
"clean": "rm -rf .turbo dist node_modules",
"lint": "eslint src",
"typecheck": "tsc --noEmit"
},
"dependencies": {
"@aws-sdk/client-s3": "3.572.0",
"@aws-sdk/s3-presigned-post": "3.572.0",
"@aws-sdk/s3-request-presigner": "3.572.0",
"@t3-oss/env-core": "^0.9.2",
"zod": "^3.22.4"
},
"devDependencies": {
"bunchee": "^5.1.5",
"eslint": "^8.57.0",
"typescript": "5.4.2"
}
}
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 './env'

console.log({ env })
Binary file not shown.
1 change: 1 addition & 0 deletions test/e2e/app-dir/self-importing-package/my-package/env.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const message = 'THIS IS THE OTHER FILE'
3 changes: 3 additions & 0 deletions test/e2e/app-dir/self-importing-package/my-package/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { message } from 'my-package/env'

export const myMessage = message
12 changes: 12 additions & 0 deletions test/e2e/app-dir/self-importing-package/my-package/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "my-package",
"type": "module",
"exports": {
".": {
"import": "./index.js"
},
"./env": {
"import": "./env.js"
}
}
}
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,17 @@
import { nextTestSetup } from 'e2e-utils'
import path from 'path'

describe('self-importing-package', () => {
const { next } = nextTestSetup({
files: __dirname,
dependencies: {
'my-package': `file:${path.join(__dirname, 'my-package.tar')}`,
'@repo/internal-pkg': `file:${path.join(__dirname, 'internal-pkg.tar')}`,
},
})

it('should work using cheerio', async () => {
const $ = await next.render$('/')
expect($('h1').text()).toBe('THIS IS THE OTHER FILE')
})
})
1 change: 1 addition & 0 deletions test/lib/create-next-install.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ async function createNextInstall({
{
...packageJson,
dependencies: combinedDependencies,
overrides: combinedDependencies,
private: true,
// Add resolutions if provided.
...(resolutions ? { resolutions } : {}),
Expand Down
16 changes: 10 additions & 6 deletions test/lib/next-modes/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export interface NextInstanceOpts {
installCommand?: InstallCommand
buildCommand?: string
startCommand?: string
devCommand?: string
env?: Record<string, string>
dirSuffix?: string
turbo?: boolean
Expand All @@ -54,6 +55,7 @@ export class NextInstance {
protected installCommand?: InstallCommand
protected buildCommand?: string
protected startCommand?: string
protected devCommand?: string
protected dependencies?: PackageJson['dependencies'] = {}
protected resolutions?: PackageJson['resolutions']
protected events: { [eventName: string]: Set<any> } = {}
Expand Down Expand Up @@ -176,17 +178,19 @@ export class NextInstance {
if (skipInstall || skipIsolatedNext) {
const pkgScripts = (this.packageJson['scripts'] as {}) || {}
await fs.mkdir(this.testDir, { recursive: true })
const dependencies = {
...finalDependencies,
next:
process.env.NEXT_TEST_VERSION ||
require('next/package.json').version,
}
await fs.writeFile(
path.join(this.testDir, 'package.json'),
JSON.stringify(
{
...this.packageJson,
dependencies: {
...finalDependencies,
next:
process.env.NEXT_TEST_VERSION ||
require('next/package.json').version,
},
dependencies,
overrides: dependencies,
...(this.resolutions ? { resolutions: this.resolutions } : {}),
scripts: {
// since we can't get the build id as a build artifact, make it
Expand Down
4 changes: 3 additions & 1 deletion test/lib/next-modes/next-dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ export class NextDevInstance extends NextInstance {
useDirArg && this.testDir,
].filter(Boolean) as string[]

if (this.startCommand) {
if (this.devCommand) {
startArgs = this.devCommand.split(' ')
} else if (this.startCommand) {
startArgs = this.startCommand.split(' ')
}

Expand Down
Loading