-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: workflow of ci test * fix: force exit when jest test finished
- Loading branch information
Showing
15 changed files
with
1,274 additions
and
957 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,12 @@ | ||
/* eslint @typescript-eslint/no-var-requires: 0 */ | ||
const utils = require('create-cli-utils'); | ||
const getBuiltInPlugins = require('./getBuiltInPlugins'); | ||
const pkg = require('../package.json'); | ||
import * as utils from 'create-cli-utils'; | ||
import getBuiltInPlugins = require('./getBuiltInPlugins'); | ||
|
||
const forkChildProcessPath = require.resolve('../bin/child-process-start'); | ||
|
||
module.exports = (frameworkName, { packageInfo, extendCli }) => { | ||
// eslint-disable-next-line global-require | ||
const pkg = require('../package.json'); | ||
process.env.__FRAMEWORK_NAME__ = frameworkName; | ||
packageInfo.__ICEJS_INFO__ = { name: pkg.name, version: pkg.version }; | ||
utils.createCli(getBuiltInPlugins, forkChildProcessPath, packageInfo, extendCli); | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { buildFixture } from './utils/build'; | ||
|
||
const example = 'basic-auth'; | ||
buildFixture(example); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { buildFixture, setupBrowser } from './utils/build'; | ||
import { IPage } from './utils/browser'; | ||
|
||
const example = 'basic-spa'; | ||
let page: IPage = null; | ||
let browser = null; | ||
|
||
buildFixture(example); | ||
|
||
test('open /', async () => { | ||
const res = await setupBrowser({ example }); | ||
page = res.page; | ||
browser = res.page; | ||
expect(await page.$$text('h2')).toStrictEqual(['SPA', 'Home Page...1']); | ||
}); | ||
|
||
test('open /dashboard', async () => { | ||
await page.push('#/dashboard'); | ||
// lazy load | ||
expect(await page.html()).toContain('加载中...'); | ||
// wait for render | ||
await page.waitForFunction(`document.getElementsByTagName('h2').length > 1`); | ||
expect(await page.$$text('h2')).toStrictEqual(['SPA', 'Dashboard Page...']); | ||
}); | ||
|
||
afterAll(async () => { | ||
await browser.close(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import * as http from 'http'; | ||
import * as url from 'url'; | ||
import * as fse from 'fs-extra'; | ||
import * as path from 'path'; | ||
import * as puppeteer from 'puppeteer'; | ||
|
||
export interface IPage extends puppeteer.Page { | ||
html?: () => Promise<string>; | ||
$text?: (selector: string, trim?: boolean) => Promise<string|null>; | ||
$$text?: (selector: string, trim?: boolean) => Promise<(string|null)[]>; | ||
$attr?: (selector: string, attr: string) => Promise<string|null>; | ||
$$attr?: (selector: string, attr: string) => Promise<(string|null)[]>; | ||
push?: (url: string, options?: puppeteer.DirectNavigationOptions) => Promise<puppeteer.Response>; | ||
} | ||
|
||
export default class Browser { | ||
private server: http.Server; | ||
private browser: puppeteer.Browser; | ||
private baseUrl: string; | ||
|
||
constructor (cwd: string, port: number) { | ||
this.server = this.createServer(cwd, port); | ||
} | ||
|
||
createServer(cwd: string, port: number) { | ||
return http.createServer((req, res) => { | ||
const requrl: string = req.url || ''; | ||
const pathname = `${cwd}${url.parse(requrl).pathname}`; | ||
if (fse.existsSync(pathname)) { | ||
switch (path.extname(pathname)) { // set HTTP HEAD | ||
case '.html': | ||
res.writeHead(200, { 'Content-Type': 'text/html' }); | ||
break; | ||
case '.js': | ||
res.writeHead(200, { 'Content-Type': 'text/javascript' }); | ||
break; | ||
case '.css': | ||
res.writeHead(200, { 'Content-Type': 'text/css' }); | ||
break; | ||
case '.gif': | ||
res.writeHead(200, { 'Content-Type': 'image/gif' }); | ||
break; | ||
case '.jpg': | ||
res.writeHead(200, { 'Content-Type': 'image/jpeg' }); | ||
break; | ||
case '.png': | ||
res.writeHead(200, { 'Content-Type': 'image/png' }); | ||
break; | ||
default: | ||
res.writeHead(200, { | ||
'Content-Type': 'application/octet-stream', | ||
}); | ||
} | ||
fse.readFile(pathname, (_err, data) => { | ||
res.end(data); | ||
}); | ||
} else { | ||
res.writeHead(404, { 'Content-Type': 'text/html' }); | ||
res.end('<h1>404 Not Found</h1>'); | ||
console.log(`${pathname} Not Found.`); | ||
} | ||
}).listen(port, '127.0.0.1'); | ||
} | ||
|
||
async start () { | ||
this.browser = await puppeteer.launch(); | ||
} | ||
|
||
async close () { | ||
if (!this.browser) { return } | ||
await this.browser.close(); | ||
this.server.close(); | ||
} | ||
|
||
async page (url: string) { | ||
this.baseUrl = url; | ||
if (!this.browser) { throw new Error('Please call start() before page(url)'); } | ||
const page: IPage = await this.browser.newPage(); | ||
await page.goto(url); | ||
page.push = (url, options) => page.goto(`${this.baseUrl}${url}`, options); | ||
page.html = () => | ||
page.evaluate(() => window.document.documentElement.outerHTML); | ||
page.$text = (selector, trim) => page.$eval(selector, (el, trim) => { | ||
return trim ? (el.textContent || '').replace(/^\s+|\s+$/g, '') : el.textContent | ||
}, trim); | ||
page.$$text = (selector, trim) => | ||
page.$$eval(selector, (els, trim) => els.map((el) => { | ||
return trim ? (el.textContent || '').replace(/^\s+|\s+$/g, '') : el.textContent | ||
}), trim); | ||
page.$attr = (selector, attr) => | ||
page.$eval(selector, (el, attr: string) => el.getAttribute(attr), attr); | ||
page.$$attr = (selector, attr) => | ||
page.$$eval( | ||
selector, | ||
(els, attr: string) => els.map(el => el.getAttribute(attr)), | ||
attr | ||
); | ||
return page; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import * as path from 'path'; | ||
import { build } from '@alib/build-scripts'; | ||
import * as getPort from 'get-port'; | ||
import Browser, { IPage } from './browser'; | ||
import getBuiltInPlugins = require('../../packages/icejs/src/getBuiltInPlugins'); | ||
|
||
interface ISetupBrowser { | ||
(options: { | ||
example: string; | ||
outputDir?: string; | ||
defaultHtml?: string; | ||
}): Promise<IReturn>; | ||
} | ||
|
||
interface IReturn { | ||
page: IPage; | ||
browser: Browser; | ||
} | ||
|
||
// get builtIn plugins | ||
export const buildFixture = function(example: string) { | ||
test(`setup ${example}`, async () => { | ||
const rootDir = path.join(__dirname, `../../examples/${example}`); | ||
await build({ | ||
args: { | ||
config: path.join(rootDir, 'build.json'), | ||
}, | ||
rootDir, | ||
getBuiltInPlugins: (userConfig) => { | ||
return getBuiltInPlugins(userConfig).concat(require.resolve('./test-plugin')); | ||
}, | ||
}); | ||
}, 120000); | ||
} | ||
|
||
export const setupBrowser: ISetupBrowser = async (options) => { | ||
const { example, outputDir = 'build', defaultHtml = 'index.html' } = options; | ||
const rootDir = path.join(__dirname, `../../examples/${example}`); | ||
const port = await getPort(); | ||
const browser = new Browser(path.join(rootDir, outputDir), port); | ||
await browser.start(); | ||
const page = await browser.page(`http://127.0.0.1:${port}/${defaultHtml}`); | ||
return { | ||
browser, | ||
page, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export default ({ modifyUserConfig }) => { | ||
// disable minify to speed-up fixture builds | ||
modifyUserConfig('minify', false); | ||
} |
Oops, something went wrong.