diff --git a/circle.yml b/circle.yml index ce16a4e81..2b5307e29 100644 --- a/circle.yml +++ b/circle.yml @@ -29,8 +29,10 @@ test: - yarn run test - nvm alias default 8 - yarn run test + - yarn run test:e2e - nvm alias default 9 - yarn run test + - yarn run test:e2e - yarn run coverage:publish - echo "machine github.com login verdacciobot password $GITHUB_TOKEN" > ~/.netrc - cd website && yarn install && GIT_USER=verdacciobot USE_SSH=false yarn run publish-gh-pages diff --git a/jest.e2e.config.js b/jest.e2e.config.js new file mode 100644 index 000000000..d18395032 --- /dev/null +++ b/jest.e2e.config.js @@ -0,0 +1,11 @@ +/* eslint comma-dangle: 0 */ + +module.exports = { + 'name': 'verdaccio-e2e-jest', + 'verbose': true, + 'collectCoverage': false, + 'globalSetup': './test/e2e/setup.js', + 'globalTeardown': './test/e2e/teardown.js', + 'testEnvironment': './test/e2e/puppeteer_environment.js', + 'testRegex': '(/test/e2e/e2e.*\\.spec)\\.js' +}; diff --git a/package.json b/package.json index 00c63ab99..7bf66b6b5 100644 --- a/package.json +++ b/package.json @@ -102,11 +102,13 @@ "jest": "22.1.4", "jest-environment-jsdom": "22.1.4", "jest-environment-jsdom-global": "1.0.3", + "jest-environment-node": "22.2.0", "localstorage-memory": "1.0.2", "node-sass": "4.7.2", "normalize.css": "7.0.0", "ora": "^1.4.0", "prop-types": "15.6.0", + "puppeteer": "1.0.0", "react": "16.2.0", "react-dom": "16.2.0", "react-hot-loader": "4.0.0-beta.17", @@ -145,6 +147,7 @@ "flow": "flow", "pretest": "npm run code:build", "test": "cross-env NODE_ENV=test BABEL_ENV=test jest --maxWorkers 2", + "test:e2e": "cross-env NODE_ENV=test BABEL_ENV=test jest --config ./jest.e2e.config.js --maxWorkers 2", "test:unit": "cross-env NODE_ENV=test BABEL_ENV=test jest '(/test/unit.*\\.spec|/test/webui/.*\\.spec)\\.js' --maxWorkers 2", "test:func": "cross-env NODE_ENV=test BABEL_ENV=test jest '(/test/functional.*\\.func)\\.js' --maxWorkers 2", "pre:ci": "npm run lint && npm run build:webui", diff --git a/test/e2e/e2e.spec.js b/test/e2e/e2e.spec.js new file mode 100644 index 000000000..f35786bfc --- /dev/null +++ b/test/e2e/e2e.spec.js @@ -0,0 +1,24 @@ +const timeout = 5000; + +describe( + '/ (Home Page)', + () => { + let page; + + beforeAll(async () => { + page = await global.__BROWSER__.newPage(); + await page.goto('https://google.com'); + }, timeout); + + afterAll(async () => { + await page.close() + }); + + it('should load without error', async () => { + let text = await page.evaluate(() => document.body.textContent); + + expect(text).toContain('google'); + }) + }, + timeout +); diff --git a/test/e2e/puppeteer_environment.js b/test/e2e/puppeteer_environment.js new file mode 100644 index 000000000..f41d7b9f3 --- /dev/null +++ b/test/e2e/puppeteer_environment.js @@ -0,0 +1,37 @@ +const chalk = require('chalk'); +const NodeEnvironment = require('jest-environment-node'); +const puppeteer = require('puppeteer'); +const fs = require('fs'); +const os = require('os'); +const path = require('path'); + +const DIR = path.join(os.tmpdir(), 'jest_puppeteer_global_setup'); + +class PuppeteerEnvironment extends NodeEnvironment { + constructor(config) { + super(config) + } + + async setup() { + console.log(chalk.yellow('Setup Test Environment.')); + await super.setup(); + const wsEndpoint = fs.readFileSync(path.join(DIR, 'wsEndpoint'), 'utf8'); + if (!wsEndpoint) { + throw new Error('wsEndpoint not found') + } + this.global.__BROWSER__ = await puppeteer.connect({ + browserWSEndpoint: wsEndpoint, + }) + } + + async teardown() { + console.log(chalk.yellow('Teardown Test Environment.')); + await super.teardown() + } + + runScript(script) { + return super.runScript(script) + } +} + +module.exports = PuppeteerEnvironment; diff --git a/test/e2e/setup.js b/test/e2e/setup.js new file mode 100644 index 000000000..26563ea99 --- /dev/null +++ b/test/e2e/setup.js @@ -0,0 +1,16 @@ +const chalk = require('chalk'); +const puppeteer = require('puppeteer'); +const fs = require('fs'); +const mkdirp = require('mkdirp'); +const os = require('os'); +const path = require('path'); + +const DIR = path.join(os.tmpdir(), 'jest_puppeteer_global_setup'); + +module.exports = async function() { + console.log(chalk.green('Setup Puppeteer')); + const browser = await puppeteer.launch({}); + global.__BROWSER__ = browser; + mkdirp.sync(DIR); + fs.writeFileSync(path.join(DIR, 'wsEndpoint'), browser.wsEndpoint()); +}; diff --git a/test/e2e/teardown.js b/test/e2e/teardown.js new file mode 100644 index 000000000..d02998e76 --- /dev/null +++ b/test/e2e/teardown.js @@ -0,0 +1,12 @@ +const chalk = require('chalk'); +const rimraf = require('rimraf'); +const os = require('os'); +const path = require('path'); + +const DIR = path.join(os.tmpdir(), 'jest_puppeteer_global_setup'); + +module.exports = async function() { + console.log(chalk.green('Teardown Puppeteer')); + await global.__BROWSER__.close(); + rimraf.sync(DIR) +}; diff --git a/yarn.lock b/yarn.lock index 59643fd6b..e6ec1b9b0 100644 Binary files a/yarn.lock and b/yarn.lock differ