Comments (14)
Cool, will do
from worktop.
Hey, nope, will not add conditional exports for this project.
This is a TS limitation that is still being worked through.
For the time being, you can either precompile your TS into JS with ESM format, and then use a test runner that supports ESM (tap does); or you can use a require hook that transforms everything on the fly into ESM or CommonJS, depending on your runtime. For example, something like this would work everywhere: uvu -r esbuild-register tests
(configured via tsconfig.json
file).
Hope that helps~!
from worktop.
Still doesn't work same error mesage, any idea?
"test": "tap --node-arg=\"--require=esbuild-register\"",
from worktop.
with uvu
"test": "uvu src \".test.ts$\" -r esbuild-register",
.../node_modules/worktop/request/index.mjs:18
src/routes/foo.test.ts 2> export {
SyntaxError: Unexpected token 'export'
from worktop.
Here's an example setup:
// src/index.ts
import { encode } from 'worktop/utils';
export function howdy(input: string) {
return encode(input);
}
// test/index.ts
import { test } from 'uvu';
import * as assert from 'uvu/assert';
import * as app from '../src';
test('should be a function', () => {
assert.type(app.howdy, 'function');
});
test('should be encoded', () => {
let output = app.howdy('hello');
assert.is.not(output, 'hello');
assert.instance(output, Uint8Array);
});
test.run();
// package.json
{
"scripts": {
"test": "uvu -r ./bin/register.js test"
},
"devDependencies": {
"esbuild": "0.11.12",
"uvu": "0.5.1",
"worktop": "0.4.2"
}
}
// bin/esbuild.js
const esbuild = require('esbuild');
/** @type {esbuild.CommonOptions} */
const options = {
target: 'es2019',
sourcemap: false,
treeShaking: true,
minifySyntax: true,
minifyIdentifiers: true,
}
/**
* @param {string} input
* @param {string} output
* @param {string[]} [externals]
*/
exports.build = function (input, output, externals=[]) {
return esbuild.build({
...options,
bundle: true,
format: 'esm',
outfile: output,
entryPoints: [input],
external: externals,
});
}
/**
* @param {string} source
* @param {esbuild.TransformOptions} [overrides]
*/
exports.transform = function (source, overrides={}) {
return esbuild.transformSync(source, {
...options,
format: 'cjs',
...overrides
});
}
// bin/register.js
const { transform } = require('./esbuild');
const loadCJS = require.extensions['.js'];
/**
* @param {string} extn
* @param {string} loader
*/
function loader(extn, loader) {
require.extensions[extn] = function (Module, filename) {
const pitch = Module._compile.bind(Module);
Module._compile = source => {
const { code, warnings } = transform(source, {
sourcefile: filename,
loader: loader,
});
warnings.forEach(msg => {
console.warn(`\nesbuild warning in ${filename}:`);
console.warn(msg.location);
console.warn(msg.text);
});
return pitch(code, filename);
};
loadCJS(Module, filename);
}
}
loader('.ts', 'ts');
loader('.mjs', 'js');
loader('.cjs', 'js');
Note: The two
/bin
files are (practically) cloned fromworktop
's testing setup
But yeah – sorry, forgot that esbuild-register
ignores .mjs
files by default. I don't know of a way around it – I always just have my own bin/register
script.
from worktop.
@lukeed thank you! I'll try it out. Are you sure you won't support cjs in worktop? I'd create a PR
from worktop.
Yup, 100% sure. Thank you though
from worktop.
Works! We should document it.
from worktop.
Do you have type-check when running the tests?
from worktop.
I see you run it explicitly.
from worktop.
No, in every project I always have it as a separate process. My IDE type checks for live-feedback. I don't need it to run again & again & again for tests – catastrophic errors will fail compilation.
I'll also add a pretest
npm-script that runs tsc
for me, most of the time.
from worktop.
Make sense.
from worktop.
It looks like that this step breaks debugging entirely. The breakpoints aren't longer mapped to the right code location.
from worktop.
Yeah, that's the missing part of the bin/*.js
scripts. I don't have sourcemap support in here yet but it should be fairly straightforward. TBH that's the missing piece before actually extracting this as a separate module.
from worktop.
Related Issues (20)
- Using Worktop in a Pages website HOT 3
- Use Sveltekit and worktop together? HOT 2
- How to set multiple cookies? HOT 2
- Worktop Middlewares Example HOT 4
- Durable Objects example HOT 2
- The example of timing requests might be misleading HOT 1
- Using official Cloudflare types HOT 4
- Can I use Worktop with Fastly Compute@Edge? HOT 1
- Is it going to be official by cloudflare?
- Cache API and KV: Concerns/Questions/Usage HOT 1
- How to access `.env` with Wrangler 2 HOT 2
- Provide a "test" module HOT 4
- Serving HTML files HOT 6
- How to use with cron / scheduled event ? HOT 2
- Is `worktop@next` safe to use in production? HOT 2
- Add generic typing for Router.add handler
- The uploaded script has no registered event handlers. [API code: 10068] HOT 1
- Add sub-routers HOT 2
- Node 16+ ESM TypeScript error HOT 1
- `isCacheable` allowed cache by default if the `cache-control` header is empty may leading to a security flaw in ssg content HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from worktop.