From e876ec207e647b3b6669e5464082aef7139fd043 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=B8=85?= Date: Wed, 14 Nov 2018 18:34:49 +0800 Subject: [PATCH] fix test (#2825) --- .circleci/config.yml | 4 +- .eslintrc.js | 1 + Dockerfile | 6 +-- config/plugin.config.js | 2 + jest.config.js | 1 + package.json | 1 + src/e2e/baseLayout.e2e.js | 46 ++++++++++++++++++++ src/e2e/home.e2e.js | 12 ++--- src/e2e/login.e2e.js | 11 ----- src/e2e/{layout.e2e.js => userLayout.e2e.js} | 12 ++--- tests/run-tests.js | 10 +++-- 11 files changed, 73 insertions(+), 33 deletions(-) create mode 100644 src/e2e/baseLayout.e2e.js rename src/e2e/{layout.e2e.js => userLayout.e2e.js} (78%) diff --git a/.circleci/config.yml b/.circleci/config.yml index ddafc61e..28ad7a2a 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -2,14 +2,14 @@ version: 2 jobs: build: docker: - - image: circleci/node:8.11.4 + - image: circleci/node:latest steps: - checkout - run: npm install - run: npm run build test: docker: - - image: circleci/node:8.11.4 + - image: circleci/node:latest steps: - checkout - run: sh ./tests/fix_puppeteer.sh diff --git a/.eslintrc.js b/.eslintrc.js index d92c54f4..61b7c58f 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -11,6 +11,7 @@ module.exports = { }, globals: { APP_TYPE: true, + page: true, }, rules: { 'react/jsx-filename-extension': [1, { extensions: ['.js'] }], diff --git a/Dockerfile b/Dockerfile index 11144bb0..af06a83c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,9 +1,9 @@ -FROM node:latest +FROM circleci/node:latest WORKDIR /usr/src/app/ - +USER root COPY package.json ./ -RUN npm install --silent --no-cache +RUN yarn COPY ./ ./ diff --git a/config/plugin.config.js b/config/plugin.config.js index 57f6deb6..55e8577b 100644 --- a/config/plugin.config.js +++ b/config/plugin.config.js @@ -23,6 +23,8 @@ export default config => { varFile: path.join(__dirname, '../node_modules/antd/lib/style/themes/default.less'), mainLessFile: outFile, // themeVariables: ['@primary-color'], indexFileName: 'index.html', + generateOne: true, + lessUrl: 'https://gw.alipayobjects.com/os/lib/less.js/3.8.1/less.min.js', }, ]); }; diff --git a/jest.config.js b/jest.config.js index ec949f06..68a1144b 100644 --- a/jest.config.js +++ b/jest.config.js @@ -1,3 +1,4 @@ module.exports = { testURL: 'http://localhost:8000', + preset: 'jest-puppeteer', }; diff --git a/package.json b/package.json index 2be16730..eddfd95c 100644 --- a/package.json +++ b/package.json @@ -36,6 +36,7 @@ "dva": "^2.4.0", "enquire-js": "^0.2.1", "hash.js": "^1.1.5", + "jest-puppeteer": "^3.5.1", "lodash": "^4.17.10", "lodash-decorators": "^6.0.0", "memoize-one": "^4.0.0", diff --git a/src/e2e/baseLayout.e2e.js b/src/e2e/baseLayout.e2e.js new file mode 100644 index 00000000..8487a758 --- /dev/null +++ b/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); + }); +}); diff --git a/src/e2e/home.e2e.js b/src/e2e/home.e2e.js index cadac245..b8d22cb8 100644 --- a/src/e2e/home.e2e.js +++ b/src/e2e/home.e2e.js @@ -1,5 +1,3 @@ -import puppeteer from 'puppeteer'; - const BASE_URL = `http://localhost:${process.env.PORT || 8000}`; describe('Homepage', () => { @@ -7,13 +5,11 @@ describe('Homepage', () => { jest.setTimeout(1000000); }); it('it should have logo text', async () => { - const browser = await puppeteer.launch({ args: ['--no-sandbox'] }); - const page = await browser.newPage(); - await page.goto(BASE_URL, { waitUntil: 'networkidle2' }); + await page.goto(BASE_URL); + await page.waitForSelector('h1', { + timeout: 2000, + }); const text = await page.evaluate(() => document.getElementsByTagName('h1')[0].innerText); expect(text).toContain('Ant Design Pro'); - - await page.close(); - browser.close(); }); }); diff --git a/src/e2e/login.e2e.js b/src/e2e/login.e2e.js index 589ad46b..b991af44 100644 --- a/src/e2e/login.e2e.js +++ b/src/e2e/login.e2e.js @@ -1,24 +1,15 @@ -import puppeteer from 'puppeteer'; - const BASE_URL = `http://localhost:${process.env.PORT || 8000}`; describe('Login', () => { - let browser; - let page; - beforeAll(async () => { jest.setTimeout(1000000); - browser = await puppeteer.launch({ args: ['--no-sandbox'] }); }); beforeEach(async () => { - page = await browser.newPage(); await page.goto(`${BASE_URL}/user/login`, { waitUntil: 'networkidle2' }); await page.evaluate(() => window.localStorage.setItem('antd-pro-authority', 'guest')); }); - afterEach(() => page.close()); - it('should login with failure', async () => { await page.waitForSelector('#userName', { timeout: 2000, @@ -40,6 +31,4 @@ describe('Login', () => { const text = await page.evaluate(() => document.body.innerHTML); expect(text).toContain('

Ant Design Pro

'); }); - - afterAll(() => browser.close()); }); diff --git a/src/e2e/layout.e2e.js b/src/e2e/userLayout.e2e.js similarity index 78% rename from src/e2e/layout.e2e.js rename to src/e2e/userLayout.e2e.js index 1a632616..f7d0ee45 100644 --- a/src/e2e/layout.e2e.js +++ b/src/e2e/userLayout.e2e.js @@ -17,7 +17,10 @@ describe('Homepage', () => { let page; 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( () => document.getElementsByTagName('footer').length > 0 ); @@ -29,11 +32,8 @@ describe('Homepage', () => { browser = await puppeteer.launch({ args: ['--no-sandbox'] }); page = await browser.newPage(); }); - - RouterConfig.forEach(({ routes = [] }) => { - formatter(routes).forEach(route => { - it(`test pages ${route}`, testPage(route)); - }); + formatter(RouterConfig[0].routes).forEach(route => { + fit(`test pages ${route}`, testPage(route)); }); afterAll(() => browser.close()); diff --git a/tests/run-tests.js b/tests/run-tests.js index 36d746e6..0a062352 100644 --- a/tests/run-tests.js +++ b/tests/run-tests.js @@ -29,9 +29,13 @@ startServer.stdout.on('data', data => { // eslint-disable-next-line once = true; console.log('Development server is started, ready to run tests.'); - const testCmd = spawn(/^win/.test(process.platform) ? 'npm.cmd' : 'npm', ['test'], { - stdio: 'inherit', - }); + const testCmd = spawn( + /^win/.test(process.platform) ? 'npm.cmd' : 'npm', + ['test', '--', '--maxWorkers=1'], + { + stdio: 'inherit', + } + ); testCmd.on('exit', code => { startServer.kill(); process.exit(code); -- GitLab