Skip to content

Commit

Permalink
feat: e2e 测试时,输出到每个case中output下的随机目录中
Browse files Browse the repository at this point in the history
  • Loading branch information
meixg committed Dec 3, 2021
1 parent e80ff4f commit 4b7a5d9
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ vendor/
/dist/
test/cases/*/ssr.js
test/cases/*/ssr.php
test/cases/*/output/*
test/cases/*/output
.vscode
/example
bin/output.js
Expand Down
22 changes: 11 additions & 11 deletions src/fixtures/case.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export function readExpected (caseName: string, caseRoot: string) {
return readFileSync(htmlPath, 'utf8')
}

export function compileJS (caseName: string, caseRoot: string, compileToFunctionBodyCode: boolean = false) {
export function compileJS (caseName: string, caseRoot: string, compileToFunctionBodyCode: boolean = false, folderName = '') {
debug('compile js', caseName)
const caseDir = join(caseRoot, caseName)
const ssrOnly = /-so/.test(caseName)
Expand All @@ -63,15 +63,15 @@ export function compileJS (caseName: string, caseRoot: string, compileToFunction
ToJSCompiler,
{ ssrOnly, bareFunctionBody: compileToFunctionBodyCode, importHelpers }
)
mkdirp.sync(join(caseRoot, caseName, 'output'))
mkdirp.sync(join(caseRoot, caseName, 'output', folderName))
const targetFile = file === 'component.js'
? join(caseRoot, caseName, 'output/ssr.js')
: join(caseRoot, caseName, 'output', file)
? join(caseRoot, caseName, 'output', folderName, 'ssr.js')
: join(caseRoot, caseName, 'output', folderName, file)
writeFileSync(targetFile, targetCode)
}
}

export function compileComponent (caseName: string, caseRoot: string, compileToFunctionBodyCode: boolean = false) {
export function compileComponent (caseName: string, caseRoot: string, compileToFunctionBodyCode: boolean = false, folderName = '') {
debug('compile js', caseName)
const caseDir = join(caseRoot, caseName)
const jsFile = join(caseDir, 'component.js')
Expand All @@ -82,12 +82,12 @@ export function compileComponent (caseName: string, caseRoot: string, compileToF
ToJSCompiler,
{ ssrOnly, bareFunctionBody: compileToFunctionBodyCode, importHelpers }
)
mkdirp.sync(join(caseRoot, caseName, 'output'))
const targetFile = join(caseRoot, caseName, 'output/ssr.js')
mkdirp.sync(join(caseRoot, caseName, 'output', folderName))
const targetFile = join(caseRoot, caseName, 'output', folderName, 'ssr.js')
return compileToFunctionBodyCode ? targetCode : writeFileSync(targetFile, targetCode)
}

export function compileTS (caseName: string, caseRoot: string) {
export function compileTS (caseName: string, caseRoot: string, folderName = '') {
debug('compile ts', caseName)
const caseDir = join(caseRoot, caseName)
const ssrOnly = /-so/.test(caseName)
Expand All @@ -97,10 +97,10 @@ export function compileTS (caseName: string, caseRoot: string) {
ToJSCompiler,
{ ssrOnly, importHelpers }
)
mkdirp.sync(join(caseRoot, caseName, 'output'))
mkdirp.sync(join(caseRoot, caseName, 'output', folderName))
const targetFile = file === 'component.ts'
? join(caseDir, 'output', 'ssr.js')
: join(caseDir, 'output', file.replace(/\.ts$/, '.js'))
? join(caseDir, 'output', folderName, 'ssr.js')
: join(caseDir, 'output', folderName, file.replace(/\.ts$/, '.js'))
writeFileSync(targetFile, targetCode)
}
}
Expand Down
27 changes: 20 additions & 7 deletions test/e2e.spec.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
import { ls, compileComponent, compileJS, jsExists, tsExists, compileTS, getRenderArguments, readExpected, renderOnthefly } from '../src/fixtures/case'
import { ls, compileComponent, compileJS, jsExists, tsExists, compileTS, getRenderArguments, readExpected, renderOnthefly, caseRoots } from '../src/fixtures/case'
import { join } from 'path'
import { parseSanHTML } from '../src/index'
import { existsSync } from 'fs'
import { execSync } from 'child_process'

// 每次执行前,把之前的产物删掉
caseRoots.forEach(caseRoot => {
execSync(`rm -rf ${caseRoot}/**/output`)
})

for (const { caseName, caseRoot } of ls()) {
const [expectedData, expectedHtml] = parseSanHTML(readExpected(caseName, caseRoot))

if (tsExists(caseName, caseRoot)) {
it('render to source (TypeScript): ' + caseName, async function () {
compileTS(caseName, caseRoot)
const render = require(join(caseRoot, caseName, 'output', 'ssr.js'))
const folderName = getRandomStr() + '_tssrc'
compileTS(caseName, caseRoot, folderName)
const render = require(join(caseRoot, caseName, 'output', folderName, 'ssr.js'))
const got = render(...getRenderArguments(caseName, caseRoot))
const [data, html] = parseSanHTML(got)

Expand All @@ -20,8 +27,9 @@ for (const { caseName, caseRoot } of ls()) {

if (jsExists(caseName, caseRoot)) {
it('js to source: ' + caseName, async function () {
compileJS(caseName, caseRoot)
const render = require(join(caseRoot, caseName, 'output/ssr.js'))
const folderName = getRandomStr() + '_jssrc'
compileJS(caseName, caseRoot, false, folderName)
const render = require(join(caseRoot, caseName, 'output', folderName, 'ssr.js'))
const ssrSpecPath = join(caseRoot, `${caseName}/ssr-spec.js`)
if (existsSync(ssrSpecPath)) {
require(ssrSpecPath)
Expand All @@ -35,9 +43,10 @@ for (const { caseName, caseRoot } of ls()) {
})

it('component to source: ' + caseName, async function () {
compileComponent(caseName, caseRoot)
const folderName = getRandomStr() + '_comsrc'
compileComponent(caseName, caseRoot, false, folderName)
// eslint-disable-next-line
const render = require(join(caseRoot, caseName, 'output/ssr.js'))
const render = require(join(caseRoot, caseName, 'output', folderName, 'ssr.js'))
// 测试在 strict mode,因此需要手动传入 require
const got = render(...getRenderArguments(caseName, caseRoot))
const [data, html] = parseSanHTML(got)
Expand All @@ -55,3 +64,7 @@ for (const { caseName, caseRoot } of ls()) {
})
}
}

function getRandomStr () {
return Math.random().toString(36).slice(2)
}

0 comments on commit 4b7a5d9

Please sign in to comment.