Skip to content

Commit 2886d70

Browse files
authored
Fix esm export (#276)
1 parent a366d4c commit 2886d70

File tree

170 files changed

+755
-583
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

170 files changed

+755
-583
lines changed

.changeset/heavy-planes-greet.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"earl": patch
3+
---
4+
5+
Fix esm exports

packages/e2e/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88
"lint": "eslint --ext .ts,.js --max-warnings 0 test-esbuild test-node test-ts-node",
99
"lint:fix": "pnpm lint --fix",
1010
"typecheck": "tsc --noEmit",
11-
"test": "pnpm test:babel && pnpm test:esbuild && pnpm test:node && pnpm test:ts-node",
11+
"test": "pnpm test:babel && pnpm test:esbuild && pnpm test:mjs && pnpm test:node && pnpm test:ts-node",
1212
"test:babel": "mocha --config test-babel/.mocharc.js",
1313
"test:esbuild": "mocha --config test-esbuild/.mocharc.js",
14+
"test:mjs": "mocha --config test-mjs/.mocharc.js",
1415
"test:node": "mocha --config test-node/.mocharc.js",
1516
"test:ts-node": "mocha --config test-ts-node/.mocharc.js",
1617
"test:fix": "pnpm lint:fix && pnpm format:fix && pnpm typecheck && pnpm test"

packages/e2e/test-mjs/.mocharc.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
process.env.NODE_ENV = 'test'
2+
module.exports = {
3+
spec: 'test-mjs/**/*.test.mjs',
4+
watchExtensions: 'mjs',
5+
extension: 'mjs',
6+
}
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
import { expect } from 'earl'
2+
import Parser from 'error-stack-parser'
3+
import { utils } from 'mocha'
4+
5+
function captureError(fn) {
6+
try {
7+
fn()
8+
} catch (error) {
9+
if (error instanceof Error) {
10+
return error
11+
}
12+
throw error
13+
}
14+
throw new Error('No error captured!')
15+
}
16+
17+
async function captureErrorAsync(fn) {
18+
try {
19+
await fn()
20+
} catch (error) {
21+
if (error instanceof Error) {
22+
return error
23+
}
24+
throw error
25+
}
26+
throw new Error('No error captured!')
27+
}
28+
29+
function getStack(error) {
30+
const stack = utils.stackTraceFilter()(error.stack ?? '')
31+
return Parser.parse({ stack }).map((x) => ({
32+
at: x.functionName,
33+
file: x.fileName,
34+
}))
35+
}
36+
37+
describe('stack traces', () => {
38+
it('captures correct synchronous stack traces', () => {
39+
const error = captureError(() => expect(1).toEqual(2))
40+
const stack = getStack(error)
41+
42+
expect(stack[0]).toEqual({
43+
at: 'expect().toEqual',
44+
file: expect.includes('stack-traces.test.mjs'),
45+
})
46+
expect(stack[1]).toEqual({
47+
at: expect.a(String),
48+
file: expect.includes('stack-traces.test.mjs'),
49+
})
50+
expect(stack[2]).toEqual({
51+
at: expect.a(String),
52+
file: expect.includes('stack-traces.test.mjs'),
53+
})
54+
})
55+
56+
it('captures correct negated synchronous stack traces', () => {
57+
const error = captureError(() => expect(1).not.toEqual(1))
58+
const stack = getStack(error)
59+
60+
expect(stack[0]).toEqual({
61+
at: 'expect().not.toEqual',
62+
file: expect.includes('stack-traces.test.mjs'),
63+
})
64+
expect(stack[1]).toEqual({
65+
at: expect.a(String),
66+
file: expect.includes('stack-traces.test.mjs'),
67+
})
68+
expect(stack[2]).toEqual({
69+
at: expect.a(String),
70+
file: expect.includes('stack-traces.test.mjs'),
71+
})
72+
})
73+
74+
it('captures correct synchronous stack traces for toThrow', () => {
75+
const error = captureError(() =>
76+
expect(() => {
77+
throw new Error('foo')
78+
}).toThrow('bar'),
79+
)
80+
const stack = getStack(error)
81+
82+
expect(stack[0]).toEqual({
83+
at: 'expect().toThrow',
84+
file: expect.includes('stack-traces.test.mjs'),
85+
})
86+
expect(stack[1]).toEqual({
87+
at: expect.a(String),
88+
file: expect.includes('stack-traces.test.mjs'),
89+
})
90+
expect(stack[2]).toEqual({
91+
at: expect.a(String),
92+
file: expect.includes('stack-traces.test.mjs'),
93+
})
94+
})
95+
96+
it('captures correct asynchronous stack traces', async () => {
97+
const error = await captureErrorAsync(async () =>
98+
expect(await Promise.resolve(1)).toEqual(2),
99+
)
100+
const stack = getStack(error)
101+
102+
expect(stack[0]).toEqual({
103+
at: 'expect().toEqual',
104+
file: expect.includes('stack-traces.test.mjs'),
105+
})
106+
expect(stack[1]).toEqual({
107+
at: expect.a(String),
108+
file: expect.includes('stack-traces.test.mjs'),
109+
})
110+
expect(stack[2]).toEqual({
111+
at: expect.a(String),
112+
file: expect.includes('stack-traces.test.mjs'),
113+
})
114+
})
115+
116+
it('captures correct asynchronous stack traces for toBeRejectedWith', async () => {
117+
const error = await captureErrorAsync(() =>
118+
expect(async () => {
119+
throw new Error('foo')
120+
}).toBeRejectedWith('bar'),
121+
)
122+
const stack = getStack(error)
123+
124+
expect(stack[0]).toEqual({
125+
at: 'expect().toBeRejectedWith',
126+
file: expect.includes('stack-traces.test.mjs'),
127+
})
128+
expect(stack[1]).toEqual({
129+
at: expect.a(String),
130+
file: expect.includes('stack-traces.test.mjs'),
131+
})
132+
expect(stack[2]).toEqual({
133+
at: expect.a(String),
134+
file: expect.includes('stack-traces.test.mjs'),
135+
})
136+
})
137+
})

packages/earl/.eslintrc.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
module.exports = {
22
extends: '../../.eslintrc.json',
3+
rules: {
4+
'import/extensions': ['error', 'ignorePackages'],
5+
'import/no-unresolved': 'off',
6+
'import/no-extraneous-dependencies': 'off',
7+
},
38
}

packages/earl/.mocharc.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ process.env.NODE_ENV = 'test'
22
module.exports = {
33
spec: 'src/**/*.test.ts',
44
file: 'src/test/setup.ts',
5-
require: 'ts-node/register/transpile-only',
6-
watchExtensions: 'ts',
7-
extension: 'ts',
5+
// require: 'ts-node/register/transpile-only',
6+
'node-option': [
7+
'experimental-specifier-resolution=node',
8+
'loader=ts-node/esm/transpile-only',
9+
],
10+
watchExtensions: ['js', 'ts'],
11+
extension: ['js', 'ts'],
812
}

packages/earl/package.json

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,10 @@
2727
"version": "1.0.0",
2828
"main": "./dist/cjs/index.js",
2929
"module": "./dist/esm/index.js",
30-
"types": "./dist/esm/index.d.ts",
3130
"exports": {
3231
".": {
33-
"import": {
34-
"types": "./dist/esm/index.d.ts",
35-
"default": "./dist/esm/index.js"
36-
},
37-
"require": {
38-
"types": "./dist/cjs/index.d.ts",
39-
"default": "./dist/cjs/index.js"
40-
}
32+
"import": "./dist/esm/index.js",
33+
"require": "./dist/cjs/index.js"
4134
}
4235
},
4336
"files": [
@@ -56,8 +49,8 @@
5649
"typecheck": "tsc --noEmit",
5750
"clean": "rimraf dist",
5851
"build": "pnpm clean && pnpm build:cjs && pnpm build:esm",
59-
"build:cjs": "tsc -p ./tsconfig.cjs.json",
60-
"build:esm": "tsc -p ./tsconfig.esm.json",
52+
"build:cjs": "tsc -p ./tsconfig.cjs.json && cp static/package-cjs.json dist/cjs/package.json",
53+
"build:esm": "tsc -p ./tsconfig.esm.json && cp static/package-esm.json dist/esm/package.json",
6154
"build:watch": "pnpm clean && tsc -p ./tsconfig.prod.json --watch",
6255
"build:watch:test": "pnpm clean && tsc -p ./tsconfig.json --watch",
6356
"test": "mocha",

packages/earl/src/Control.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { AssertionError } from './errors'
2-
import { format } from './format'
1+
import { AssertionError } from './errors/index.js'
2+
import { format } from './format/index.js'
33

44
export interface ValidationResult {
55
success: boolean

packages/earl/src/errors/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export { AssertionError } from './AssertionError'
1+
export { AssertionError } from './AssertionError.js'

packages/earl/src/errors/stack-traces.test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { expect } from 'chai'
2-
import * as errorStackParser from 'error-stack-parser'
2+
import ErrorStackParser from 'error-stack-parser'
33

4-
import { Control } from '../Control'
5-
import { expect as earl } from '../index'
6-
import { AssertionError } from './AssertionError'
4+
import { Control } from '../Control.js'
5+
import { expect as earl } from '../index.js'
6+
import { AssertionError } from './AssertionError.js'
77

88
describe('stack traces for errors', () => {
99
it('cleans stack traces for sync errors', () => {
@@ -12,7 +12,7 @@ describe('stack traces for errors', () => {
1212
expect.fail('should throw')
1313
} catch (e: any) {
1414
expect(e).to.be.instanceOf(AssertionError, 'Earl did not throw')
15-
const stackTrace = errorStackParser.parse(e)
15+
const stackTrace = ErrorStackParser.parse(e)
1616

1717
expect(stackTrace[0]?.fileName?.endsWith('stack-traces.test.ts')).to.be
1818
.true
@@ -25,7 +25,7 @@ describe('stack traces for errors', () => {
2525
expect.fail('should throw')
2626
} catch (e: any) {
2727
expect(e).to.be.instanceOf(AssertionError, 'Earl did not throw')
28-
const stackTrace = errorStackParser.parse(e)
28+
const stackTrace = ErrorStackParser.parse(e)
2929

3030
expect(stackTrace[0]?.fileName?.endsWith('stack-traces.test.ts')).to.be
3131
.true
@@ -41,6 +41,6 @@ describe('stack traces for errors', () => {
4141
return nestedGetControl()
4242
}
4343
const control = nestedValidator()
44-
expect(control.file).to.equal(__filename)
44+
expect(control.file).to.equal(import.meta.url)
4545
})
4646
})

0 commit comments

Comments
 (0)