|
| 1 | +jest.setTimeout(30000) |
| 2 | + |
| 3 | +const path = require('path') |
| 4 | +const portfinder = require('portfinder') |
| 5 | +const { defaultPreset } = require('@vue/cli/lib/options') |
| 6 | +const { createServer } = require('http-server') |
| 7 | +const create = require('@vue/cli-test-utils/createTestProject') |
| 8 | +const serve = require('@vue/cli-test-utils/serveWithPuppeteer') |
| 9 | +const launchPuppeteer = require('@vue/cli-test-utils/launchPuppeteer') |
| 10 | + |
| 11 | +async function makeProjectMultiPage (project) { |
| 12 | + await project.write('vue.config.js', ` |
| 13 | + module.exports = { |
| 14 | + pages: { |
| 15 | + index: { entry: 'src/main.js' }, |
| 16 | + foo: { entry: 'src/foo.js' }, |
| 17 | + bar: { entry: 'src/bar.js' } |
| 18 | + }, |
| 19 | + chainWebpack: config => { |
| 20 | + const splitOptions = config.optimization.get('splitChunks') |
| 21 | + config.optimization.splitChunks(Object.assign({}, splitOptions, { |
| 22 | + minSize: 10000 |
| 23 | + })) |
| 24 | + } |
| 25 | + } |
| 26 | + `) |
| 27 | + await project.write('src/foo.js', ` |
| 28 | + import Vue from 'vue' |
| 29 | + new Vue({ |
| 30 | + el: '#app', |
| 31 | + render: h => h('h1', 'Foo') |
| 32 | + }) |
| 33 | + `) |
| 34 | + await project.write('src/bar.js', ` |
| 35 | + import Vue from 'vue' |
| 36 | + import App from './App.vue' |
| 37 | + new Vue({ |
| 38 | + el: '#app', |
| 39 | + render: h => h(App) |
| 40 | + }) |
| 41 | + `) |
| 42 | + const app = await project.read('src/App.vue') |
| 43 | + await project.write('src/App.vue', app.replace( |
| 44 | + `import HelloWorld from './components/HelloWorld.vue'`, |
| 45 | + `const HelloWorld = () => import('./components/HelloWorld.vue')` |
| 46 | + )) |
| 47 | +} |
| 48 | + |
| 49 | +test('serve w/ multi page', async () => { |
| 50 | + const project = await create('e2e-multi-page-serve', defaultPreset) |
| 51 | + |
| 52 | + await makeProjectMultiPage(project) |
| 53 | + |
| 54 | + await serve( |
| 55 | + () => project.run('vue-cli-service serve'), |
| 56 | + async ({ page, url, helpers }) => { |
| 57 | + expect(await helpers.getText('h1')).toMatch(`Welcome to Your Vue.js App`) |
| 58 | + |
| 59 | + await page.goto(`${url}/foo.html`) |
| 60 | + expect(await helpers.getText('h1')).toMatch(`Foo`) |
| 61 | + |
| 62 | + await page.goto(`${url}/bar.html`) |
| 63 | + expect(await helpers.getText('h1')).toMatch(`Welcome to Your Vue.js App`) |
| 64 | + } |
| 65 | + ) |
| 66 | +}) |
| 67 | + |
| 68 | +let server, browser, page |
| 69 | +test('build w/ multi page', async () => { |
| 70 | + const project = await create('e2e-multi-page-build', defaultPreset) |
| 71 | + |
| 72 | + await makeProjectMultiPage(project) |
| 73 | + |
| 74 | + const { stdout } = await project.run('vue-cli-service build') |
| 75 | + expect(stdout).toMatch('Build complete.') |
| 76 | + |
| 77 | + // should generate the HTML pages |
| 78 | + expect(project.has('dist/index.html')).toBe(true) |
| 79 | + expect(project.has('dist/foo.html')).toBe(true) |
| 80 | + expect(project.has('dist/bar.html')).toBe(true) |
| 81 | + |
| 82 | + const assertSharedAssets = file => { |
| 83 | + // should split and preload vendor chunk |
| 84 | + expect(file).toMatch(/<link [^>]+js\/chunk-vendors[^>]+\.js rel=preload>/) |
| 85 | + // should split and preload common js and css |
| 86 | + expect(file).toMatch(/<link [^>]+js\/chunk-common[^>]+\.js rel=preload>/) |
| 87 | + expect(file).toMatch(/<link [^>]+chunk-common[^>]+\.css rel=preload>/) |
| 88 | + // should load common css |
| 89 | + expect(file).toMatch(/<link href=\/css\/chunk-common\.\w+\.css rel=stylesheet>/) |
| 90 | + // should load common js |
| 91 | + expect(file).toMatch(/<script [^>]+src=\/js\/chunk-vendors\.\w+\.js>/) |
| 92 | + expect(file).toMatch(/<script [^>]+src=\/js\/chunk-common\.\w+\.js>/) |
| 93 | + } |
| 94 | + |
| 95 | + const index = await project.read('dist/index.html') |
| 96 | + assertSharedAssets(index) |
| 97 | + // should preload correct page file |
| 98 | + expect(index).toMatch(/<link [^>]+js\/index[^>]+\.js rel=preload>/) |
| 99 | + expect(index).not.toMatch(/<link [^>]+js\/foo[^>]+\.js rel=preload>/) |
| 100 | + expect(index).not.toMatch(/<link [^>]+js\/bar[^>]+\.js rel=preload>/) |
| 101 | + // should prefetch async chunk js and css |
| 102 | + expect(index).toMatch(/<link [^>]+css\/0\.\w+\.css rel=prefetch>/) |
| 103 | + expect(index).toMatch(/<link [^>]+js\/0\.\w+\.js rel=prefetch>/) |
| 104 | + // should load correct page js |
| 105 | + expect(index).toMatch(/<script [^>]+src=\/js\/index\.\w+\.js>/) |
| 106 | + expect(index).not.toMatch(/<script [^>]+src=\/js\/foo\.\w+\.js>/) |
| 107 | + expect(index).not.toMatch(/<script [^>]+src=\/js\/bar\.\w+\.js>/) |
| 108 | + |
| 109 | + const foo = await project.read('dist/foo.html') |
| 110 | + assertSharedAssets(foo) |
| 111 | + // should preload correct page file |
| 112 | + expect(foo).not.toMatch(/<link [^>]+js\/index[^>]+\.js rel=preload>/) |
| 113 | + expect(foo).toMatch(/<link [^>]+js\/foo[^>]+\.js rel=preload>/) |
| 114 | + expect(foo).not.toMatch(/<link [^>]+js\/bar[^>]+\.js rel=preload>/) |
| 115 | + // should not prefetch async chunk js and css because it's not used by |
| 116 | + // this entry |
| 117 | + expect(foo).not.toMatch(/<link [^>]+css\/0\.\w+\.css rel=prefetch>/) |
| 118 | + expect(foo).not.toMatch(/<link [^>]+js\/0\.\w+\.js rel=prefetch>/) |
| 119 | + // should load correct page js |
| 120 | + expect(foo).not.toMatch(/<script [^>]+src=\/js\/index\.\w+\.js>/) |
| 121 | + expect(foo).toMatch(/<script [^>]+src=\/js\/foo\.\w+\.js>/) |
| 122 | + expect(foo).not.toMatch(/<script [^>]+src=\/js\/bar\.\w+\.js>/) |
| 123 | + |
| 124 | + const bar = await project.read('dist/bar.html') |
| 125 | + assertSharedAssets(bar) |
| 126 | + // should preload correct page file |
| 127 | + expect(bar).not.toMatch(/<link [^>]+js\/index[^>]+\.js rel=preload>/) |
| 128 | + expect(bar).not.toMatch(/<link [^>]+js\/foo[^>]+\.js rel=preload>/) |
| 129 | + expect(bar).toMatch(/<link [^>]+js\/bar[^>]+\.js rel=preload>/) |
| 130 | + // should prefetch async chunk js and css |
| 131 | + expect(bar).toMatch(/<link [^>]+css\/0\.\w+\.css rel=prefetch>/) |
| 132 | + expect(bar).toMatch(/<link [^>]+js\/0\.\w+\.js rel=prefetch>/) |
| 133 | + // should load correct page js |
| 134 | + expect(bar).not.toMatch(/<script [^>]+src=\/js\/index\.\w+\.js>/) |
| 135 | + expect(bar).not.toMatch(/<script [^>]+src=\/js\/foo\.\w+\.js>/) |
| 136 | + expect(bar).toMatch(/<script [^>]+src=\/js\/bar\.\w+\.js>/) |
| 137 | + |
| 138 | + // assert pages work |
| 139 | + const port = await portfinder.getPortPromise() |
| 140 | + server = createServer({ root: path.join(project.dir, 'dist') }) |
| 141 | + |
| 142 | + await new Promise((resolve, reject) => { |
| 143 | + server.listen(port, err => { |
| 144 | + if (err) return reject(err) |
| 145 | + resolve() |
| 146 | + }) |
| 147 | + }) |
| 148 | + |
| 149 | + const url = `http://localhost:${port}/` |
| 150 | + const launched = await launchPuppeteer(url) |
| 151 | + browser = launched.browser |
| 152 | + page = launched.page |
| 153 | + |
| 154 | + const getH1Text = async () => page.evaluate(() => { |
| 155 | + return document.querySelector('h1').textContent |
| 156 | + }) |
| 157 | + |
| 158 | + expect(await getH1Text()).toMatch('Welcome to Your Vue.js App') |
| 159 | + |
| 160 | + await page.goto(`${url}foo.html`) |
| 161 | + expect(await getH1Text()).toMatch('Foo') |
| 162 | + |
| 163 | + await page.goto(`${url}bar.html`) |
| 164 | + expect(await getH1Text()).toMatch('Welcome to Your Vue.js App') |
| 165 | +}) |
| 166 | + |
| 167 | +afterAll(async () => { |
| 168 | + await browser.close() |
| 169 | + server.close() |
| 170 | +}) |
0 commit comments