Coder Social home page Coder Social logo

Comments (7)

devinivy avatar devinivy commented on June 8, 2024 1

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.

kanongil avatar kanongil commented on June 8, 2024 1

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.

devinivy avatar devinivy commented on June 8, 2024

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.

devinivy avatar devinivy commented on June 8, 2024

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.

AnthonyCouval avatar AnthonyCouval commented on June 8, 2024

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.

kanongil avatar kanongil commented on June 8, 2024

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.

AnthonyCouval avatar AnthonyCouval commented on June 8, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.