Skip to content

Add tests for text modules#56812

Open
eemeli wants to merge 7 commits intoweb-platform-tests:masterfrom
eemeli:import-text
Open

Add tests for text modules#56812
eemeli wants to merge 7 commits intoweb-platform-tests:masterfrom
eemeli:import-text

Conversation

@eemeli
Copy link

@eemeli eemeli commented Dec 17, 2025

This is to support the spec changes in:

I've effectively tried to duplicate and appropriately modify all the tests for JSON modules that I was able to find.

@bakkot
Copy link
Contributor

bakkot commented Dec 17, 2025

Might consider adding a test that the request has a Sec-Fetch-Dest: text header. I think this is lacking for JSON as well, and have it on my backlog to write such a test, but as long as you're here...

Also, you could add a test that importing a module as JS, from a server which responds with valid JS file with a content type of text/javascript (and so loading succeeds), and then fetching it again with type: "text" causes two network requests (because the type is part of the module map). This is similar to repeated-imports.any.js but the other way around.

@eemeli
Copy link
Author

eemeli commented Dec 20, 2025

Might consider adding a test that the request has a Sec-Fetch-Dest: text header. I think this is lacking for JSON as well, and have it on my backlog to write such a test, but as long as you're here...

Done. There are some test for the JSON module fetch headers already at fetch/metadata/tools/templates/script-json-module-import-static.sub.html; I added corresponding ones for text modules, as well as a non-generated test modeled after the audioworklet test.

Also, you could add a test that importing a module as JS, from a server which responds with valid JS file with a content type of text/javascript (and so loading succeeds), and then fetching it again with type: "text" causes two network requests (because the type is part of the module map). This is similar to repeated-imports.any.js but the other way around.

Done; added those to repeated-imports.any.js.

@eemeli eemeli requested a review from bakkot December 20, 2025 10:01
@bakkot
Copy link
Contributor

bakkot commented Dec 20, 2025

New tests look great, thanks.

promise_test(async test => {
await promise_rejects_js(test, SyntaxError,
import('./bom-utf-16be.txt', { with: { type: 'text' } }), 'Expected parse error from UTF-16BE BOM');
}, 'UTF-16BE BOM should result in parse error in text module script');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels needlessly inconsistent from the precedent that has been set for always decoding as UTF-8. Why account for the UTF-16 BOM when we don't account for many other things.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, this was a copy-paste error from the JSON module tests, which will consider these errors because they're not valid JSON. For text modules, we should get the file contents, parsed as UTF-8.

@eemeli eemeli requested a review from annevk March 12, 2026 22:42
Copy link
Member

@annevk annevk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These look good to me. I didn't verify every line though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants