Skip to content

Commit

Permalink
feat: add test
Browse files Browse the repository at this point in the history
  • Loading branch information
wangyi7099 authored and wangyi committed Mar 14, 2020
1 parent ebc7b97 commit 61df46e
Show file tree
Hide file tree
Showing 31 changed files with 1,410 additions and 179 deletions.
50 changes: 44 additions & 6 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,63 @@ defaults: &defaults
- image: circleci/node:10.15.2

jobs:
build-docs:
# install dependencies
install-dependencies:
<<: *defaults
steps:
- checkout

- restore_cache:
keys:
- v1-deps-{{ checksum "yarn.lock" }}
- v1-deps

docker:
# specify the version you desire here
- image: circleci/node:10.15.2
- run:
name: 'Install dependencies'
command: yarn bs

- save_cache:
key: v1-deps-{{ checksum "yarn.lock" }}
paths:
- ~/.cache/yarn

- persist_to_workspace:
root: ~/project/rcpress
paths:
- node_modules
- packages/*/node_modules
- packages/@rcpress/*/node_modules

# test
run-tests:
<<: *defaults
steps:
- checkout
- attach_workspace:
at: ~/project/rcpress
- run:
name: 'Run tests'
command: yarn test

# build docs and upload to server
build-docs:
<<: *defaults
steps:
- checkout
- run: yarn && yarn bs
- run: yarn generate-with-prefix
- run: ssh -o "StrictHostKeyChecking no" root@$DOCKER_IP "~/rcpress/before-dep.sh"
- run: scp -o "StrictHostKeyChecking no" -r packages/docs/docs/.rcpress/dist/* root@$DOCKER_IP:~/rcpress/tmp
- run: ssh -o "StrictHostKeyChecking no" root@$DOCKER_IP "~/rcpress/after-dep.sh"

workflows:
version: 2
build-and-deploy:
build:
jobs:
- install-dependencies
- run-tests:
requires: install-dependencies
- build-docs:
requires: install-dependencies
filters:
branches:
only:
Expand Down
1 change: 1 addition & 0 deletions __mocks__/fileMock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = {};
1 change: 1 addition & 0 deletions __mocks__/styleMock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = {};
7 changes: 7 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
env: {
test: {
presets: ['@babel/preset-env', '@babel/preset-react']
}
}
};
28 changes: 28 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module.exports = {
rootDir: './',
coverageReporters: ['lcov'],
collectCoverageFrom: ['**/*.{js,ts,tsx}', '!**/*/node_modules/**', '!**/docs/**', '!**/test/**'],
moduleNameMapper: {
'\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$':
'<rootDir>/__mocks__/fileMock.js',
'.*\\.(css|less|scss)$': 'identity-obj-proxy'
},
testRegex: '(/__test__/.*|(\\.|/)(test|spec))\\.(ts|js)?$',
transform: {
'^.+\\.jsx?$': 'babel7-jest',
'^.+\\.tsx?$': 'ts-jest'
},
snapshotSerializers: ['enzyme-to-json/serializer'],
setupFiles: ['<rootDir>/node_modules/regenerator-runtime/runtime'],
setupFilesAfterEnv: ['<rootDir>/setupTest.js'],
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
globals: {
'ts-jest': {
isolatedModules: true,
tsConfig: {
target: 'es6'
},
babelConfig: true
}
}
};
21 changes: 14 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,35 @@
"private": true,
"workspaces": [
"packages/@rcpress/*",
"packages/docs",
"packages/test"
"packages/docs"
],
"scripts": {
"bs": "lerna bootstrap",
"serve": "yarn workspace docs serve",
"generate": "yarn workspace docs generate",
"generate-with-prefix": "cross-env base=/rcpress yarn generate",
"cl": "conventional-changelog -p angular -i CHANGELOG.md -s",
"clean": "lerna clean && rm -rf node_modules",
"pub": "lerna publish --conventional-commits",
"dev": "yarn workspace docs dev",
"generate": "yarn workspace docs generate",
"generate-with-prefix": "cross-env base=/rcpress yarn generate",
"pre-pub": "lerna publish --conventional-commits --canary",
"dev": "yarn workspace docs dev"
"pub": "lerna publish --conventional-commits",
"serve": "yarn workspace docs serve",
"test": "jest --config jest.config.js --collectCoverage",
"test-u": "jest -u --collectCoverage"
},
"devDependencies": {
"babel7-jest": "^1.0.1",
"conventional-changelog-cli": "2.0.23",
"cross-env": "5.2.0",
"enzyme": "^3.8.0",
"enzyme-adapter-react-16": "^1.7.1",
"enzyme-to-json": "^3.3.5",
"husky": "^3.0.0",
"jest": "^24.8.0",
"jest-serializer-enzyme": "^1.0.0",
"lerna": "^3.11.1",
"npm-run-all": "^4.1.5",
"prettier": "^1.18.2",
"react-test-renderer": "^16.6.1",
"ts-jest": "^24.0.2",
"typescript": "3.5.3"
}
Expand Down
51 changes: 51 additions & 0 deletions packages/@rcpress/core/lib/node/__tests__/commands/cli.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const path = require('path');
const fs = require('fs-extra');
const { getSourceDirs } = require('@rcpress/test-util');
const spa = require('../../commands/spa');
const ssr = require('../../commands/ssr');

describe('Commands', () => {
test('dev', async () => {
await Promise.all(
getSourceDirs(__dirname).map(async ({ name, docsPath, docsTempPath }) => {
await fs.ensureDir(docsTempPath);
expect(() => {
spa(docsPath);
}).not.toThrow();
})
);
});

test('dev-prod', async () => {
await Promise.all(
getSourceDirs(__dirname).map(async ({ name, docsPath, docsTempPath }) => {
await fs.ensureDir(docsTempPath);
expect(() => {
spa(docsPath, {}, true);
}).not.toThrow();
})
);
});

test('ssr', async () => {
await Promise.all(
getSourceDirs(__dirname).map(async ({ name, docsPath, docsTempPath }) => {
await fs.ensureDir(docsTempPath);
expect(() => {
ssr(docsPath);
}).not.toThrow();
})
);
});

test('ssr-prod', async () => {
await Promise.all(
getSourceDirs(__dirname).map(async ({ name, docsPath, docsTempPath }) => {
await fs.ensureDir(docsTempPath);
expect(() => {
ssr(docsPath, {}, true);
}).not.toThrow();
})
);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# hello world!

hello world
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# hello world!

hello world
16 changes: 16 additions & 0 deletions packages/@rcpress/core/lib/node/__tests__/prepare/prepare.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const path = require('path');
const fs = require('fs-extra');
const prepare = require('../../prepare/index');
const { getSourceDirs } = require('@rcpress/test-util');

describe('App', () => {
test('should not throw error', async () => {
await Promise.all(
getSourceDirs(__dirname).map(async ({ name, docsPath, docsTempPath }) => {
await fs.ensureDir(docsTempPath);
const option = await prepare(docsPath);
expect(option.sourceDir).toBe(docsPath);
})
);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`ssr pagerender 1`] = `
"<!-- prettier-ignore -->
<!DOCTYPE html>
<html {{{ htmlAttr }}}>
<head>
<meta charset=\\"utf-8\\" />
<meta name=\\"viewport\\" content=\\"width=device-width,initial-scale=1\\" />
<!--react-helmet render area -->
{{{ title }}}
{{{ meta }}}
{{{ helmet-links }}}
<script>
window.RC_CONTEXT = \\"{{ RC_CONTEXT }}\\";
</script>
<!--loadable-components prefetch -->
{{{ links }}}
{{{ style }}}
</head>
<body>
<div id=\\"app\\"><div class=\\"page-wrapper index-page-wrapper\\"><div style=\\"width:100%\\"><div class=\\"\\"><div id=\\"header\\" class=\\"header\\"><div class=\\"ant-row\\"><div class=\\"ant-col ant-col-xs-24 ant-col-sm-24 ant-col-md-8 ant-col-lg-8 ant-col-xl-5 ant-col-xxl-4\\"><a id=\\"site-logo\\" href=\\"/\\"><span class=\\"left-top-title\\">rcpress</span></a></div><div class=\\"ant-col ant-col-xs-0 ant-col-sm-0 ant-col-md-16 ant-col-lg-16 ant-col-xl-19 ant-col-xxl-20\\"><div class=\\"header-meta\\"><div class=\\"right-header\\"></div><div id=\\"menu\\"><ul id=\\"nav\\" class=\\"ant-menu ant-menu-light ant-menu-root ant-menu-horizontal\\" role=\\"menu\\"></ul></div></div></div></div></div></div></div><div class=\\"ant-row main-wrapper\\"><div class=\\"ant-col ant-col-xs-24 ant-col-sm-24 ant-col-md-24 ant-col-lg-24 ant-col-xl-24 ant-col-xxl-24\\"><div><div><div class=\\"main-container\\"><article class=\\"markdown\\"><div class=\\"modifiedTime modifiedTimeLeft\\">Last Updated<!-- --> <!-- -->2020-03-14 21:55:00</div><div class=\\" toc-affix\\"><div><div class=\\"\\"><div class=\\"toc toc-affix ant-anchor-wrapper\\" style=\\"max-height:calc(100vh - 70px)\\"><div class=\\"ant-anchor\\"><div class=\\"ant-anchor-ink\\"><span class=\\"ant-anchor-ink-ball\\"></span></div><div class=\\"ant-anchor-link\\"><a class=\\"ant-anchor-link-title\\" href=\\"#hello-world\\" title=\\"hello world!\\">hello world!</a></div></div></div></div></div></div><section class=\\"markdown api-container\\"><h1 class=\\"__internal\\" id=\\"hello-world\\">hello world!<a href=\\"#hello-world\\" aria-hidden=\\"true\\" class=\\"anchor\\">#</a></h1><p>hello world</p></section><section class=\\"prev-next-nav\\"></section></article></div></div></div></div></div></div></div>
<!--loadable-components render area -->
{{{ scripts }}}
</body>
</html>
"
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# hello world!

hello world
31 changes: 31 additions & 0 deletions packages/@rcpress/core/lib/node/__tests__/ssr/page-render.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const prepare = require('../../prepare/index');
const fs = require('fs-extra');
const { getSourceDirs } = require('@rcpress/test-util');
const getConfig = require('../../commands/ssr/getConfig');
const { Helmet } = require('react-helmet');
jest.setTimeout(200000);
// Helmet.canUseDOM = false;
describe('ssr', () => {
beforeEach(() => {
Helmet.canUseDOM = false;
});

afterEach(() => {
Helmet.canUseDOM = true;
});
test('pagerender', async () => {
const { getPageRender } = require('../../commands/ssr');
await Promise.all(
getSourceDirs(__dirname).map(async ({ name, docsPath, docsTempPath }) => {
await fs.ensureDir(docsTempPath);
const [ssrConfig, spaConfig, options] = await getConfig(docsPath, {}, true);
const pagerender = await getPageRender(ssrConfig, spaConfig, options);
const html = await pagerender.renderPage(
options.siteData.pages[0],
true /* only render html*/
);
expect(html).toMatchSnapshot();
})
);
});
});
36 changes: 18 additions & 18 deletions packages/@rcpress/core/lib/node/commands/spa/index.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
module.exports = async function dev(sourceDir, cliOptions = {}, isProd) {
if (isProd) {
process.env.NODE_ENV = 'production';
}
const fs = require('fs-extra');
const path = require('path');
const chalk = require('chalk');
const webpack = require('webpack');

const fs = require('fs-extra');
const path = require('path');
const chalk = require('chalk');
const {
WebpackLogPlugin,
createSPAConfig,
markdownLoader: { frontMatterEmitter }
} = require('@rcpress/webpack');

const fileWatcher = require('../../fileWatcher');
const createServer = require('../../server');
const prepare = require('../../prepare');
const buildSW = require('../../sw');
const fileWatcher = require('../../fileWatcher');
const createServer = require('../../server');
const prepare = require('../../prepare');
const buildSW = require('../../sw');

const {
WebpackLogPlugin,
createSPAConfig,
markdownLoader: { frontMatterEmitter }
} = require('@rcpress/webpack');
const { applyUserWebpackConfig, logger, resolveHostandPort } = require('@rcpress/util');
const { applyUserWebpackConfig, logger, resolveHostandPort } = require('@rcpress/util');

const webpack = require('webpack');
module.exports = async function dev(sourceDir, cliOptions = {}, isProd) {
if (isProd) {
process.env.NODE_ENV = 'production';
}

logger.wait('\nExtracting site metadata...');

Expand Down
Loading

0 comments on commit 61df46e

Please sign in to comment.