Skip to content

Commit 10e0e80

Browse files
committed
Clean up globals
- Import globals from various files instead of manually adding them to ensure they are availability in Jest and eslint configurations - Add middleware to server configuration for serving virtual “_blank.html” file - Add BLANK_URL - Rename TEST_URL to TEST_HOST - Removed ./test/fixtures/ directory (blank page now served via server.js middleware) - Added page.goto(BLANK_URL) call to global Playwright beforeEach() setup
1 parent e4887ae commit 10e0e80

File tree

9 files changed

+61
-50
lines changed

9 files changed

+61
-50
lines changed

jest.config.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
const path = require('path');
2-
const { TEST_URL, DOCS_URL, LIB_URL } = require('./test/config/server.js');
2+
const { globals: serverGlobals } = require('./test/config/server.js');
33

44
const sharedConfig = {
55
errorOnDeprecated: true,
66
globals: {
7+
...serverGlobals, // BLANK_URL, DOCS_URL, LIB_URL, TEST_HOST
78
DOCS_PATH: path.resolve(__dirname, 'docs'),
8-
DOCS_URL,
99
LIB_PATH: path.resolve(__dirname, 'lib'),
10-
LIB_URL,
1110
SRC_PATH: path.resolve(__dirname, 'src'),
12-
TEST_URL,
1311
},
1412
globalSetup: './test/config/jest.setup.js',
1513
globalTeardown: './test/config/jest.teardown.js',
@@ -21,22 +19,26 @@ const sharedConfig = {
2119
process.env.JEST_PLAYWRIGHT_CONFIG = './test/config/jest-playwright.config.js';
2220

2321
module.exports = {
22+
// Adding globals to config root for easier importing into .eslint.js, but
23+
// as of Jest 26.4.2 these globals need to be added to each project config
24+
// as well.
25+
globals: sharedConfig.globals,
2426
projects: [
2527
// Unit Tests (Jest)
2628
{
2729
...sharedConfig,
2830
displayName: 'unit',
2931
setupFilesAfterEnv: ['<rootDir>/test/config/jest.setup-tests.js'],
3032
testMatch: ['<rootDir>/test/unit/*.test.js'],
31-
testURL: TEST_URL,
33+
testURL: serverGlobals.BLANK_URL,
3234
},
3335
// Integration Tests (Jest)
3436
{
3537
...sharedConfig,
3638
displayName: 'integration',
3739
setupFilesAfterEnv: ['<rootDir>/test/config/jest.setup-tests.js'],
3840
testMatch: ['<rootDir>/test/integration/*.test.js'],
39-
testURL: TEST_URL,
41+
testURL: serverGlobals.BLANK_URL,
4042
},
4143
// E2E Tests (Jest + Playwright)
4244
{

test/.eslintrc.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1+
const jestConfig = require('../jest.config.js');
2+
13
module.exports = {
24
env: {
35
'jest/globals': true,
46
},
57
extends: ['plugin:jest/recommended', 'plugin:jest/style'],
68
globals: {
7-
TEST_URL: 'readonly',
8-
DOCS_PATH: 'readonly',
9-
DOCS_URL: 'readonly',
10-
LIB_PATH: 'readonly',
11-
LIB_URL: 'readonly',
12-
SRC_PATH: 'readonly',
9+
...jestConfig.globals,
1310
},
1411
plugins: ['jest'],
1512
};

test/README.md

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,18 @@
1414

1515
### Global Variables
1616

17-
- `TEST_URL`: Test server base URL (http://localhost:3001 => `/test/fixtures/`)
18-
- `DOCS_PATH`: File system path to docsify's `/docs` directory
17+
#### File paths
18+
19+
- `DOCS_PATH`: File path to docsify's `/docs` directory
20+
- `LIB_PATH`: File path to docsify's `/lib` directory
21+
- `SRC_PATH`: File path to docsify's `/src` directory
22+
23+
#### URLs
24+
25+
- `BLANK_URL`: Test server route to virtual `_blank.html` file
1926
- `DOCS_URL`: Test server route to docsify's `/docs` directory
20-
- `LIB_PATH`: File system path to docsify's `/lib` directory
2127
- `LIB_URL`: Test server route to docsify's `/lib` directory
22-
- `SRC_PATH`: File system path to docsify's `/src` directory
28+
- `TEST_HOST`: Test server host:port (`http://127.0.0.1:3001`)
2329

2430
### CLI commands
2531

@@ -32,9 +38,6 @@ npm run test:e2e
3238
npm run test:integration
3339
npm run test:unit
3440

35-
# Run example tests
36-
npm run test:examples
37-
3841
# Run test file
3942
npm run test -- -i /path/to/file.test.js
4043

test/config/jest-playwright.setup-tests.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* global browserName */
1+
/* global browserName page */
22
const { configureToMatchImageSnapshot } = require('jest-image-snapshot');
33

44
// Lifecycle Hooks
@@ -42,4 +42,13 @@ beforeAll(async () => {
4242

4343
beforeEach(async () => {
4444
await global.jestPlaywright.resetPage();
45+
46+
// Goto URL ()
47+
// https://playwright.dev/#path=docs%2Fapi.md&q=pagegotourl-options
48+
// NOTE: Tests typically begin by navigating to a page for testing. When
49+
// this doesn't happen, Playwright operates on the "about:blank" page which
50+
// will cause operations that require the window location to be a valid URL
51+
// to fail (e.g. AJAX requests). To avoid these issues, this hook ensures
52+
// that each tests begins by a blank HTML page.
53+
await page.goto(BLANK_URL);
4554
});

test/config/server.js

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@ const serverConfig = {
1111
function startServer(options = {}, cb = Function.prototype) {
1212
const defaults = {
1313
...serverConfig,
14+
middleware: [
15+
{
16+
route: '/_blank.html',
17+
handle: function(req, res, next) {
18+
res.setHeader('Content-Type', 'text/html');
19+
res.end('');
20+
next();
21+
},
22+
},
23+
],
1424
notify: false,
1525
open: false,
1626
rewriteRules: [
@@ -21,7 +31,7 @@ function startServer(options = {}, cb = Function.prototype) {
2131
},
2232
],
2333
server: {
24-
baseDir: path.resolve(__dirname, '../fixtures'),
34+
baseDir: path.resolve(__dirname, '../'),
2535
routes: {
2636
'/docs': path.resolve(__dirname, '../../docs'),
2737
'/lib': path.resolve(__dirname, '../../lib'),
@@ -87,15 +97,19 @@ if (hasStartArg) {
8797
}
8898

8999
module.exports = {
100+
globals: {
101+
get BLANK_URL() {
102+
return `${this.TEST_HOST}/_blank.html`;
103+
},
104+
get DOCS_URL() {
105+
return `${this.TEST_HOST}/docs`;
106+
},
107+
get LIB_URL() {
108+
return `${this.TEST_HOST}/lib`;
109+
},
110+
TEST_HOST: `http://${serverConfig.host}:${serverConfig.port}`,
111+
},
90112
start: startServer,
91113
startAsync: startServerAsync,
92114
stop: stopServer,
93-
// Jest Globals
94-
TEST_URL: `http://${serverConfig.host}:${serverConfig.port}`,
95-
get DOCS_URL() {
96-
return `${this.TEST_URL}/docs`;
97-
},
98-
get LIB_URL() {
99-
return `${this.TEST_URL}/lib`;
100-
},
101115
};

test/e2e/example.test.js

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,6 @@ const docsifyInit = require('../helpers/docsify-init');
55
// Suite
66
// -----------------------------------------------------------------------------
77
describe(`Example Tests`, function() {
8-
// Setup & Teardown
9-
// ---------------------------------------------------------------------------
10-
beforeEach(async () => {
11-
// Goto URL (fixtures/index.html)
12-
// https://playwright.dev/#path=docs%2Fapi.md&q=pagegotourl-options
13-
// NOTE: Tests typically begin by navigating to a page for testing. When
14-
// this doesn't happen, Playwright operates on the "about:blank" page which
15-
// will cause operations that require the window location to be a valid URL
16-
// to fail (e.g. AJAX requests). To avoid these issues, this hook ensures
17-
// that each tests begins by loading the default test server page.
18-
await page.goto(TEST_URL);
19-
});
20-
218
// Tests
229
// ---------------------------------------------------------------------------
2310
test('dom manipulation', async () => {
@@ -105,7 +92,7 @@ describe(`Example Tests`, function() {
10592
test('manual docsify site using playwright methods', async () => {
10693
// Goto URL
10794
// https://playwright.dev/#path=docs%2Fapi.md&q=pagegotourl-options
108-
await page.goto(TEST_URL);
95+
await page.goto(BLANK_URL);
10996

11097
// Set docsify configuration
11198
// https://playwright.dev/#path=docs%2Fapi.md&q=pageevaluatepagefunction-arg

test/fixtures/index.html

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/helpers/docsify-init.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ const isPlaywright = 'page' in global;
3737
async function docsifyInit(options = {}) {
3838
const defaults = {
3939
config: {
40-
basePath: TEST_URL,
40+
basePath: TEST_HOST,
4141
el: '#app',
4242
},
4343
html: `
@@ -62,7 +62,7 @@ async function docsifyInit(options = {}) {
6262
scriptURLs: [],
6363
style: '',
6464
styleURLs: [],
65-
testURL: `${TEST_URL}/docsify-init.html`,
65+
testURL: `${TEST_HOST}/docsify-init.html`,
6666
waitForSelector: '#main',
6767
};
6868
const settings = {
@@ -116,7 +116,7 @@ async function docsifyInit(options = {}) {
116116
.filter(([url, responseText]) => url && responseText)
117117
.map(([url, responseText]) => [
118118
// Convert relative to absolute URL
119-
new URL(url, TEST_URL).href,
119+
new URL(url, TEST_HOST).href,
120120
// Strip indentation from responseText
121121
stripIndent`${responseText}`,
122122
])
@@ -128,11 +128,11 @@ async function docsifyInit(options = {}) {
128128
scriptURLs: []
129129
.concat(options.scriptURLs || '')
130130
.filter(url => url)
131-
.map(url => new URL(url, TEST_URL).href),
131+
.map(url => new URL(url, TEST_HOST).href),
132132
styleURLs: []
133133
.concat(options.styleURLs || '')
134134
.filter(url => url)
135-
.map(url => new URL(url, TEST_URL).href),
135+
.map(url => new URL(url, TEST_HOST).href),
136136
};
137137

138138
// Routes

test/integration/example.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ describe('Example Tests', function() {
1010
test('Docsify /docs/ site using docsifyInit()', async () => {
1111
await docsifyInit({
1212
config: {
13-
basePath: `${TEST_URL}/docs`,
13+
basePath: `${TEST_HOST}/docs`,
1414
},
1515
// _logHTML: true,
1616
});

0 commit comments

Comments
 (0)