Skip to content

Commit

Permalink
test: assert the response header in unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
adenvt committed Jul 14, 2023
1 parent 8816598 commit 060c00f
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 7 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nupolyon",
"version": "0.2.0",
"version": "0.2.1",
"packageManager": "yarn@3.6.1",
"description": "Auto-Inject polyfill from polyfill.io",
"license": "MIT",
Expand Down Expand Up @@ -41,6 +41,7 @@
"@nuxt/test-utils": "^3.1.2",
"@types/polyfill-library": "^3.108.1",
"changelogen": "^0.5.0",
"cheerio": "^1.0.0-rc.12",
"eslint": "^8.33.0",
"nuxt": "^3.1.2",
"vitest": "^0.33.0"
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/server/plugins/polyfill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ export default defineNitroPlugin((nitroApp) => {
const host = !isSelfHost ? src : cleanDoubleSlashes(joinURL(config.app.baseURL, src))

if (host) {
const preload = `<link rel="preload" href="${host}" crossorigin="anonymous" as="script" />`
const preload = `<link rel="preload" href="${host}" crossorigin="anonymous" as="script" data-testid="nupolyon-preload" />`
// NOTE: We intentionally omit type="module" here because it will defer the execution of the polyfill
const polyfill = `<script src="${host}" crossorigin="anonymous"></script>`
const polyfill = `<script src="${host}" crossorigin="anonymous" data-testid="nupolyon-script"></script>`

nitroApp.hooks.hook('render:html', (html) => {
html.head.unshift(preload, polyfill) // insert at the beginning of the array
Expand Down
11 changes: 9 additions & 2 deletions test/basic.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
import { describe, it, expect } from 'vitest'
import { fileURLToPath } from 'node:url'
import { setup, fetch, $fetch } from '@nuxt/test-utils'
import cheerio from 'cheerio'

describe('ssr', async () => {
await setup({
rootDir: fileURLToPath(new URL('./fixtures/basic', import.meta.url)),
})

it('should render the index page', async () => {
it('should inject the polyfill script into page', async () => {
// Get response to a server-rendered page with `$fetch`.
const html = await $fetch('/')
expect(html).toContain('<div>basic</div>')
const $ = cheerio.load(html)

const preload = $('link[data-testid="nupolyon-preload"]')
const script = $('script[data-testid="nupolyon-script"]')

expect(preload.attr('href')).toBe('/_nupolyon/polyfill')
expect(script.attr('src')).toBe('/_nupolyon/polyfill')
})

it('should return selfhosted polyfill with correct mimetype', async () => {
Expand Down
65 changes: 63 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2592,6 +2592,35 @@ __metadata:
languageName: node
linkType: hard

"cheerio-select@npm:^2.1.0":
version: 2.1.0
resolution: "cheerio-select@npm:2.1.0"
dependencies:
boolbase: ^1.0.0
css-select: ^5.1.0
css-what: ^6.1.0
domelementtype: ^2.3.0
domhandler: ^5.0.3
domutils: ^3.0.1
checksum: 843d6d479922f28a6c5342c935aff1347491156814de63c585a6eb73baf7bb4185c1b4383a1195dca0f12e3946d737c7763bcef0b9544c515d905c5c44c5308b
languageName: node
linkType: hard

"cheerio@npm:^1.0.0-rc.12":
version: 1.0.0-rc.12
resolution: "cheerio@npm:1.0.0-rc.12"
dependencies:
cheerio-select: ^2.1.0
dom-serializer: ^2.0.0
domhandler: ^5.0.3
domutils: ^3.0.1
htmlparser2: ^8.0.1
parse5: ^7.0.0
parse5-htmlparser2-tree-adapter: ^7.0.0
checksum: 5d4c1b7a53cf22d3a2eddc0aff70cf23cbb30d01a4c79013e703a012475c02461aa1fcd99127e8d83a02216386ed6942b2c8103845fd0812300dd199e6e7e054
languageName: node
linkType: hard

"chokidar@npm:^3.5.1, chokidar@npm:^3.5.3":
version: 3.5.3
resolution: "chokidar@npm:3.5.3"
Expand Down Expand Up @@ -3228,7 +3257,7 @@ __metadata:
languageName: node
linkType: hard

"domhandler@npm:^5.0.1, domhandler@npm:^5.0.2":
"domhandler@npm:^5.0.1, domhandler@npm:^5.0.2, domhandler@npm:^5.0.3":
version: 5.0.3
resolution: "domhandler@npm:5.0.3"
dependencies:
Expand Down Expand Up @@ -3338,7 +3367,7 @@ __metadata:
languageName: node
linkType: hard

"entities@npm:^4.2.0":
"entities@npm:^4.2.0, entities@npm:^4.4.0":
version: 4.5.0
resolution: "entities@npm:4.5.0"
checksum: 853f8ebd5b425d350bffa97dd6958143179a5938352ccae092c62d1267c4e392a039be1bae7d51b6e4ffad25f51f9617531fedf5237f15df302ccfb452cbf2d7
Expand Down Expand Up @@ -4415,6 +4444,18 @@ __metadata:
languageName: node
linkType: hard

"htmlparser2@npm:^8.0.1":
version: 8.0.2
resolution: "htmlparser2@npm:8.0.2"
dependencies:
domelementtype: ^2.3.0
domhandler: ^5.0.3
domutils: ^3.0.1
entities: ^4.4.0
checksum: 29167a0f9282f181da8a6d0311b76820c8a59bc9e3c87009e21968264c2987d2723d6fde5a964d4b7b6cba663fca96ffb373c06d8223a85f52a6089ced942700
languageName: node
linkType: hard

"http-cache-semantics@npm:^4.1.0":
version: 4.1.1
resolution: "http-cache-semantics@npm:4.1.1"
Expand Down Expand Up @@ -5898,6 +5939,7 @@ __metadata:
"@types/polyfill-library": ^3.108.1
browserslist-to-esbuild: ^1.2.0
changelogen: ^0.5.0
cheerio: ^1.0.0-rc.12
eslint: ^8.33.0
nuxt: ^3.1.2
polyfill-library: ^4.6.0
Expand Down Expand Up @@ -6214,6 +6256,25 @@ __metadata:
languageName: node
linkType: hard

"parse5-htmlparser2-tree-adapter@npm:^7.0.0":
version: 7.0.0
resolution: "parse5-htmlparser2-tree-adapter@npm:7.0.0"
dependencies:
domhandler: ^5.0.2
parse5: ^7.0.0
checksum: fc5d01e07733142a1baf81de5c2a9c41426c04b7ab29dd218acb80cd34a63177c90aff4a4aee66cf9f1d0aeecff1389adb7452ad6f8af0a5888e3e9ad6ef733d
languageName: node
linkType: hard

"parse5@npm:^7.0.0":
version: 7.1.2
resolution: "parse5@npm:7.1.2"
dependencies:
entities: ^4.4.0
checksum: 59465dd05eb4c5ec87b76173d1c596e152a10e290b7abcda1aecf0f33be49646ea74840c69af975d7887543ea45564801736356c568d6b5e71792fd0f4055713
languageName: node
linkType: hard

"parseurl@npm:~1.3.3":
version: 1.3.3
resolution: "parseurl@npm:1.3.3"
Expand Down

0 comments on commit 060c00f

Please sign in to comment.