You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
What are you trying to achieve or the steps to reproduce?
I have CJS based repo written in Typescript which needs to use ESM-only dependency. I do it by dynamically importing that dependency. Code works correctly when run, but fails to compile during test run.
I use lab with typescript option as advised by docs. In my tsconfig.json I use compilerOptions.module: "Node16" to enable native ECMAScript module support (as per Typescript docs). When running test I get
Unexpected token 'export'
Reason is found in typescript.js file, which is used when typescript option is passed. Mentioned line overrides repos tsconfig option and disables possibility to dynamically import ESM dependency. When I comment out that override, code compiles correctly during test run.
I am not sure whether it is a bug or intended behaviour. If it is intended what is designed way to run my scenario?
import * as Lab from "@hapi/lab";
import { expect } from "@hapi/code";
import { isPMapImported } from "../src/utils";
const lab = Lab.script();
const { describe, it, before } = lab;
export { lab };
describe("experiment", () => {
before(() => {});
it("is p-map imported", async () => {
await isPMapImported();
// assertion doesn't matter as compilation error will happen
expect(true).to.be.true();
});
});
When running:
npm run dist && npm run start (runs dist index.js) I get log Is Pmap imported: true
npm run test I get compilation error
When I comment out line 29 in node_modules/@hap/lab/lib/modules/typescript.js and run npm run test I get log Is Pmap imported: true and test succeeds
What was the result you got?
Compilation error
Failed tests:
1) experiment is p-map imported:
Unexpected token 'export'
at wrapSafe (node:internal/modules/cjs/loader:1281:20)
at Module._compile (node:internal/modules/cjs/loader:1321:27)
at Object.require.extensions.<computed> [as .js] (/Users/leszek_pawlega/git/lab-test/node_modules/@hapi/lab/lib/modules/transform.js:41:28)
at Module.load (node:internal/modules/cjs/loader:1208:32)
at Function.Module._load (node:internal/modules/cjs/loader:1024:12)
at Module.require (node:internal/modules/cjs/loader:1233:19)
at require (node:internal/modules/helpers:179:18)
at /Users/leszek_pawlega/git/src/isPMapImported.ts:3:17
at isPMapImported (/Users/leszek_pawlega/git/src/isPMapImported.ts:3:17)
at /Users/leszek_pawlega/git/test/isPMapImported.ts:14:5
at Immediate.<anonymous> (/Users/leszek_pawlega/git/lab-test/node_modules/@hapi/lab/lib/runner.js:659:21)
What result did you expect?
Test will pass
The text was updated successfully, but these errors were encountered:
leszek---
changed the title
Dynamic import ESM based dependency in CJS based repo
Dynamic import ESM based dependency in Typescript CJS based repo
Sep 16, 2024
Runtime
node.js
Runtime version
20.16.0
Module version
25.3.1
Last module version without issue
No response
Used with
No response
Any other relevant information
No response
What are you trying to achieve or the steps to reproduce?
I have CJS based repo written in Typescript which needs to use ESM-only dependency. I do it by dynamically importing that dependency. Code works correctly when run, but fails to compile during test run.
I use lab with
typescript
option as advised by docs. In mytsconfig.json
I usecompilerOptions.module: "Node16"
to enable native ECMAScript module support (as per Typescript docs). When running test I getReason is found in typescript.js file, which is used when
typescript
option is passed. Mentioned line overrides repos tsconfig option and disables possibility to dynamically import ESM dependency. When I comment out that override, code compiles correctly during test run.I am not sure whether it is a bug or intended behaviour. If it is intended what is designed way to run my scenario?
How to reproduce?
package.json
tsconfig.json
src/index.ts
src/utils.ts
test/utils.ts
When running:
npm run dist && npm run start
(runs dist index.js) I get logIs Pmap imported: true
npm run test
I get compilation errorWhen I comment out line 29 in node_modules/@hap/lab/lib/modules/typescript.js and run
npm run test
I get logIs Pmap imported: true
and test succeedsWhat was the result you got?
Compilation error
What result did you expect?
Test will pass
The text was updated successfully, but these errors were encountered: