|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +const webpack = require("webpack"); |
| 4 | +const { test } = require("@playwright/test"); |
| 5 | +const { describe } = require("@playwright/test"); |
| 6 | +const { expect } = require("@playwright/test"); |
| 7 | +const { beforeEach, afterEach } = require("@playwright/test"); |
| 8 | +const Server = require("../../lib/Server"); |
| 9 | +const config = require("../fixtures/mime-types-config/webpack.config"); |
| 10 | +const port = require("../ports-map")["mime-types-option"]; |
| 11 | + |
| 12 | +describe("mimeTypes option", () => { |
| 13 | + describe("as an object with a remapped type", () => { |
| 14 | + let compiler; |
| 15 | + let server; |
| 16 | + let pageErrors; |
| 17 | + let consoleMessages; |
| 18 | + |
| 19 | + beforeEach(async () => { |
| 20 | + compiler = webpack(config); |
| 21 | + |
| 22 | + server = new Server( |
| 23 | + { |
| 24 | + devMiddleware: { |
| 25 | + mimeTypes: { |
| 26 | + js: "text/plain", |
| 27 | + }, |
| 28 | + }, |
| 29 | + port, |
| 30 | + }, |
| 31 | + compiler, |
| 32 | + ); |
| 33 | + |
| 34 | + await server.start(); |
| 35 | + |
| 36 | + pageErrors = []; |
| 37 | + consoleMessages = []; |
| 38 | + }); |
| 39 | + |
| 40 | + afterEach(async () => { |
| 41 | + await server.stop(); |
| 42 | + }); |
| 43 | + |
| 44 | + test("should request file with different js mime type", async ({ |
| 45 | + page, |
| 46 | + }) => { |
| 47 | + page |
| 48 | + .on("console", (message) => { |
| 49 | + consoleMessages.push(message); |
| 50 | + }) |
| 51 | + .on("pageerror", (error) => { |
| 52 | + pageErrors.push(error); |
| 53 | + }); |
| 54 | + |
| 55 | + const response = await page.goto(`http://127.0.0.1:${port}/main.js`, { |
| 56 | + waitUntil: "networkidle0", |
| 57 | + }); |
| 58 | + |
| 59 | + expect(JSON.stringify(response.status())).toMatchSnapshot(); |
| 60 | + |
| 61 | + expect( |
| 62 | + JSON.stringify(response.headers()["content-type"]), |
| 63 | + ).toMatchSnapshot(); |
| 64 | + |
| 65 | + expect( |
| 66 | + JSON.stringify(consoleMessages.map((message) => message.text())), |
| 67 | + ).toMatchSnapshot(); |
| 68 | + |
| 69 | + expect(JSON.stringify(pageErrors)).toMatchSnapshot(); |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + describe("as an object with a custom type", () => { |
| 74 | + let compiler; |
| 75 | + let server; |
| 76 | + let pageErrors; |
| 77 | + let consoleMessages; |
| 78 | + |
| 79 | + beforeEach(async () => { |
| 80 | + compiler = webpack(config); |
| 81 | + |
| 82 | + server = new Server( |
| 83 | + { |
| 84 | + devMiddleware: { |
| 85 | + mimeTypes: { |
| 86 | + custom: "text/html", |
| 87 | + }, |
| 88 | + }, |
| 89 | + port, |
| 90 | + }, |
| 91 | + compiler, |
| 92 | + ); |
| 93 | + |
| 94 | + await server.start(); |
| 95 | + |
| 96 | + pageErrors = []; |
| 97 | + consoleMessages = []; |
| 98 | + }); |
| 99 | + |
| 100 | + afterEach(async () => { |
| 101 | + await server.stop(); |
| 102 | + }); |
| 103 | + |
| 104 | + test("should request file with different js mime type", async ({ |
| 105 | + page, |
| 106 | + }) => { |
| 107 | + page |
| 108 | + .on("console", (message) => { |
| 109 | + consoleMessages.push(message); |
| 110 | + }) |
| 111 | + .on("pageerror", (error) => { |
| 112 | + pageErrors.push(error); |
| 113 | + }); |
| 114 | + |
| 115 | + const response = await page.goto(`http://127.0.0.1:${port}/file.custom`, { |
| 116 | + waitUntil: "networkidle0", |
| 117 | + }); |
| 118 | + |
| 119 | + expect(JSON.stringify(response.status())).toMatchSnapshot(); |
| 120 | + |
| 121 | + expect( |
| 122 | + JSON.stringify(response.headers()["content-type"]), |
| 123 | + ).toMatchSnapshot(); |
| 124 | + |
| 125 | + expect( |
| 126 | + JSON.stringify(consoleMessages.map((message) => message.text())), |
| 127 | + ).toMatchSnapshot(); |
| 128 | + |
| 129 | + expect(JSON.stringify(pageErrors)).toMatchSnapshot(); |
| 130 | + }); |
| 131 | + }); |
| 132 | +}); |
0 commit comments