Skip to content

Commit

Permalink
refactor: automate integration test for both js and php
Browse files Browse the repository at this point in the history
  • Loading branch information
harttle committed Aug 29, 2019
1 parent 6ba4b91 commit 639b587
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 34 deletions.
17 changes: 2 additions & 15 deletions bin/test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#!/usr/bin/env node

const chalk = require('chalk')
const { spawnSync } = require('child_process')
const { readFileSync, writeFileSync } = require('fs')
const compileToJSSource = require('../src/js-ssr').compileToSource
const compileToPHPSource = require('../src/php-ssr').compileToSource
const { resolve, join } = require('path')
const caseRoot = resolve(__dirname, '../test/cases')
const { render } = require('../src/render')

const caseName = process.argv[2]
const caseDir = join(caseRoot, caseName)
Expand All @@ -16,6 +16,7 @@ const phpSSRPath = join(caseDir, 'ssr.php')
const compPath = join(caseDir, 'component.js')

// generate js ssr
delete require.cache[require.resolve(compPath)]
const ComponentClass = require(compPath)
const fn = compileToJSSource(ComponentClass)
writeFileSync(jsSSRPath, `module.exports = ${fn}`)
Expand All @@ -42,17 +43,3 @@ function check (title, html) {
console.log()
if (html !== expected) process.exit(1)
}

function render (caseName, target) {
const bin = resolve(__dirname, `./render.${target}`)
const proc = spawnSync(bin, [caseName])
if (proc.error || proc.stderr.length) {
if (proc.error) console.error(proc.error)
else {
console.log('STDOUT:', proc.stdout.toString())
console.error('STDERR:', proc.stderr.toString())
}
process.exit(1)
}
return proc.stdout.toString()
}
13 changes: 13 additions & 0 deletions src/render.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const { spawnSync } = require('child_process')
const { resolve } = require('path')

function render (caseName, target) {
const bin = resolve(__dirname, `../bin/render.${target}`)
const proc = spawnSync(bin, [caseName])
if (proc.error || proc.stderr.length) {
throw proc.error || new Error(proc.stderr.toString())
}
return proc.stdout.toString()
}

exports.render = render
37 changes: 18 additions & 19 deletions test/integration.spec.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
const { readFileSync, readdirSync } = require('fs')
const san = require('../src/php-ssr')
const { existsSync, readFileSync, readdirSync } = require('fs')
const { resolve, join } = require('path')
const { read } = require('../src/data')
const { render } = require('../src/render')
const caseRoot = resolve(__dirname, 'cases')
const files = readdirSync(caseRoot)
const notExist = []

for (const dir of files) {
const caseDir = resolve(caseRoot, dir)
const expected = readFileSync(join(caseDir, 'result.html'), 'utf8')
const component = join(caseDir, 'component.js')
const data = join(caseDir, 'data.json')
const noDataOutput = /-ndo$/.test(caseDir)
const htmlPath = join(caseDir, 'result.html')
const phpPath = join(caseDir, 'ssr.php')
const expected = readFileSync(htmlPath, 'utf8')

// if (dir === 'load-success')
it(dir, function () {
expect(render(component, noDataOutput, data)).toBe(expected)
// if (dir !== 'load-success') continue

it('js: ' + dir, function () {
expect(render(dir, 'js')).toBe(expected)
})
if (!existsSync(phpPath)) {
notExist.push(dir)
continue
}
it('php: ' + dir, function () {
expect(render(dir, 'php')).toBe(expected)
})
}

function render (component, noDataOutput, datafile) {
const ComponentClass = require(component)

const renderer = san.compileToRenderer(ComponentClass)
const componentData = read(datafile)

const html = renderer(componentData, noDataOutput)
return html
}
console.log(`${notExist.length} cases ssr.php not found: ${notExist.join(',')}`)

0 comments on commit 639b587

Please sign in to comment.