Skip to content
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

need help building example, esmregister + esmock #788

Closed
iambumblehead opened this issue Jun 15, 2024 · 11 comments · Fixed by oxc-project/oxc-resolver#223, #811 or #825
Closed

need help building example, esmregister + esmock #788

iambumblehead opened this issue Jun 15, 2024 · 11 comments · Fixed by oxc-project/oxc-resolver#223, #811 or #825
Assignees

Comments

@iambumblehead
Copy link

When trying to create an example test with swc and esmock, this error occurs around the types definition file,

node --import @swc-node/register/esm-register --test esmock.node-swc.test.ts

> test:current
> node --import @swc-node/register/esm-register --test esmock.node-swc.test.ts

file:///home/bumble/soft/esmock/src/esmock.d.ts:1
type MockMap = { [specifier: string]: any }
     ^^^^^^^

SyntaxError: Unexpected identifier 'MockMap'
    at ModuleLoader.moduleStrategy (node:internal/modules/esm/translators:169:18)
    at callTranslator (node:internal/modules/esm/loader:272:14)
    at ModuleLoader.moduleProvider (node:internal/modules/esm/loader:278:30)

Thanks in advance for any advice

@Brooooooklyn
Copy link
Member

Fixed in #798

@iambumblehead
Copy link
Author

The specific error described at the top of this issue is resolved, however, after updating @swc-node/register a new issue is encountered,

✖ should mock js when using swc (34.125566ms)
  file:///home/bumble/soft/esmock/tests/local/mainUtil.js:2
  import babelGeneratedDoubleDefault from 'babelGeneratedDoubleDefault';
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  SyntaxError: The requested module 'babelGeneratedDoubleDefault' does not provide an export named 'default'

The error needs context to be understood but, essentially, @swc-node/register encounters an error loading mixed esm/cjs modules.

Slightly difficult to describe the sequence resulting in the error, here is what happens,

  1. 'esmock.node-swc.test.ts' imports '../local/main.js' (esm) link
  2. '../local/main.js' imports './mainUtil.js' (esm) link
  3. 'mainUtil.js' imports 'babelGeneratedDoubleDefault' (cjs) error link
  4. '../package.json' links 'babelGeneratedDoubleDefault' to the local directory package 'babelGeneratedDoubleDefault', link package.json
  5. 'babelGeneratedDoubleDefault' describes a cjs module, link babelGeneratedDoubleDefault/package.json, link babelGeneratedDoubleDefault.js

The other loaders used in the test suite process the same import tree without error https://github.com/iambumblehead/esmock/tree/attempt-enable-swc-tests/tests

Thank you for reading me

@Brooooooklyn
Copy link
Member

I've tried tsx, it's also throw a error:

// index.ts
import assert from 'node:assert'
import test from 'node:test'

import babelGeneratedDoubleDefault from './babel-generated-double-default'

await test('import default from babel-generated cjs file', () => {
  assert.equal(babelGeneratedDoubleDefault(), 'default.default')
})

pnpm tsx index.ts:

CleanShot 2024-07-08 at 21 37 02@2x

@iambumblehead
Copy link
Author

git clone https://github.com/iambumblehead/esm-import-cjs-repro.git
cd esm-import-cjs-repro
npm install
npm run test:node # pass
npm run test:tsx # pass
npm run test:swc # fail

@Brooooooklyn
Copy link
Member

https://github.com/iambumblehead/esm-import-cjs-repro.git

@iambumblehead this failed because of the peerDependencies are missing, you need to install @swc/core and typescript, and everything should be fine.

@iambumblehead
Copy link
Author

after adding @swc/core and typescript dependencies, tests still fail. Do they need to be declared in a specific way in pacakge.json?

$ cat package.json | grep dependencies -A 6
  "dependencies": {
    "babelGeneratedDoubleDefault": "file:./babelGeneratedDoubleDefault",
    "typescript": "^5.5.3",
    "@swc-node/register": "1.10.2",
    "@swc/core": "^1.6.13",
    "tsx": "^4.15.5"
  },


$ npm install

added 2 packages, and audited 28 packages in 34s

6 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities


$ npm run test:swc

> esm-import-cjs-repro@0.0.1 test:swc
> node --import @swc-node/register/esm-register --test test.swc.test.ts

✖ should import babelGeneratedDoubleDefault, cjs deep (12.353619ms)
  file:///home/bumble/soft/esm-import-cjs-repro/src/esmmain2.js:1
  import babelGeneratedDoubleDefault from 'babelGeneratedDoubleDefault';

@iambumblehead
Copy link
Author

the first test, importing mixed esm cjs tree passes, but the second test encounters a runtime error,

test('should mock ts when using swc', async () => {
  const main = await esmock('../local/main-ts.ts', {
    path: {
      basename: () => 'hellow'
    }
  })

  assert.strictEqual(main.pathbasenamewrap(), 'hellow')
})

The error occurs when the typescript file "main-ts.ts" is loaded.

test at esmock.node-swc.test.ts:15:1
✖ should mock ts when using swc (5.206873ms)
  SyntaxError [Error]: Unexpected token ':'

main-ts.ts, link

import path from 'path'
import pg from 'pg'

export default {
  pathbasenamewrap: (n: any) => path.basename(n),
  pgpoolwrap: (n: any) => pg.Pool(n)
}

If the typescript : any is removed from the file, the test passes. It seems the file is not transformed.

@iambumblehead
Copy link
Author

the error happens when esmock is engaged to load the ts file await esmock('../local/main-ts.ts') using import does not result in an error await import ('../local/main-ts.ts')

the same test using esmock passes when using node-ts, tsx or tsm

@iambumblehead
Copy link
Author

At esm-import-cjs-repro, the test is added for tsx and swc and the error is reproduced using swc

git clone https://github.com/iambumblehead/esm-import-cjs-repro.git
cd esm-import-cjs-repro
npm install
npm run test:node # pass
npm run test:tsx # pass
npm run test:swc # fail

@iambumblehead
Copy link
Author

related #710

@iambumblehead
Copy link
Author

all tests pass :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment