-
-
Notifications
You must be signed in to change notification settings - Fork 16
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
Implement tests with uvu/swc #104
base: main
Are you sure you want to change the base?
Conversation
|
This pull request is being automatically deployed with Vercel (learn more). microsite – ./site🔍 Inspect: https://vercel.com/nmoo/microsite/qremvjqc8 [Deployment for 1853843 canceled] microsite-examples – ./🔍 Inspect: https://vercel.com/nmoo/microsite-examples/huy9k67do [Deployment for 1853843 failed] |
I’m stoked to see this working! I will say I found Swc docs somewhat underwhelming, so I took another stab at an esbuild-based solution and once again sunk a bunch of time. I’m gonna let this white whale go for now and focus on getting something real done. But I will mention I discovered a few issues along the way with how some dependencies and local modules are imported.
|
|
||
2. Install dependencies for the monorepo: `npm install`. | ||
|
||
3. Install and link dependencies in sub-packages: `npx lerna bootstrap` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see you’ve landed on leaving off hoist
, is that intentional? I certainly think it’ll improve the top level dependency flow for now. But just wanted to check that this is actually the preferred flow.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, intentional! Seems to work just fine.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It’s a whole lot less fussy! I will say that I’m certain Yarn (1) would make hoisting work better for perf and a better FS impact. I can almost guarantee PNPM would too, just haven’t tried it. But this is definitely a case where perfection is something I’d avoid solving, I’m just happy to know I’m all good to install a top level dependency without wanting to take a smoke break during the re-bootrappening
Oof. So I'm back at my keyboard and giving this a go again, with a test importing import { suite } from 'uvu';
import * as assert from 'uvu/assert';
import build from '../../src/cli/microsite-build';
const component = suite('Build');
component('should render a basic HTML document', () => {
assert.type(build, 'function');
});
component.run(); ... which causes this error: const require1 = createRequire(import.meta.url);
^^^^
SyntaxError: Cannot use 'import.meta' outside a module
at wrapSafe (internal/modules/cjs/loader.js:915:16)
at Module._compile (internal/modules/cjs/loader.js:963:27)
at Module._compile (/Users/gnosis/Projects/eyelidlessness/microsite/node_modules/pirates/lib/index.js:99:24)
at Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
at Object.newLoader [as .ts] (/Users/gnosis/Projects/eyelidlessness/microsite/node_modules/pirates/lib/index.js:104:7)
at Module.load (internal/modules/cjs/loader.js:863:32)
at Function.Module._load (internal/modules/cjs/loader.js:708:14)
at Module.require (internal/modules/cjs/loader.js:887:19)
at require (internal/modules/cjs/helpers.js:74:18)
at Object.<anonymous> (/Users/gnosis/Projects/eyelidlessness/microsite/packages/microsite/test/cli/microsite-build.test.ts:3:1) I think this is all being treated as CJS at runtime, which I fear may cause testing anything ESM-specific to be error prone and potentially untrustworthy. |
I've also noticed that SWC is doing some weirdness with stuff in node_modules, it disallows a fair bit of syntax by default. I think I might have one more go at the white whale. What do you think of this approach?
I'm out of steam for today but happy to look at this tomorrow if you think it's worth one more go. |
It occurred to me when I mentioned that the other tooling uses esbuild... would it be absurd to just run tests through snowpack? I see there’s a first party test runner. It’s using |
Obviously I haven’t fully run out of steam I’m just doing some research whilst sipping a whiskey on my porch. I haven’t read through all of this thread but it looks like it’ll be helpful! |
I'm not opposed to other approaches, but I'd ideally like to keep things as simple as possible. Seems like the tooling for Node Using Snowpack would be interesting but Microsite can't really run in-browser at the moment, so there'd be some refactoring needed to make that happen (and I'm not sure it would even be possible.) As for the I wonder if shipping Microsite as dual .cjs and .mjs would kinda solve this whole thing? We'd drop the |
On the same page! This is part of why I’m here working with this project! I actually think the snowpack solution might be the answer because it bakes in all the same assumptions the project already has. If it works out I’d almost certainly offer it as a separate dependency (like web/test-runner, with no opinion on which test framework is used; apparently that’s their design, the only difference is running on node instead of a browser; the discussion I linked goes into this and should be invaluable).
This is a big part of why the “use snowpack” idea felt like a big lightbulb. You already have a config that works for your project. A plugin which executes a test runner not only guarantees the least rework and weird branching logic, it also guarantees the tests are as close to the real world product as possible. I’ve seen enough of the snowpack and rollup plugin APIs to know you can just run anything you want on the build/dev product if you know where to plug in.
There’s some value in that for DX but I’m hesitant to endorse it. ESM is the future and that future is coming fast. Snowpack and rollup already have special benefits using ESM. Offering a CJS escape hatch is a really good way to deoptimize everything happening here. I’m not sure what the right answer is because like you said ESM is very immature where people are doing dev. But I think if I spend some time looking into this BYOR plugin it might shake some answers out. |
Well, I tried the snowpack route first, and bailed pretty quickly because the perf hit at the installing dependencies stage was more than bad enough to negate any benefits. I’m neck deep in trying to build out an experimental loader and esbuild is, well, complaining about perfectly valid TSX syntax. For instance here it complains about the It’s possible I just haven’t found the right magic incantation for esbuild. But at this point I’m not sure what is actually going to work and I’m kind of antsy to get something working so I can build my site. If it’s okay with you, and if you’re comfortable with my proposed solution on #96, I may just suck it up and do that without a test and defer to you on test setup. Because I’m at a loss at this point. |
@eyelidlessness Yeah this is turning out to be a much bigger chore than expected. I'll take a look at #105 and we can circle back to this. |
9ebc5d5
to
b70487a
Compare
Alternative to #100, using
uvu -r @swc-node/register
to compile.ts
tests on the fly without writing them to disk.@eyelidlessness did a bunch of this legwork, so this wouldn't have happened without them!
@swc-node/[email protected]
which includes a patch to respectjsxFactory
andjsxFragmentFactory
fromtsconfig
..js
file extensions because@swc-node/register
doesn't (and probably shouldn't) allow that like TypeScript does