Skip to content

Commit 3eb1710

Browse files
pimlieclarkdo
authored andcommitted
test(vue-app): add template compiler helper (nuxt#6299)
1 parent facc056 commit 3eb1710

File tree

3 files changed

+77
-7
lines changed

3 files changed

+77
-7
lines changed

packages/builder/src/builder.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,10 @@ export default class Builder {
213213
return `${path}/**/*.{${this.supportedExtensions.join(',')}}`
214214
}
215215

216+
createTemplateContext () {
217+
return new TemplateContext(this, this.options)
218+
}
219+
216220
async generateRoutesAndFiles () {
217221
consola.debug('Generating nuxt files')
218222

@@ -223,7 +227,7 @@ export default class Builder {
223227
// Plugins
224228
this.plugins = Array.from(this.normalizePlugins())
225229

226-
const templateContext = new TemplateContext(this, this.options)
230+
const templateContext = this.createTemplateContext()
227231

228232
await Promise.all([
229233
this.resolveLayouts(templateContext),
@@ -527,6 +531,7 @@ export default class Builder {
527531

528532
// Render template to dst
529533
const fileContent = await fsExtra.readFile(src, 'utf8')
534+
530535
let content
531536
try {
532537
const templateFunction = template(fileContent, templateOptions)

packages/vue-app/test/__utils__/index.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,71 @@
1+
import path from 'path'
2+
import { remove } from 'fs-extra'
3+
import consola from 'consola'
4+
import { getNuxtConfig, Nuxt, Builder } from '../../../../test/utils'
5+
6+
const rootDir = path.resolve(__dirname, '..')
7+
8+
export async function compileTemplate (template, destination, options = {}) {
9+
if (arguments.length < 3) {
10+
options = destination || {}
11+
destination = undefined
12+
}
13+
14+
const config = getNuxtConfig(options)
15+
16+
config.rootDir = rootDir
17+
config.dev = false
18+
config.test = false
19+
config.server = false
20+
21+
const nuxt = new Nuxt(config)
22+
const builder = new Builder(nuxt)
23+
24+
const templateContext = builder.createTemplateContext()
25+
26+
const multipleTemplates = Array.isArray(template)
27+
if (!multipleTemplates) {
28+
template = [template]
29+
}
30+
31+
templateContext.templateFiles = template.map((template) => {
32+
if (typeof template === 'string') {
33+
return {
34+
src: path.resolve(rootDir, '../template', template),
35+
dst: path.join(rootDir, '.nuxt', path.basename(template)),
36+
custom: false
37+
}
38+
}
39+
40+
return {
41+
src: path.resolve(rootDir, '../template', template.src),
42+
dst: path.join(rootDir, '.nuxt', template.dst),
43+
custom: template.custom
44+
}
45+
})
46+
47+
try {
48+
// clear all destinations
49+
await Promise.all(templateContext.templateFiles.map(({ dst }) => remove(dst)))
50+
51+
await builder.compileTemplates(templateContext)
52+
53+
if (multipleTemplates) {
54+
return templateContext.templateFiles.map(template => template.dst)
55+
}
56+
57+
const [template] = templateContext.templateFiles
58+
return template.dst
59+
} catch (err) {
60+
consola.error(`Could not compile template`, err.message, template)
61+
return false
62+
}
63+
}
64+
65+
export function importComponent (componentPath) {
66+
return import(componentPath).then(m => m.default || m)
67+
}
68+
169
export const vmTick = (vm) => {
270
return new Promise((resolve) => {
371
vm.$nextTick(resolve)

packages/vue-app/test/nuxt-loading.test.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
/**
22
* @jest-environment jsdom
33
*/
4-
import { resolve } from 'path'
54
import { mount, createLocalVue } from '@vue/test-utils'
65
import { renderToString } from '@vue/server-test-utils'
7-
import { loadFixture } from '../../../test/utils'
8-
import { vmTick } from './__utils__'
6+
import { compileTemplate, importComponent, vmTick } from './__utils__'
97

108
jest.useFakeTimers()
119

@@ -14,11 +12,10 @@ describe('nuxt-loading', () => {
1412
let Component
1513

1614
beforeAll(async () => {
17-
const config = await loadFixture('basic')
18-
const componentDir = resolve(config.rootDir, '.nuxt/components')
15+
const compiledTemplate = await compileTemplate('components/nuxt-loading.vue')
16+
Component = await importComponent(compiledTemplate)
1917

2018
localVue = createLocalVue()
21-
Component = (await import(resolve(componentDir, 'nuxt-loading.vue'))).default
2219
})
2320

2421
afterEach(() => jest.clearAllTimers())

0 commit comments

Comments
 (0)