Skip to content

Commit

Permalink
Merge branch 'canary' into strict-linting
Browse files Browse the repository at this point in the history
  • Loading branch information
Timer committed Nov 15, 2019
2 parents 9a5c9eb + 332852b commit 995ee6c
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 2 deletions.
File renamed without changes.
5 changes: 5 additions & 0 deletions packages/next/build/polyfills/fetch/whatwg-fetch.js
@@ -0,0 +1,5 @@
/* globals self */
exports.Headers = self.Headers
exports.Request = self.Request
exports.Response = self.Response
exports.fetch = self.fetch
9 changes: 7 additions & 2 deletions packages/next/build/webpack-config.ts
Expand Up @@ -59,7 +59,7 @@ function getOptimizedAliases(isServer: boolean): { [pkg: string]: string } {
return {}
}

const stubWindowFetch = path.join(__dirname, 'polyfills', 'fetch.js')
const stubWindowFetch = path.join(__dirname, 'polyfills', 'fetch', 'index.js')
const stubObjectAssign = path.join(__dirname, 'polyfills', 'object-assign.js')

const shimAssign = path.join(__dirname, 'polyfills', 'object.assign')
Expand All @@ -68,7 +68,12 @@ function getOptimizedAliases(isServer: boolean): { [pkg: string]: string } {
__next_polyfill__fetch: require.resolve('whatwg-fetch'),
unfetch$: stubWindowFetch,
'isomorphic-unfetch$': stubWindowFetch,
'whatwg-fetch$': stubWindowFetch,
'whatwg-fetch$': path.join(
__dirname,
'polyfills',
'fetch',
'whatwg-fetch.js'
),

// Polyfill: Object.assign
__next_polyfill__object_assign: require.resolve('object-assign'),
Expand Down
47 changes: 47 additions & 0 deletions test/integration/polyfills/pages/fetch.js
@@ -0,0 +1,47 @@
import { useState, useEffect } from 'react'
import unfetchImp from 'unfetch'
import isomorphicUnfetchImp from 'isomorphic-unfetch'

const testWhatwgFetchMethods = whatWgFetch => {
return (
whatWgFetch.Headers.name === 'Headers' &&
whatWgFetch.Request.name === 'Request' &&
whatWgFetch.Response.name === 'Response'
)
}

const testFetchImports = async () => {
const whatwgFetchImp = await import('whatwg-fetch')
const whatwgFetchReq = require('whatwg-fetch')
const unfetchReq = require('unfetch')
const isomorphicUnfetchReq = require('isomorphic-unfetch')

let areImportsMatching =
[whatwgFetchImp.fetch, whatwgFetchReq.fetch].every(
lib => lib.name === 'fetch'
) &&
[unfetchImp, unfetchReq, isomorphicUnfetchImp, isomorphicUnfetchReq].every(
lib => lib.name === 'bound fetch'
)

return areImportsMatching &&
testWhatwgFetchMethods(whatwgFetchReq) &&
testWhatwgFetchMethods(whatwgFetchImp)
? 'pass'
: 'fail'
}

const Page = () => {
const [testStatus, setTestStatus] = useState('computing')

useEffect(() => {
testFetchImports().then(status => {
console.log(status)
setTestStatus(status)
})
}, [])

return <div id="test-status">{testStatus}</div>
}

export default Page
7 changes: 7 additions & 0 deletions test/integration/polyfills/pages/index.js
@@ -0,0 +1,7 @@
import Link from 'next/link'

export default () => (
<div>
<Link href="/fetch">Fetch</Link>
</div>
)
44 changes: 44 additions & 0 deletions test/integration/polyfills/test/index.test.js
@@ -0,0 +1,44 @@
/* eslint-env jest */
/* global jasmine */
import { join } from 'path'
import {
nextBuild,
findPort,
waitFor,
nextStart,
killApp,
} from 'next-test-utils'
import webdriver from 'next-webdriver'

jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 1

const appDir = join(__dirname, '../')

let appPort
let app

describe('Polyfills', () => {
beforeAll(async () => {
const { stdout, stderr } = await nextBuild(appDir, [], {
stdout: true,
stderr: true,
})
console.log(stdout)
console.error(stderr)
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(async () => {
await killApp(app)
})

it('should alias fetch', async () => {
const browser = await webdriver(appPort, '/fetch')
await waitFor(1000)
const text = await browser.elementByCss('#test-status').text()

expect(text).toBe('pass')

await browser.close()
})
})

0 comments on commit 995ee6c

Please sign in to comment.