diff --git a/.gitignore b/.gitignore index 9ff9d51..00066b1 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,6 @@ yarn.lock lib # Common -tmp +.tmp-* cache *.log diff --git a/package.json b/package.json index 43c8925..ca8a072 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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" }, diff --git a/test/loader.spec.js b/test/loader.spec.js index 5918cf7..b325fdc 100644 --- a/test/loader.spec.js +++ b/test/loader.spec.js @@ -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') @@ -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 () => { @@ -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) + ]) }) diff --git a/test/runtime.js b/test/runtime.js new file mode 100644 index 0000000..61aaccb --- /dev/null +++ b/test/runtime.js @@ -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)) +}