1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00
next.js/examples/with-apollo-auth/test/shared-apollo.js

42 lines
1.4 KiB
JavaScript
Raw Normal View History

const clearRequire = require('clear-require')
const glob = require('glob')
const test = require('ava')
/**
* Motivations:
*
* - Client-side getInitialProps() wont have access to the apollo client for
* that page (because it's not shared across page bundles), so wont be able to
* reset the state, leaving all the logged in user data there :(
* - So, we have to have a shared module. BUT; next's code splitting means the
* bundle for each page will include its own copy of the module, _unless it's
* inlcuded in every page_.
* - https://github.com/zeit/next.js/issues/659#issuecomment-271824223
* - https://github.com/zeit/next.js/issues/1635#issuecomment-292236785
* - Therefore, this test ensures that every page includes that module, and
* hence it will be shared across every page, giving us a global store in
* Apollo that we can clear, etc
*/
const apolloFilePath = require.resolve('../lib/initApollo')
test.beforeEach(() => {
// Clean up the cache
clearRequire.all()
})
glob.sync('./pages/**/*.js').forEach((file) => {
test(`.${file} imports shared apollo module`, (t) => {
t.falsy(require.cache[apolloFilePath])
try {
require(`.${file}`)
} catch (error) {
// Don't really care if it fails to execute, etc, just want to be
// certain the expected require call was made
}
t.truthy(require.cache[apolloFilePath])
})
})