Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add component render test #6

Merged
merged 1 commit into from
Dec 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ yarn.lock
lib

# Common
tmp
.tmp-*
cache
*.log
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
"@babel/core": "^7.7.2",
"@babel/preset-env": "^7.7.1",
"@types/jest": "^24.0.23",
"@vue/server-test-utils": "^1.0.0-beta.30",
"@vue/test-utils": "^1.0.0-beta.30",
"babel-jest": "^24.9.0",
"husky": "^3.0.9",
"jest": "^24.9.0",
Expand All @@ -50,6 +52,7 @@
"vue": "^2.6.10",
"vue-docgen-api": "^4.1.1",
"vue-loader": "^15.7.2",
"vue-server-renderer": "^2.6.10",
"vue-template-compiler": "^2.6.10",
"webpack": "^4.41.2"
},
Expand Down
10 changes: 10 additions & 0 deletions test/loader.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import compiler from './compiler'
import { setup, renderComponent } from './runtime'

setup()

it('Injects docgen result as __docgenInfo property', async () => {
const stats = await compiler('./fixtures/basic-sfc.vue')
Expand All @@ -21,6 +24,8 @@ it('Injects docgen result for non-SFC', async () => {

expect(output).toMatch(docgenPattern)
expect(JSON.parse(output.match(docgenPattern)[1])).toMatchSnapshot()

await renderComponent(output, fixture, mod => mod.MyButton)
})

it('Injects docgen result non-SFC with multiple exports', async () => {
Expand All @@ -41,4 +46,9 @@ it('Injects docgen result non-SFC with multiple exports', async () => {
expect(output).toMatch(match2)
expect(JSON.parse(output.match(match1)[1])).toMatchSnapshot()
expect(JSON.parse(output.match(match2)[1])).toMatchSnapshot()

await Promise.all([
renderComponent(output, fixture, mod => mod.MyButton1),
renderComponent(output, fixture, mod => mod.MyButton2)
])
})
34 changes: 34 additions & 0 deletions test/runtime.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import fs from 'fs'
import path from 'path'

import { render } from '@vue/server-test-utils'

const tmpDirSuffix = (Math.random() * 1000).toString(36)
const tmpDir = path.resolve(__dirname, `./.tmp-${tmpDirSuffix}`)

export function setup() {
beforeAll(async () => {
await fs.promises.mkdir(tmpDir)
})

afterAll(async () => {
await fs.promises.rmdir(tmpDir, {
recursive: true
})
})
}

export async function renderComponent(
code,
filename,
getComponent = mod => mod.default
) {
const filepath = path.resolve(tmpDir, path.basename(filename))

await fs.promises.writeFile(filepath, code)

const mod = require(filepath)

// Make sure the transformed code works
await render(getComponent(mod))
}