Skip to content

Commit 9dadb35

Browse files
hramosJoelMarcey
authored andcommitted
Add first set of docusaurus-build tests using Jest (facebook#259)
* Add Prettier formatting to source files and example files, and check that Prettier formatting is maintained on PRs * Remove trailing-comma as we are using Node 6 on Circle * Use latest Node 6 LTS version in Circle * Initial test suite * Rename test to match original file * restore test files * Remove yarn.lock from pull request. Will run it again in a separate commit
1 parent 8ec4a6b commit 9dadb35

File tree

4 files changed

+122
-6
lines changed

4 files changed

+122
-6
lines changed

.circleci/config.yml

+7-4
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ defaults: &defaults
3131

3232
version: 2
3333
jobs:
34-
test-website:
34+
tests:
3535
<<: *defaults
3636
steps:
3737
- checkout
@@ -42,7 +42,10 @@ jobs:
4242
name: Check Prettier
4343
command: yarn ci-check
4444
- run:
45-
name: Test Build Static Website
45+
name: Run Test Suites
46+
command: yarn test
47+
- run:
48+
name: Test Static Website Builds
4649
command: cd website && yarn run build
4750

4851
deploy-website:
@@ -99,11 +102,11 @@ workflows:
99102

100103
website:
101104
jobs:
102-
- test-website:
105+
- tests:
103106
filters: *filter-ignore-gh-pages
104107
- deploy-website:
105108
requires:
106-
- test-website
109+
- tests
107110
filters: *filter-only-master
108111

109112
deploy:

.watchmanconfig

Whitespace-only changes.

lib/__tests__/build-files.tests.js

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
const filepath = require('filepath');
2+
const fm = require('front-matter');
3+
const fs = require('fs-extra');
4+
const glob = require('glob-promise');
5+
const rimraf = require('rimraf');
6+
const shell = require('shelljs');
7+
8+
const CWD = process.cwd();
9+
10+
const siteConfig = require(CWD + '/website/siteConfig.js');
11+
const buildDir = CWD + '/website/build';
12+
const docsDir = CWD + '/docs';
13+
const staticCSSDir = CWD + '/website/static/css';
14+
15+
let inputMarkdownFiles = [];
16+
let inputAssetsFiles = [];
17+
let outputHTMLFiles = [];
18+
let outputAssetsFiles = [];
19+
20+
function generateSite() {
21+
shell.cd('website');
22+
shell.exec('yarn build');
23+
}
24+
25+
function clearBuildFolder() {
26+
return rimraf(buildDir);
27+
}
28+
29+
beforeEach(() => {
30+
shell.cd(CWD);
31+
});
32+
33+
beforeAll(() => {
34+
generateSite();
35+
return Promise.all([
36+
glob(docsDir + '/**/*.md'),
37+
glob(buildDir + '/' + siteConfig.projectName + '/docs/*.html'),
38+
glob(docsDir + '/assets/*'),
39+
glob(buildDir + '/' + siteConfig.projectName + '/img/*'),
40+
]).then(function(results) {
41+
inputMarkdownFiles = results[0];
42+
outputHTMLFiles = results[1];
43+
inputAssetsFiles = results[2];
44+
outputAssetsFiles = results[3];
45+
return;
46+
});
47+
});
48+
49+
function afterAll() {
50+
clearBuildFolder();
51+
}
52+
53+
test('Build folder exists', function() {
54+
return fs.stat(buildDir).then(function(status) {
55+
expect(status.isDirectory()).toBeTruthy();
56+
});
57+
});
58+
59+
test('Generated HTML for each Markdown resource', function() {
60+
let metadata = [];
61+
outputHTMLFiles.forEach(function(file) {
62+
const path = filepath.create(file);
63+
metadata.push(path.basename());
64+
});
65+
inputMarkdownFiles.forEach(function(file) {
66+
const path = filepath.create(file);
67+
const data = fs.readFileSync(file, 'utf8');
68+
const frontmatter = fm(data);
69+
expect(metadata).toContain(frontmatter.attributes.id + '.html');
70+
});
71+
});
72+
73+
test('Generated table of contents', function() {
74+
outputHTMLFiles.forEach(function(file) {
75+
const fileContents = fs.readFileSync(file, 'utf8');
76+
expect(fileContents).not.toContain('<AUTOGENERATED_TABLE_OF_CONTENTS>');
77+
});
78+
});
79+
80+
test('Concatenated CSS files', function() {
81+
return Promise.all([
82+
glob(staticCSSDir + '/*.css'),
83+
fs.readFile(
84+
buildDir + '/' + siteConfig.projectName + '/css/main.css',
85+
'utf8'
86+
),
87+
]).then(function(results) {
88+
const inputFiles = results[0];
89+
const outputFile = results[1];
90+
inputFiles.forEach(function(file) {
91+
const contents = fs.readFileSync(file, 'utf8');
92+
expect(outputFile).toContain(contents);
93+
});
94+
});
95+
});
96+
97+
test('Copied assets from /docs/assets', function() {
98+
let metadata = [];
99+
outputAssetsFiles.forEach(function(file) {
100+
const path = filepath.create(file);
101+
metadata.push(path.basename());
102+
});
103+
inputAssetsFiles.forEach(function(file) {
104+
const path = filepath.create(file);
105+
expect(metadata).toContain(path.basename());
106+
});
107+
});

package.json

+8-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
"nit:source": "prettier --config .prettierrc --list-different \"lib/**/*.js\"",
2121
"nit:examples": "prettier --config .prettierrc --list-different \"examples/**/*.js\"",
2222
"prettier": "yarn format:source && yarn format:examples",
23-
"prettier:diff": "yarn nit:source && yarn nit:examples"
23+
"prettier:diff": "yarn nit:source && yarn nit:examples",
24+
"test": "jest"
2425
},
2526
"dependencies": {
2627
"babel-preset-env": "^1.6.0",
@@ -59,6 +60,11 @@
5960
"docusaurus-feed": "./lib/generate-feed.js"
6061
},
6162
"devDependencies": {
62-
"prettier": "^1.9.1"
63+
"prettier": "^1.9.1",
64+
"filepath": "^1.1.0",
65+
"front-matter": "^2.3.0",
66+
"glob-promise": "^3.3.0",
67+
"jest": "^21.2.1",
68+
"rimraf": "^2.6.2"
6369
}
6470
}

0 commit comments

Comments
 (0)