Comments (7)
I've been thinking about this recently, and I agree. There are a couple paths we can take, e.g. integrating with c8 or rolling our own using similar techniques. I also wrote a module mo-walk for my own purposes recently that we could fork or expand on, for enumerating and running test files across both cjs and esm.
from lab.
Hmm, contrary to the docs, node will actually apply require.extensions
to .js
commonjs files through an import
. This seems quite suspect, as it will need to determine the type before loading.
This means that it should be somewhat straightforward to support ESM-based test files, as long as all code that needs coverage checks are commonjs .js
files. I'm not sure it makes much sense with that limitation, though.
from lab.
Unfortunately we don't support ES module projects at the moment, though I expect we'll probably see this working in the future. It's not ideal, but one option may be to use lab's transform option to "treat" it as a typescript project (which supports that syntax) even though it technically isn't. For the time being that is the only workaround I can think of π
from lab.
Actually, it may be slightly simpler now since you can use the --typescript
flag rather than a transform, as of this recent PR: #1026.
from lab.
Oh ok, I probably missed that information about ES module project. Sorry.
Unfortunately even with --typescript flag it's not working. I've tried to put a tsconfig.json in my root app directory and install Typescript.
Is there anything else I should add ?
> [email protected] test D:\Projects\new-api
> lab --typescript ./test/server.test.js
Error requiring file: D:\Projects\new-api\test\server.test.js
Cannot use import statement outside a module
D:\Projects\new-api\test\server.test.js:1
import * as Lab from '@hapi/lab';
^^^^^^
SyntaxError: Cannot use import statement outside a module
at wrapSafe (internal/modules/cjs/loader.js:988:16)
at Module._compile (internal/modules/cjs/loader.js:1036:27)
at Object.require.extensions.<computed> [as .js] (D:\Projects\new-api\node_modules\@hapi\lab\lib\modules\transform.js:41:28)
at Module.load (internal/modules/cjs/loader.js:937:32)
at Function.Module._load (internal/modules/cjs/loader.js:778:12)
at Module.require (internal/modules/cjs/loader.js:961:19)
at require (internal/modules/cjs/helpers.js:92:18)
at D:\Projects\new-api\node_modules\@hapi\lab\lib\cli.js:135:13
at Array.forEach (<anonymous>)
at Object.internals.traverse (D:\Projects\new-api\node_modules\@hapi\lab\lib\cli.js:129:15)
at Object.exports.run (D:\Projects\new-api\node_modules\@hapi\lab\lib\cli.js:60:31)
at main (D:\Projects\new-api\node_modules\@hapi\lab\bin\lab:56:50)
at Object.<anonymous> (D:\Projects\new-api\node_modules\@hapi\lab\bin\lab:60:1)
at Module._compile (internal/modules/cjs/loader.js:1072:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)
at Module.load (internal/modules/cjs/loader.js:937:32)
at Function.Module._load (internal/modules/cjs/loader.js:778:12)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
at internal/main/run_main_module.js:17:47
My tsconfig.json :
{
"compilerOptions": {
"target": "es2021",
"module": "commonjs",
"moduleResolution": "node"
}
}
from lab.
You will need to rename your files to end in .ts
for that approach to have a chance of working. It will still fail, since you also need to add an export
before const lab = β¦
.
Looking into it, it will be a considerable task to enable ESM-based tests. Mostly because we rely on require.extensions
to dynamically transform required code to instrument it for the coverage tool. This API has no stable replacement, and can only be enabled through a new experimental API that is quite different in v14 vs v16.
This would probably be simpler to do, if code coverage is updated to use the v8 instrumentation. Then we could use the existing require
for typescript tests, and import
for plain .js
files.
from lab.
Thanks for all these explanations, it's clearer now.
FYI Jest has the same problem, it works with ESM but it's unstable and Mocha works but with some limitations.
from lab.
Related Issues (20)
- Use the new module ESLint configuration HOT 1
- Using the coverage reporter requires "@hapi/eslint-plugin" to be installed, even if itβs not used. HOT 12
- DOMException will be a global in Node.js 17+ HOT 2
- Update linter to work with ESLint 8 HOT 15
- Revise leak detection HOT 2
- Supply AbortController signal to tests HOT 1
- Add built-in TypeScript support
- How to run individual tests: updated docs HOT 1
- tsconfig paths alias HOT 2
- Report "Type T has no properties in common with type U" as a type error
- Allow to specify debugger interface HOT 1
- Lint .mjs (et al) HOT 1
- Uncovered lines in test report HOT 2
- Use diffWordsWithSpace instead of diffWords in the console reporter
- Node 18 undici global HOT 6
- Cannot find module non-relatively imported
- UnhandledPromiseRejectionWarning HOT 11
- Add `.ts` to the default `extensions` for `--lint`
- Babel Preset transform is breaking on updating node version
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 lab.