Skip to content

Commit 68400c5

Browse files
authored
don't test external early-access in unit tests (github#26612)
* don't test external early-access in unit tests * make the rendering tests less weird && gg push
1 parent 9a7776b commit 68400c5

File tree

3 files changed

+25
-51
lines changed

3 files changed

+25
-51
lines changed

middleware/contextualizers/early-access-links.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { uniq } from 'lodash-es'
22

33
export default function earlyAccessContext(req, res, next) {
4-
if (process.env.NODE_ENV === 'production') {
4+
if (process.env.NODE_ENV !== 'development') {
55
return next(404)
66
}
77

tests/rendering/server.js

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -596,46 +596,33 @@ describe('server', () => {
596596
})
597597

598598
describeViaActionsOnly('Early Access articles', () => {
599-
let hiddenPageHrefs, hiddenPages
600-
601-
beforeAll(async () => {
602-
const $ = await getDOM('/early-access')
603-
hiddenPageHrefs = $('#article-contents ul > li > a')
604-
.map((i, el) => $(el).attr('href'))
605-
.get()
606-
607-
const allPages = await loadPages()
608-
hiddenPages = allPages.filter((page) => page.languageCode === 'en' && page.hidden)
609-
})
610-
611-
test('exist in the set of English pages', async () => {
612-
expect(hiddenPages.length).toBeGreaterThan(0)
613-
})
614-
615-
test('are listed at /early-access', async () => {
616-
expect(hiddenPageHrefs.length).toBeGreaterThan(0)
617-
})
618-
619599
// Test skipped because this test file is no longer able to
620600
// change the `NODE_ENV` between tests because it depends on
621601
// HTTP and not raw supertest.
622602
// Idea: Move this one test somewhere into tests/unit/
623603
test.skip('are not listed at /early-access in production', async () => {
624-
const oldNodeEnv = process.env.NODE_ENV
625-
process.env.NODE_ENV = 'production'
626604
const res = await get('/early-access', { followRedirects: true })
627-
process.env.NODE_ENV = oldNodeEnv
628605
expect(res.statusCode).toBe(404)
629606
})
630607

631608
test('have noindex meta tags', async () => {
632-
const $ = await getDOM(hiddenPageHrefs[0])
633-
expect($('meta[content="noindex"]').length).toBe(1)
634-
})
635-
636-
test('public articles do not have noindex meta tags', async () => {
637-
const $ = await getDOM('/en/articles/set-up-git')
638-
expect($('meta[content="noindex"]').length).toBe(0)
609+
const allPages = await loadPages()
610+
// This is what the earlyAccessContext middleware does to get a
611+
// list of early-access pages for that TOC it displays when
612+
// viewing /en/early-access in development.
613+
// Here we're using it to get a least 1 page we can end-to-end
614+
// test to look at it's meta tags.
615+
const hiddenPages = allPages.filter(
616+
(page) =>
617+
page.languageCode === 'en' &&
618+
page.hidden &&
619+
page.relativePath.startsWith('early-access') &&
620+
!page.relativePath.endsWith('index.md')
621+
)
622+
for (const { href } of hiddenPages[0].permalinks) {
623+
const $ = await getDOM(href)
624+
expect($('meta[content="noindex"]').length).toBe(1)
625+
}
639626
})
640627
})
641628

tests/unit/early-access.js

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
import { jest } from '@jest/globals'
21
import { stat } from 'fs/promises'
32
import path from 'path'
4-
import { testViaActionsOnly } from '../helpers/conditional-runs.js'
5-
import { getDOM } from '../helpers/supertest.js'
6-
import got from 'got'
73

8-
jest.useFakeTimers('legacy')
4+
import { expect } from '@jest/globals'
5+
6+
import { testViaActionsOnly } from '../helpers/conditional-runs.js'
7+
import { get, getDOM } from '../helpers/e2etest.js'
98

109
describe('cloning early-access', () => {
1110
testViaActionsOnly('the content directory exists', async () => {
@@ -25,21 +24,9 @@ describe('cloning early-access', () => {
2524
})
2625

2726
describe('rendering early-access', () => {
28-
jest.setTimeout(5 * 60 * 1000)
29-
30-
testViaActionsOnly('the top-level TOC renders locally', async () => {
31-
const $ = await getDOM('/en/early-access')
32-
expect(
33-
$.html().includes('Hello, local developer! This page is not visible on production.')
34-
).toBe(true)
35-
expect($('ul a').length).toBeGreaterThan(5)
36-
})
37-
38-
testViaActionsOnly('the top-level TOC does not render on production', async () => {
39-
async function getEarlyAccess() {
40-
return await got('https://docs.github.com/en/early-access')
41-
}
42-
await expect(getEarlyAccess).rejects.toThrowError('Response code 404 (Not Found)')
27+
testViaActionsOnly('the top-level TOC is always 404', async () => {
28+
const res = await get('/en/early-access')
29+
expect(res.statusCode).toBe(404)
4330
})
4431

4532
testViaActionsOnly('TOCs display on category pages', async () => {

0 commit comments

Comments
 (0)