-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Inlining tests & more #34
Comments
Interesting point. While not ideal, you can solve this with dynamic import: export function fib(n) { /* ... */ }
/** @test */
module {
import {expect} from "chai"
const {fib} = await import(import.meta.url);
expect(fib(4)).to.equal(24);
} I think thee “JavaScript Bundles” proposal that is mentioned in the README might help here. It’s likely out of scope for Module Blocks directly. |
That would only be possible if |
I like the idea of inlining test modules, but |
We could pick any non-string specifier :-) the entire design space is open. |
I think this is an interesting use case, but it'd be best if we had a way to address the test module from the outside, so it can be imported by the test framework from the outside, rather than relegating it to dead code, requiring transpilation based on magic comments to make it work. I have some ideas about this, and hope to get back soon with a proposal (more closely related to "JS module bundles" rather than "JS module blocks"). |
@littledan to draw inspiration from TypeScript's // myModule.js
export type Foo = { ... } // example from ts syntax
export test myModuleTests = module { ... }
// myOtherModule.js
import type { Foo } from "myModule.js" // example from ts syntax
import test { myModuleTests } from "myModule.js" Where So could // myModule.js
export test myModuleTests = module {
export function foo() {}
}
let newName = myModuleTests // Error: Cannot reference myModuleTests in non-test position
let exports = import(myModuleTests) // Error: Cannot reference myModuleTests in non-test position
// myOtherModule.js
import { myModuleTests } from "myModule.js" // Error: Cannot reference myModuleTests in non-test position
import test { myModuleTests } from "myModule.js" // (No Error) |
I don't like a syntax level solution for this problem (the |
I wrote out a JS module fragments proposal, where testing could be done like this: // math.js
export function add(a, b) {
return a + b
}
module "#test" {
import { add } from "./math.js" // Hmm, maybe there should be shorthand for this, indeed
export function addTest() {
assert(add(35, 7) === 42)
}
} Then, the test framework can import |
I received the email notifying me of someone comments this:
But I cannot find that comment in the thread. I think this idea is cool to resolve the |
There’s no single concept of a “parent module”, because 0, 1, or 57 modules might import the one in question. |
I think by "parent module of X" they don't mean "the module which imports X", but "the module inside of which X is declared". However, we could still have 57 nested module blocks. |
This proposal doesn't have the goal to extend the import syntax: module expressions can only use the existing However, https://github.com/tc39/proposal-built-in-modules does attempt to expand the import syntax to allow referring to "syntactic modules", and we could consider a "import from the parent" feature as part of that proposal. I opened tc39/proposal-module-declarations#20 to keep track of this. |
I promise there is a relevant point at the end of this if you stick with me for a second.
An increasing number of language have a way to write various types of tests inside of source files. For example, in Rust: Demo
Module blocks provide a similar sort of syntax. And you could envision something like this:
module {}
is dead code, and (once supported) your average optimizing compiler would remove this code. So you can easily avoid it at runtime.Most JavaScript testing frameworks are already apply various compile-time transforms today, so they already have all the pieces they need to find these modules blocks and execute them in a test environment.
The biggest limitation of the JS module blocks proposal right now is that you cannot import functions, classes, etc from the parent module.
In Rust:
Maybe in JavaScript?
It's easy to imagine how this could be useful for the web workers use case as well.
I know this isn't the sort of thing TC39 normally considers when designing features. But I hope this doesn't just get dismissed as a tooling problem. There are way too many of those, and the result ends up being the same syntax with different behaviors depending on the tool you're using (see existing module behavior in webpack, jest, etc.).
The text was updated successfully, but these errors were encountered: