Unverified Commit e876ec20 authored by 陈帅's avatar 陈帅 Committed by GitHub

fix test (#2825)

parent 5beacedd
...@@ -2,14 +2,14 @@ version: 2 ...@@ -2,14 +2,14 @@ version: 2
jobs: jobs:
build: build:
docker: docker:
- image: circleci/node:8.11.4 - image: circleci/node:latest
steps: steps:
- checkout - checkout
- run: npm install - run: npm install
- run: npm run build - run: npm run build
test: test:
docker: docker:
- image: circleci/node:8.11.4 - image: circleci/node:latest
steps: steps:
- checkout - checkout
- run: sh ./tests/fix_puppeteer.sh - run: sh ./tests/fix_puppeteer.sh
......
...@@ -11,6 +11,7 @@ module.exports = { ...@@ -11,6 +11,7 @@ module.exports = {
}, },
globals: { globals: {
APP_TYPE: true, APP_TYPE: true,
page: true,
}, },
rules: { rules: {
'react/jsx-filename-extension': [1, { extensions: ['.js'] }], 'react/jsx-filename-extension': [1, { extensions: ['.js'] }],
......
FROM node:latest FROM circleci/node:latest
WORKDIR /usr/src/app/ WORKDIR /usr/src/app/
USER root
COPY package.json ./ COPY package.json ./
RUN npm install --silent --no-cache RUN yarn
COPY ./ ./ COPY ./ ./
......
...@@ -23,6 +23,8 @@ export default config => { ...@@ -23,6 +23,8 @@ export default config => {
varFile: path.join(__dirname, '../node_modules/antd/lib/style/themes/default.less'), varFile: path.join(__dirname, '../node_modules/antd/lib/style/themes/default.less'),
mainLessFile: outFile, // themeVariables: ['@primary-color'], mainLessFile: outFile, // themeVariables: ['@primary-color'],
indexFileName: 'index.html', indexFileName: 'index.html',
generateOne: true,
lessUrl: 'https://gw.alipayobjects.com/os/lib/less.js/3.8.1/less.min.js',
}, },
]); ]);
}; };
module.exports = { module.exports = {
testURL: 'http://localhost:8000', testURL: 'http://localhost:8000',
preset: 'jest-puppeteer',
}; };
import RouterConfig from '../../config/router.config';
const BASE_URL = `http://localhost:${process.env.PORT || 8000}`;
function formatter(data) {
return data
.reduce((pre, item) => {
pre.push(item.path);
return pre;
}, [])
.filter(item => item);
}
describe('Homepage', async () => {
const testPage = path =>
new Promise(async reslove => {
console.log(`test ${path}`);
await page.goto(`${BASE_URL}${path}`, {
timeout: 600000,
});
await page.waitForSelector('footer', {
timeout: 600000,
});
reslove();
});
beforeAll(async () => {
jest.setTimeout(1000000);
await page.setCacheEnabled(false);
});
it(`test pages`, async () => {
const routers = formatter(RouterConfig[1].routes);
const testAll = index =>
new Promise(async reslove => {
await testPage(routers[index]);
if (index < routers.length - 1) {
const newIndex = index + 1;
await testAll(newIndex);
reslove();
}
reslove();
});
await testAll(0);
});
});
import puppeteer from 'puppeteer';
const BASE_URL = `http://localhost:${process.env.PORT || 8000}`; const BASE_URL = `http://localhost:${process.env.PORT || 8000}`;
describe('Homepage', () => { describe('Homepage', () => {
...@@ -7,13 +5,11 @@ describe('Homepage', () => { ...@@ -7,13 +5,11 @@ describe('Homepage', () => {
jest.setTimeout(1000000); jest.setTimeout(1000000);
}); });
it('it should have logo text', async () => { it('it should have logo text', async () => {
const browser = await puppeteer.launch({ args: ['--no-sandbox'] }); await page.goto(BASE_URL);
const page = await browser.newPage(); await page.waitForSelector('h1', {
await page.goto(BASE_URL, { waitUntil: 'networkidle2' }); timeout: 2000,
});
const text = await page.evaluate(() => document.getElementsByTagName('h1')[0].innerText); const text = await page.evaluate(() => document.getElementsByTagName('h1')[0].innerText);
expect(text).toContain('Ant Design Pro'); expect(text).toContain('Ant Design Pro');
await page.close();
browser.close();
}); });
}); });
import puppeteer from 'puppeteer';
const BASE_URL = `http://localhost:${process.env.PORT || 8000}`; const BASE_URL = `http://localhost:${process.env.PORT || 8000}`;
describe('Login', () => { describe('Login', () => {
let browser;
let page;
beforeAll(async () => { beforeAll(async () => {
jest.setTimeout(1000000); jest.setTimeout(1000000);
browser = await puppeteer.launch({ args: ['--no-sandbox'] });
}); });
beforeEach(async () => { beforeEach(async () => {
page = await browser.newPage();
await page.goto(`${BASE_URL}/user/login`, { waitUntil: 'networkidle2' }); await page.goto(`${BASE_URL}/user/login`, { waitUntil: 'networkidle2' });
await page.evaluate(() => window.localStorage.setItem('antd-pro-authority', 'guest')); await page.evaluate(() => window.localStorage.setItem('antd-pro-authority', 'guest'));
}); });
afterEach(() => page.close());
it('should login with failure', async () => { it('should login with failure', async () => {
await page.waitForSelector('#userName', { await page.waitForSelector('#userName', {
timeout: 2000, timeout: 2000,
...@@ -40,6 +31,4 @@ describe('Login', () => { ...@@ -40,6 +31,4 @@ describe('Login', () => {
const text = await page.evaluate(() => document.body.innerHTML); const text = await page.evaluate(() => document.body.innerHTML);
expect(text).toContain('<h1>Ant Design Pro</h1>'); expect(text).toContain('<h1>Ant Design Pro</h1>');
}); });
afterAll(() => browser.close());
}); });
...@@ -17,7 +17,10 @@ describe('Homepage', () => { ...@@ -17,7 +17,10 @@ describe('Homepage', () => {
let page; let page;
const testPage = path => async () => { const testPage = path => async () => {
await page.goto(`${BASE_URL}${path}`, { waitUntil: 'networkidle2' }); await page.goto(`${BASE_URL}${path}`);
await page.waitForSelector('footer', {
timeout: 2000,
});
const haveFooter = await page.evaluate( const haveFooter = await page.evaluate(
() => document.getElementsByTagName('footer').length > 0 () => document.getElementsByTagName('footer').length > 0
); );
...@@ -29,11 +32,8 @@ describe('Homepage', () => { ...@@ -29,11 +32,8 @@ describe('Homepage', () => {
browser = await puppeteer.launch({ args: ['--no-sandbox'] }); browser = await puppeteer.launch({ args: ['--no-sandbox'] });
page = await browser.newPage(); page = await browser.newPage();
}); });
formatter(RouterConfig[0].routes).forEach(route => {
RouterConfig.forEach(({ routes = [] }) => { fit(`test pages ${route}`, testPage(route));
formatter(routes).forEach(route => {
it(`test pages ${route}`, testPage(route));
});
}); });
afterAll(() => browser.close()); afterAll(() => browser.close());
......
...@@ -29,9 +29,13 @@ startServer.stdout.on('data', data => { ...@@ -29,9 +29,13 @@ startServer.stdout.on('data', data => {
// eslint-disable-next-line // eslint-disable-next-line
once = true; once = true;
console.log('Development server is started, ready to run tests.'); console.log('Development server is started, ready to run tests.');
const testCmd = spawn(/^win/.test(process.platform) ? 'npm.cmd' : 'npm', ['test'], { const testCmd = spawn(
stdio: 'inherit', /^win/.test(process.platform) ? 'npm.cmd' : 'npm',
}); ['test', '--', '--maxWorkers=1'],
{
stdio: 'inherit',
}
);
testCmd.on('exit', code => { testCmd.on('exit', code => {
startServer.kill(); startServer.kill();
process.exit(code); process.exit(code);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment