Browse Source

fix test (#2825)

pull/2861/head
陈帅 7 years ago
committed by GitHub
parent
commit
e876ec207e
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      .circleci/config.yml
  2. 1
      .eslintrc.js
  3. 6
      Dockerfile
  4. 2
      config/plugin.config.js
  5. 1
      jest.config.js
  6. 1
      package.json
  7. 46
      src/e2e/baseLayout.e2e.js
  8. 12
      src/e2e/home.e2e.js
  9. 11
      src/e2e/login.e2e.js
  10. 12
      src/e2e/userLayout.e2e.js
  11. 8
      tests/run-tests.js

4
.circleci/config.yml

@ -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

1
.eslintrc.js

@ -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'] }],

6
Dockerfile

@ -1,9 +1,9 @@
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 ./ ./

2
config/plugin.config.js

@ -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',
}, },
]); ]);
}; };

1
jest.config.js

@ -1,3 +1,4 @@
module.exports = { module.exports = {
testURL: 'http://localhost:8000', testURL: 'http://localhost:8000',
preset: 'jest-puppeteer',
}; };

1
package.json

@ -36,6 +36,7 @@
"dva": "^2.4.0", "dva": "^2.4.0",
"enquire-js": "^0.2.1", "enquire-js": "^0.2.1",
"hash.js": "^1.1.5", "hash.js": "^1.1.5",
"jest-puppeteer": "^3.5.1",
"lodash": "^4.17.10", "lodash": "^4.17.10",
"lodash-decorators": "^6.0.0", "lodash-decorators": "^6.0.0",
"memoize-one": "^4.0.0", "memoize-one": "^4.0.0",

46
src/e2e/baseLayout.e2e.js

@ -0,0 +1,46 @@
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);
});
});

12
src/e2e/home.e2e.js

@ -1,5 +1,3 @@
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', () => {
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();
}); });
}); });

11
src/e2e/login.e2e.js

@ -1,24 +1,15 @@
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', () => {
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());
}); });

12
src/e2e/layout.e2e.js → src/e2e/userLayout.e2e.js

@ -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', () => {
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());

8
tests/run-tests.js

@ -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(
/^win/.test(process.platform) ? 'npm.cmd' : 'npm',
['test', '--', '--maxWorkers=1'],
{
stdio: 'inherit', stdio: 'inherit',
}); }
);
testCmd.on('exit', code => { testCmd.on('exit', code => {
startServer.kill(); startServer.kill();
process.exit(code); process.exit(code);

Loading…
Cancel
Save