Skip to content

Commit

Permalink
fix: default export when importing in pure node ESM (#145)
Browse files Browse the repository at this point in the history
* Fix default export when importing in pure node ESM

* preserve compatibility with code accessing the `default` field

* fix "default" type for cjs require

* add tests
  • Loading branch information
ohana54 committed Dec 4, 2022
1 parent 800f0c0 commit 42aa0cc
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 2 deletions.
2 changes: 1 addition & 1 deletion jest.config.json
@@ -1,6 +1,6 @@
{
"preset": "ts-jest",
"testMatch": ["**/test/*test.ts"],
"testMatch": ["**/test/*test.(j|t)s"],
"transform": {
"<rootDir>/.+\\.ts?$": "ts-jest"
},
Expand Down
5 changes: 4 additions & 1 deletion src/index.ts
Expand Up @@ -3,7 +3,7 @@ import { createLocalServerApi } from './localServerApi'
import { createInitNetworkInterceptor } from './initNetworkInterceptor'
import { createSentryTransport } from './sentryTransport'

export default () => {
function create() {
const testkit = createTestkit()
const sentryTransport = createSentryTransport(testkit)
const initNetworkInterceptor = createInitNetworkInterceptor(testkit)
Expand All @@ -16,3 +16,6 @@ export default () => {
localServer,
}
}

create.default = create
export = create
8 changes: 8 additions & 0 deletions test/cjs-require.test.js
@@ -0,0 +1,8 @@
const sentryTestkit = require('../dist/index')

describe('commonjs require', () => {
it('should allow to require both default and non-default exports', () => {
expect(sentryTestkit()).toBeDefined()
expect(sentryTestkit.default()).toBeDefined()
})
})
10 changes: 10 additions & 0 deletions test/esm-require.test.ts
@@ -0,0 +1,10 @@
import execa from 'execa'

describe('es modules require', () => {
it('should allow to import as native esm', () => {
// test will fail if the command fails
execa.commandSync('node --experimental-vm-modules test/fixtures/esm.mjs', {
stdio: 'inherit',
})
})
})
3 changes: 3 additions & 0 deletions test/fixtures/esm.mjs
@@ -0,0 +1,3 @@
import sentryTestkit from '../../dist/index.js'

sentryTestkit()

0 comments on commit 42aa0cc

Please sign in to comment.