|
| 1 | +jest.setTimeout(50000) |
| 2 | + |
| 3 | +const fs = require('fs-extra') |
| 4 | +const path = require('path') |
| 5 | +const portfinder = require('portfinder') |
| 6 | +const { createServer } = require('http-server') |
| 7 | +const { defaultPreset } = require('@vue/cli/lib/options') |
| 8 | +const create = require('@vue/cli-test-utils/createTestProject') |
| 9 | +const launchPuppeteer = require('@vue/cli-test-utils/launchPuppeteer') |
| 10 | + |
| 11 | +let server, browser |
| 12 | +test('modern mode', async () => { |
| 13 | + const project = await create('modern-mode', defaultPreset) |
| 14 | + |
| 15 | + await project.write('vue.config.js', ` |
| 16 | + module.exports = { |
| 17 | + modernMode: true |
| 18 | + } |
| 19 | + `) |
| 20 | + |
| 21 | + const { stdout } = await project.run('vue-cli-service build') |
| 22 | + expect(stdout).toMatch('Build complete.') |
| 23 | + |
| 24 | + // assert correct bundle files |
| 25 | + const files = await fs.readdir(path.join(project.dir, 'dist/js')) |
| 26 | + expect(files.some(f => /^app\.\w{8}\.js$/.test(f))).toBe(true) |
| 27 | + expect(files.some(f => /^app-legacy\.\w{8}\.js$/.test(f))).toBe(true) |
| 28 | + expect(files.some(f => /^chunk-vendors\.\w{8}\.js$/.test(f))).toBe(true) |
| 29 | + expect(files.some(f => /^chunk-vendors-legacy\.\w{8}\.js$/.test(f))).toBe(true) |
| 30 | + |
| 31 | + // assert correct asset links |
| 32 | + const index = await project.read('dist/index.html') |
| 33 | + |
| 34 | + // should use <script type="module"> for modern bundle |
| 35 | + expect(index).toMatch(/<script type=module src=\/js\/chunk-vendors\.\w{8}\.js>/) |
| 36 | + expect(index).toMatch(/<script type=module src=\/js\/app\.\w{8}\.js>/) |
| 37 | + |
| 38 | + // should use <link rel="modulepreload"> for modern bundle |
| 39 | + expect(index).toMatch(/<link [^>]*js\/chunk-vendors\.\w{8}\.js rel=modulepreload>/) |
| 40 | + expect(index).toMatch(/<link [^>]*js\/app\.\w{8}\.js rel=modulepreload>/) |
| 41 | + |
| 42 | + // should use <script nomodule> for legacy bundle |
| 43 | + expect(index).toMatch(/<script src=\/js\/chunk-vendors-legacy\.\w{8}\.js nomodule>/) |
| 44 | + expect(index).toMatch(/<script src=\/js\/app-legacy\.\w{8}\.js nomodule>/) |
| 45 | + |
| 46 | + // should inject Safari 10 nomodule fix |
| 47 | + const { safariFix } = require('../lib/webpack/ModernModePlugin') |
| 48 | + expect(index).toMatch(`<script>${safariFix}</script>`) |
| 49 | + |
| 50 | + // start server and ensure the page loads properly |
| 51 | + const port = await portfinder.getPortPromise() |
| 52 | + server = createServer({ root: path.join(project.dir, 'dist') }) |
| 53 | + |
| 54 | + await new Promise((resolve, reject) => { |
| 55 | + server.listen(port, err => { |
| 56 | + if (err) return reject(err) |
| 57 | + resolve() |
| 58 | + }) |
| 59 | + }) |
| 60 | + |
| 61 | + const url = `http://localhost:${port}/` |
| 62 | + const launched = await launchPuppeteer(url) |
| 63 | + browser = launched.browser |
| 64 | + const page = launched.page |
| 65 | + |
| 66 | + const getH1Text = async () => page.evaluate(() => { |
| 67 | + return document.querySelector('h1').textContent |
| 68 | + }) |
| 69 | + |
| 70 | + expect(await getH1Text()).toMatch('Welcome to Your Vue.js App') |
| 71 | +}) |
| 72 | + |
| 73 | +afterAll(async () => { |
| 74 | + if (browser) { |
| 75 | + await browser.close() |
| 76 | + } |
| 77 | + if (server) { |
| 78 | + server.close() |
| 79 | + } |
| 80 | +}) |
0 commit comments