Skip to content
This repository was archived by the owner on Jun 15, 2019. It is now read-only.

Commit ecaf75c

Browse files
committed
test(e2e): Two e2e tests were added (and outms-test was removed)
Apart from End-to-End test, a lot of configurations were changed.
1 parent db5ae28 commit ecaf75c

23 files changed

+648
-166
lines changed

.travis.yml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
language: node_js
22

3-
cache:
4-
directories:
5-
- ~/.npm
3+
cache: npm
64

75
jobs:
86
include:
@@ -20,9 +18,10 @@ jobs:
2018

2119
- stage: "Release"
2220
name: "Build and Release"
23-
if: type == push && tag !~ ^v\d+\.\d+\.\d+ && branch == master && commit_message =~ /^(feat|fix|refactor|pref|build|revert)\(/
21+
if: type == push && tag !~ ^v\d+\.\d+\.\d+ && branch == master &&
22+
commit_message =~ /^((feat|fix|refactor|pref|build|revert)\(|test\((?i)[\s\S]*?\b(E2E|END-TO-END|END2END)\b)/ # https://regex101.com/r/XWKT5J/1/
2423
script: npm run build:prod
25-
after_success: npm run test:outms
24+
after_success: npm run test:e2e
2625
node_js: lts/*
2726
deploy:
2827
provider: script

babel.config.js

Lines changed: 0 additions & 12 deletions
This file was deleted.

jest.outms.config.json

Lines changed: 0 additions & 4 deletions
This file was deleted.

jest.unit.config.js

Lines changed: 0 additions & 31 deletions
This file was deleted.

jest/e2e/browser.js.flow

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* @author [S. Mahdi Mir-Ismaili](https://mirismaili.github.io)
3+
* Created on 1398/3/1 (2019/5/22).
4+
*/
5+
'use strict'
6+
7+
const path = require('path')
8+
const Page = require('puppeteer')
9+
const Browser = require('puppeteer')
10+
const PORT = require('./config/options.js').listenPort
11+
12+
const mockPagePath = path.relative(process.cwd(), path.resolve(__dirname, 'mock', 'index.html'))
13+
.replace(/\\/g, '/')
14+
const mockPageUrl = `http://127.0.0.1:${PORT}/${mockPagePath}`
15+
16+
const timeout = 5000
17+
18+
describe(`Browser environment: "bundle.umd.js" & "bundle.esm.js" (at: ${mockPageUrl})`, () => {
19+
let browser: Browser
20+
let page: Page
21+
let pageTitle: string
22+
let errors = []
23+
24+
beforeAll(async () => {
25+
// noinspection JSUnresolvedVariable
26+
browser = global.__BROWSER__
27+
28+
page = (await browser.pages())[0];
29+
30+
const handler = err => errors.push(err)
31+
32+
page.on('requestfailed', handler)
33+
page.on('pageerror', handler)
34+
page.on('error', handler)
35+
36+
await page.goto(mockPageUrl)
37+
38+
pageTitle = await page.title()
39+
}, timeout)
40+
41+
it('Should load without error', () => {
42+
expect(pageTitle).toBe('Mock HTML')
43+
expect(errors).toStrictEqual([])
44+
})
45+
}, timeout)

jest/e2e/config/jestPreprocess.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* @author [S. Mahdi Mir-Ismaili](https://mirismaili.github.io)
3+
* Created on 1398/3/2 (2019/5/23).
4+
*/
5+
'use strict'
6+
7+
module.exports = require('babel-jest').createTransformer({
8+
presets: [
9+
["@babel/preset-flow", {"targets": {"node": "current"}}],
10+
]
11+
})

jest/e2e/config/main.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @author [S. Mahdi Mir-Ismaili](https://mirismaili.github.io)
3+
* Created on 1398/3/2 (2019/5/23).
4+
*/
5+
'use strict'
6+
7+
const path = require('path')
8+
const parentPath = path.resolve(__dirname, '..').replace(/\\/g, '/')
9+
10+
module.exports = {
11+
rootDir: path.relative(__dirname, process.cwd()),
12+
moduleFileExtensions: ['js', 'flow'],
13+
testRegex: `${parentPath}/(?!config|mock).+?\\.js(\\.flow)?$`,
14+
globalSetup: `${__dirname}/setup.js`,
15+
globalTeardown: `${__dirname}/teardown.js`,
16+
testEnvironment: `${__dirname}/puppeteer_environment.js`,
17+
transform: {
18+
'.+\\.js\\.flow$': `${__dirname}/jestPreprocess.js`
19+
},
20+
}

jest/e2e/config/options.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* @author [S. Mahdi Mir-Ismaili](https://mirismaili.github.io)
3+
* Created on 1398/3/3 (2019/5/24).
4+
*/
5+
'use strict'
6+
7+
const os = require('os')
8+
const path = require('path')
9+
10+
module.exports.listenPort = 3000
11+
module.exports.tmpDir = path.join(os.tmpdir(), 'jest_puppeteer_global_setup')
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* @author [S. Mahdi Mir-Ismaili](https://mirismaili.github.io)
3+
* Created on 1398/3/1 (2019/5/22).
4+
*/
5+
'use strict'
6+
7+
const NodeEnvironment = require('jest-environment-node')
8+
const fs = require('fs')
9+
const path = require('path')
10+
const puppeteer = require('puppeteer')
11+
const os = require('os')
12+
13+
const DIR = path.join(os.tmpdir(), 'jest_puppeteer_global_setup')
14+
15+
class PuppeteerEnvironment extends NodeEnvironment {
16+
constructor(config) {
17+
super(config)
18+
}
19+
20+
async setup() {
21+
await super.setup();
22+
// get the wsEndpoint
23+
const wsEndpoint = fs.readFileSync(path.join(DIR, 'wsEndpoint'), 'utf8')
24+
if (!wsEndpoint) {
25+
throw new Error('wsEndpoint not found');
26+
}
27+
28+
// connect to puppeteer
29+
// noinspection JSUndefinedPropertyAssignment
30+
this.global.__BROWSER__ = await puppeteer.connect({
31+
browserWSEndpoint: wsEndpoint,
32+
})
33+
}
34+
35+
async teardown() {
36+
await super.teardown()
37+
}
38+
39+
runScript(script) {
40+
return super.runScript(script)
41+
}
42+
}
43+
44+
module.exports = PuppeteerEnvironment

jest/e2e/config/setup.js

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/**
2+
* @author [S. Mahdi Mir-Ismaili](https://mirismaili.github.io)
3+
* Created on 1398/3/1 (2019/5/22).
4+
*/
5+
'use strict'
6+
7+
const fs = require('fs')
8+
const path = require('path')
9+
const url = require('url')
10+
const http = require('http')
11+
12+
const puppeteer = require('puppeteer')
13+
const mkdirp = require('mkdirp')
14+
15+
const PORT = require('./options.js').listenPort
16+
const TMP_DIR = require('./options.js').tmpDir
17+
18+
const staticServerPromise = new Promise((resolve, reject) => {
19+
const staticServer = http.createServer((req, res) => {
20+
// console.debug(req.method + ' ' + req.url);
21+
22+
try {
23+
const uri = url.parse(req.url).pathname
24+
let filePath = path.join(process.cwd(), uri)
25+
26+
e404:{
27+
if (!fs.existsSync(filePath)) break e404
28+
29+
if (fs.statSync(filePath).isDirectory()) filePath = path.join(filePath, 'index.html')
30+
31+
if (!fs.existsSync(filePath)) break e404
32+
33+
const contentTypesByExtension = {
34+
'.html': 'text/html',
35+
'.css': 'text/css',
36+
'.js': 'text/javascript'
37+
}
38+
39+
const data = fs.readFileSync(filePath, 'binary')
40+
41+
const headers = {}
42+
const contentType = contentTypesByExtension[path.extname(filePath)]
43+
if (contentType) headers['Content-Type'] = contentType
44+
res.writeHead(200, headers)
45+
res.write(data, 'binary')
46+
res.end()
47+
48+
return
49+
}
50+
//----------------------------------------------------------/e404:
51+
// noinspection UnreachableCodeJS
52+
console.error(`File not found: "${filePath}"`)
53+
res.writeHead(404, {'Content-Type': 'text/plain'})
54+
res.write('404/ Not Found\n')
55+
res.end()
56+
} catch (exeption) {
57+
console.error(e.stack)
58+
res.writeHead(500, {'Content-Type': 'text/plain'})
59+
res.write(e.toString())
60+
res.end()
61+
}
62+
})
63+
64+
staticServer.on('error', reject)
65+
66+
staticServer.listen(PORT, () => {
67+
// noinspection JSUndefinedPropertyAssignment
68+
global.__STATIC_SERVER__ = staticServer
69+
70+
console.info(`Static file server running on: http://127.0.0.1:${PORT}`)
71+
resolve()
72+
})
73+
})
74+
75+
const launchPuppeteerPromise = new Promise((resolve, reject) => {
76+
puppeteer.launch(
77+
// {
78+
// headless: false,
79+
// // slowMo: 500,
80+
// devtools: true,
81+
// }
82+
).then(browser => {
83+
// store the browser instance so we can teardown it later
84+
// this global is only available in the teardown but not in TestEnvironments
85+
// noinspection JSUndefinedPropertyAssignment
86+
global.__BROWSER_GLOBAL__ = browser
87+
88+
// use the file system to expose the wsEndpoint for TestEnvironments
89+
mkdirp.sync(TMP_DIR)
90+
fs.writeFileSync(path.join(TMP_DIR, 'wsEndpoint'), browser.wsEndpoint())
91+
}, reject).then(resolve, reject)
92+
})
93+
94+
// noinspection JSCheckFunctionSignatures
95+
module.exports = async () => await Promise.all([staticServerPromise, launchPuppeteerPromise])
96+

0 commit comments

Comments
 (0)