Skip to content

Commit 5aa54b3

Browse files
timneutkensijjk
andauthored
Add pagesDir to Jest transformer (#36599)
Fixes #35469 Adds `pagesDir` which is required for the Relay transform. Added a test case based on https://github.com/hanford/relay-swc-jest. ## Bug - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Errors have helpful link attached, see `contributing.md` ## Feature - [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. - [ ] Errors have helpful link attached, see `contributing.md` ## Documentation / Examples - [ ] Make sure the linting passes by running `yarn lint` Co-authored-by: JJ Kasper <[email protected]>
1 parent bd3dfe1 commit 5aa54b3

File tree

14 files changed

+344
-1
lines changed

14 files changed

+344
-1
lines changed

packages/next/build/jest/jest.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import loadConfig from '../../server/config'
44
import { PHASE_TEST } from '../../shared/lib/constants'
55
import loadJsConfig from '../load-jsconfig'
66
import * as Log from '../output/log'
7+
import { findPagesDir } from '../../lib/find-pages-dir'
78

89
async function getConfig(dir: string) {
910
const conf = await loadConfig(PHASE_TEST, dir)
@@ -50,8 +51,11 @@ export default function nextJest(options: { dir?: string } = {}) {
5051
let jsConfig
5152
let resolvedBaseUrl
5253
let isEsmProject = false
54+
let pagesDir
55+
5356
if (options.dir) {
5457
const resolvedDir = resolve(options.dir)
58+
pagesDir = findPagesDir(resolvedDir)
5559
const packageConfig = loadClosestPackageJson(resolvedDir)
5660
isEsmProject = packageConfig.type === 'module'
5761

@@ -108,6 +112,7 @@ export default function nextJest(options: { dir?: string } = {}) {
108112
jsConfig,
109113
resolvedBaseUrl,
110114
isEsmProject,
115+
pagesDir,
111116
},
112117
],
113118
// Allow for appending/overriding the default transforms

packages/next/build/swc/jest-transformer.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ module.exports = {
4747
nextConfig: inputOptions.nextConfig,
4848
jsConfig: inputOptions.jsConfig,
4949
resolvedBaseUrl: inputOptions.resolvedBaseUrl,
50+
pagesDir: inputOptions.pagesDir,
5051
esm:
5152
isSupportEsm &&
5253
isEsm(Boolean(inputOptions.isEsmProject), filename, jestConfig),

packages/next/build/swc/options.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ export function getJestSWCOptions({
145145
esm,
146146
nextConfig,
147147
jsConfig,
148+
pagesDir,
148149
// This is not passed yet as "paths" resolving needs a test first
149150
// resolvedBaseUrl,
150151
}) {
@@ -174,6 +175,7 @@ export function getJestSWCOptions({
174175
},
175176
disableNextSsg: true,
176177
disablePageConfig: true,
178+
pagesDir,
177179
}
178180
}
179181

test/lib/e2e-utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { NextStartInstance } from './next-modes/next-start'
77
import { NextDeployInstance } from './next-modes/next-deploy'
88

99
// increase timeout to account for yarn install time
10-
jest.setTimeout((process.platform === 'win32' ? 240 : 180) * 1000)
10+
jest.setTimeout(240 * 1000)
1111

1212
const testsFolder = path.join(__dirname, '..')
1313

test/production/next/jest/index.test.ts renamed to test/production/jest/index.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ import { renderViaHTTP } from 'next-test-utils'
55
describe('next/jest', () => {
66
let next: NextInstance
77

8+
if (process.env.NEXT_TEST_REACT_VERSION === '^17') {
9+
// react testing library is specific to react version
10+
it('should bail on react v17', () => {})
11+
return
12+
}
13+
814
beforeAll(async () => {
915
next = await createNext({
1016
files: {
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { Environment, Network, RecordSource, Store } from 'relay-runtime'
2+
3+
async function fetchGraphQL(text, variables) {
4+
return new Promise((next) => {
5+
const res = () => {
6+
return next({ data: { viewer: { user: { id: '123', name: 'Foo' } } } })
7+
}
8+
9+
setTimeout(res, 1000)
10+
})
11+
}
12+
13+
// Relay passes a "params" object with the query name and text. So we define a helper function
14+
// to call our fetchGraphQL utility with params.text.
15+
async function fetchRelay(params, variables) {
16+
return await fetchGraphQL(params.text, variables)
17+
}
18+
19+
// Export a singleton instance of Relay Environment configured with our network function:
20+
export default new Environment({
21+
network: Network.create(fetchRelay),
22+
store: new Store(new RecordSource()),
23+
})
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const nextJest = require('next/jest')
2+
3+
const createJestConfig = nextJest({
4+
// Provide the path to your Next.js app to load next.config.js and .env files in your test environment
5+
dir: './',
6+
})
7+
8+
// Add any custom config to be passed to Jest
9+
const customJestConfig = {
10+
// if using TypeScript with a baseUrl set to the root directory then you need the below for alias' to work
11+
moduleDirectories: ['node_modules', '<rootDir>/'],
12+
testEnvironment: 'jest-environment-jsdom',
13+
moduleNameMapper: {
14+
// When changing these, also look at the tsconfig!
15+
'^types/(.+)$': '<rootDir>/types/$1',
16+
},
17+
}
18+
19+
// createJestConfig is exported this way to ensure that next/jest can load the Next.js config which is async
20+
module.exports = createJestConfig(customJestConfig)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
type Query {
2+
viewer(after: String, before: String, first: Int, last: Int): Viewer!
3+
node(id: ID!): Node
4+
}
5+
6+
type Viewer implements Node {
7+
id: ID!
8+
user: User!
9+
}
10+
11+
interface Node {
12+
id: ID!
13+
}
14+
15+
type User implements Node {
16+
id: ID!
17+
name: String!
18+
title: String!
19+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module.exports = {
2+
compiler: {
3+
relay: {
4+
src: './',
5+
artifactDirectory: './types',
6+
language: 'typescript',
7+
},
8+
},
9+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { RelayEnvironmentProvider } from 'react-relay/hooks'
2+
import RelayEnvironment from '../components/environment'
3+
4+
export default function MyApp({ Component, pageProps }) {
5+
return (
6+
<RelayEnvironmentProvider environment={RelayEnvironment}>
7+
<Component {...pageProps} />
8+
</RelayEnvironmentProvider>
9+
)
10+
}

0 commit comments

Comments
 (0)