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

Fails to import with node:test #113

Open
1 task done
willfarrell opened this issue Aug 28, 2022 · 3 comments
Open
1 task done

Fails to import with node:test #113

willfarrell opened this issue Aug 28, 2022 · 3 comments
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@willfarrell
Copy link

willfarrell commented Aug 28, 2022

Checklist

  • I have read Caveats documentation and didn't find a solution for this problem there.

Bug description
When using with node:test, node is unable to import the library. It looks like this might be a tsc configuration issue.

> test
> node --test  --trace-warnings

TAP version 13
# Subtest: /path/to/index.test.js
not ok 1 - /path/to/index.test.js
  ---
  duration_ms: 0.040726583
  failureType: 'subtestsFailed'
  exitCode: 1
  stdout: ''
  stderr: |-
    (node:99872) ExperimentalWarning: The test runner is an experimental feature. This feature could change at any time
        at emitExperimentalWarning (node:internal/util:226:11)
        at node:test:5:1
        at BuiltinModule.compileForInternalLoader (node:internal/bootstrap/loaders:331:7)
        at BuiltinModule.compileForPublicLoader (node:internal/bootstrap/loaders:269:10)
        at loadBuiltinModule (node:internal/modules/cjs/helpers:50:9)
        at ESMLoader.builtinStrategy (node:internal/modules/esm/translators:258:18)
        at ESMLoader.moduleProvider (node:internal/modules/esm/loader:470:14)
    file:///path/to/index.test.js:3
    import { mockClient } from 'aws-sdk-client-mock'
             ^^^^^^^^^^
    SyntaxError: Named export 'mockClient' not found. The requested module 'aws-sdk-client-mock' is a CommonJS module, which may not support all module.exports as named exports.
    CommonJS modules can always be imported via the default export, for example using:
    
    import pkg from 'aws-sdk-client-mock';
    const { mockClient } = pkg;
    
        at ModuleJob._instantiate (node:internal/modules/esm/module_job:123:21)
        at async ModuleJob.run (node:internal/modules/esm/module_job:189:5)
        at async Promise.all (index 0)
        at async ESMLoader.import (node:internal/modules/esm/loader:528:24)
        at async loadESM (node:internal/process/esm_loader:91:5)
        at async handleMainPromise (node:internal/modules/run_main:65:12)
    
    Node.js v18.8.0
    
  error: 'test failed'
  code: 'ERR_TEST_FAILURE'
  ...
1..1
# tests 1
# pass 0
# fail 1
# cancelled 0
# skipped 0
# todo 0
# duration_ms 0.063401583

Updating to use import pkg from 'aws-sdk-client-mock'; const { mockClient } = pkg;, returns the following:

> test
> node --test  --trace-warnings

TAP version 13
# Subtest: /paht/to/index.test.js
not ok 1 - /path/to/index.test.js
  ---
  duration_ms: 0.044459167
  failureType: 'subtestsFailed'
  exitCode: 1
  stdout: ''
  stderr: |-
    (node:835) ExperimentalWarning: The test runner is an experimental feature. This feature could change at any time
        at emitExperimentalWarning (node:internal/util:226:11)
        at node:test:5:1
        at BuiltinModule.compileForInternalLoader (node:internal/bootstrap/loaders:331:7)
        at BuiltinModule.compileForPublicLoader (node:internal/bootstrap/loaders:269:10)
        at loadBuiltinModule (node:internal/modules/cjs/helpers:50:9)
        at ESMLoader.builtinStrategy (node:internal/modules/esm/translators:258:18)
        at ESMLoader.moduleProvider (node:internal/modules/esm/loader:470:14)
    (node:835) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
    /path/to/node_modules/aws-sdk-client-mock/dist/es/index.js:1
    export * from './mockClient';
    ^^^^^^
    
    SyntaxError: Unexpected token 'export'
        at Object.compileFunction (node:vm:360:18)
        at wrapSafe (node:internal/modules/cjs/loader:1048:15)
        at Module._compile (node:internal/modules/cjs/loader:1083:27)
        at Module._extensions..js (node:internal/modules/cjs/loader:1173:10)
        at Module.load (node:internal/modules/cjs/loader:997:32)
        at Module._load (node:internal/modules/cjs/loader:838:12)
        at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:170:29)
        at ModuleJob.run (node:internal/modules/esm/module_job:193:25)
        at async Promise.all (index 0)
        at async ESMLoader.import (node:internal/modules/esm/loader:528:24)
    
    Node.js v18.8.0
    
  error: 'test failed'
  code: 'ERR_TEST_FAILURE'
  ...
1..1
# tests 1
# pass 0
# fail 1
# cancelled 0
# skipped 0
# todo 0
# duration_ms 0.0666855

Reproduction

package.json

{
  "name": "test",
  "scripts": {
    "test": "node --test  --trace-warnings"
  },
  "type": "module",
  "devDependencies": {
    "aws-sdk-client-mock": "1.0.0"
  }
}

index.test.js

import test from 'node:test'
import { mockClient } from 'aws-sdk-client-mock'
// doesn't matter, doesn't get this far
$ npm test

Environment

  • Node version:18.8
  • Testing lib and version: node:test
  • Typescript version: NA
  • AWS SDK v3 Client mock version: NA
  • AWS JS SDK libs and versions: NA
@willfarrell willfarrell added the bug Something isn't working label Aug 28, 2022
@m-radzikowski
Copy link
Owner

Lib provides both CJS and ESM files. Here it seems the Node chooses to use the CJS version and then complains it's not ESM...

I don't have an idea right now for how to fix this and I will be off for most of next month, but I'm happy to review a PR if someone has an idea for this.

@m-radzikowski m-radzikowski added the help wanted Extra attention is needed label Aug 28, 2022
@dude0001
Copy link

@willfarrell were you able to resolve this?

@takakikasuga
Copy link

takakikasuga commented Nov 9, 2024

+1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

4 participants