Skip to content

Commit c218a21

Browse files
Fix TS 4.7 compatibility
Export type definitions for CJS imports, make the existing ESM definitions discoverable by TS 4.7. Co-authored-by: Mark Wubben <mark@novemberborn.net>
1 parent 714c474 commit c218a21

File tree

12 files changed

+55
-22
lines changed

12 files changed

+55
-22
lines changed

Diff for: .c8rc.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"exclude": [
44
"{coverage,examples,media,test,test-d,test-tap,types}/**",
55
"*.config.cjs",
6-
"*.d.ts"
6+
"*.d.*(c|m)ts"
77
],
88
"reporter": [
99
"html",

Diff for: .github/workflows/ci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ jobs:
4343
runs-on: ubuntu-latest
4444
strategy:
4545
matrix:
46-
ts-version: [~4.4, ~4.5, ~4.6]
46+
ts-version: [~4.4, ~4.5, ~4.6, ~4.7]
4747
steps:
4848
- uses: actions/checkout@v2
4949
- run: rm .npmrc

Diff for: .xo-config.cjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ module.exports = {
2828
},
2929
overrides: [
3030
{
31-
files: '**/*.d.ts',
31+
files: '**/*.d.*(c|m)ts',
3232
rules: {
3333
'import/extensions': 'off',
3434
},

Diff for: docs/recipes/typescript.md

+17-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ It's worth noting that with this configuration, tests will fail if there are Typ
8282

8383
[![Open in StackBlitz](https://developer.stackblitz.com/img/open_in_stackblitz.svg)](https://stackblitz.com/github/avajs/ava/tree/main/examples/typescript-basic?file=source%2Ftest.ts&terminal=test&view=editor)
8484

85-
Create a `test.ts` file.
85+
Create a `test.ts` file. ESM syntax works best, even if you're targeting CommonJS.
8686

8787
```ts
8888
import test from 'ava';
@@ -94,6 +94,22 @@ test('fn() returns foo', t => {
9494
});
9595
```
9696

97+
You can use CommonJS syntax as well:
98+
99+
```ts
100+
const test = require('ava');
101+
```
102+
103+
This works whether `esModuleInterop` is enabled or not.
104+
105+
`import … = require()` syntax is less elegant. It's best like this:
106+
107+
```ts
108+
import ava = require('ava')
109+
110+
const test = ava.default;
111+
```
112+
97113
## Using [macros](../01-writing-tests.md#reusing-test-logic-through-macros)
98114

99115
Macros can receive additional arguments. AVA can infer these to ensure you're using the macro correctly:

Diff for: entrypoints/index.d.cts

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export {default} from '../index.js';
2+
export * from '../index.js';

Diff for: entrypoints/plugin.d.cts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from '../plugin.js'

Diff for: index.d.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import type {TestFn} from './types/test-fn';
1+
import type {TestFn} from './types/test-fn.js';
22

3-
export * from './types/assertions';
4-
export * from './types/try-fn';
5-
export * from './types/test-fn';
6-
export * from './types/subscribable';
3+
export * from './types/assertions.js';
4+
export * from './types/try-fn.js';
5+
export * from './types/test-fn.js';
6+
export * from './types/subscribable.js';
77

88
/** Call to declare a test, or chain to declare hooks or test modifiers */
99
declare const test: TestFn;

Diff for: lib/create-chain.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,13 @@ export default function createChain(fn, defaults, meta) {
101101

102102
root.meta = meta;
103103

104-
// Our type definition uses ESM syntax; when using CJS with VSCode, the
105-
// auto-completion assumes the root is accessed through `require('ava').default`.
106-
// Placate VSCode by adding a mostly hidden default property on the root.
107-
// This is available through both CJS and ESM imports. We use a proxy so that
108-
// we don't end up with root.default.default.default chains.
104+
// The ESM and CJS type definitions export the chain (`test()` function) as
105+
// the default. TypeScript's CJS output (when `esModuleInterop` is disabled)
106+
// assume `require('ava').default` is available. The same goes for `import ava
107+
// = require('ava')` syntax.
108+
//
109+
// Add `test.default` to make this work. Use a proxy to avoid
110+
// `test.default.default` chains.
109111
Object.defineProperty(root, 'default', {
110112
configurable: false,
111113
enumerable: false,

Diff for: package.json

+16-4
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,25 @@
1010
},
1111
"exports": {
1212
".": {
13-
"import": "./entrypoints/main.mjs",
14-
"require": "./entrypoints/main.cjs"
13+
"import": {
14+
"types": "./index.d.ts",
15+
"default": "./entrypoints/main.mjs"
16+
},
17+
"require": {
18+
"types": "./entrypoints/index.d.cts",
19+
"default": "./entrypoints/main.cjs"
20+
}
1521
},
1622
"./eslint-plugin-helper": "./entrypoints/eslint-plugin-helper.cjs",
1723
"./plugin": {
18-
"import": "./entrypoints/plugin.mjs",
19-
"require": "./entrypoints/plugin.cjs"
24+
"import": {
25+
"types": "./plugin.d.ts",
26+
"default": "./entrypoints/plugin.mjs"
27+
},
28+
"require": {
29+
"types": "./entrypoints/plugin.d.cts",
30+
"default": "./entrypoints/plugin.cjs"
31+
}
2032
}
2133
},
2234
"type": "module",
File renamed without changes.

Diff for: types/test-fn.d.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import type {Assertions} from './assertions';
2-
import type {Subscribable} from './subscribable';
3-
import type {TryFn} from './try-fn';
1+
import type {Assertions} from './assertions.js';
2+
import type {Subscribable} from './subscribable.js';
3+
import type {TryFn} from './try-fn.js';
44

55
/** The `t` value passed to test & hook implementations. */
66
export interface ExecutionContext<Context = unknown> extends Assertions {

Diff for: types/try-fn.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type {Implementation} from './test-fn';
1+
import type {Implementation} from './test-fn.js';
22

33
export type CommitDiscardOptions = {
44
/**

0 commit comments

Comments
 (0)