Skip to content

Commit

Permalink
Upgrade to AVA 4 and switch to ES modules
Browse files Browse the repository at this point in the history
  • Loading branch information
novemberborn committed Nov 3, 2021
1 parent 0db80e4 commit 0562f4c
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 38 deletions.
5 changes: 0 additions & 5 deletions .xo-config.cjs

This file was deleted.

16 changes: 2 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,17 @@
# @ava/get-port

Experimental AVA plugin which works like [`get-port`](https://github.com/sindresorhus/get-port), but ensures the port is locked across all test files.
AVA 4 plugin which works like [`get-port`](https://github.com/sindresorhus/get-port), but ensures the port is locked across all test files.

Install this as a development dependency alongside AVA itself:

```console
npm install --save-dev @ava/get-port
```

Then make sure you've enabled the shared workers experiment:

`ava.config.js`:

```js
export default {
nonSemVerExperiments: {
sharedWorkers: true
}
};
```

## Usage

```ts
const {default: getPort} = require('@ava/get-port');
import getPort from '@ava/get-port';

test.before('get port', async t => {
t.context.port = await getPort();
Expand Down
4 changes: 1 addition & 3 deletions ava.config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
export default { // eslint-disable-line import/no-anonymous-default-export
files: ['!dist/**'],
nonSemVerExperiments: {
sharedWorkers: true,
},
typescript: {
compile: false,
rewritePaths: {
'test/': 'dist/test/',
},
Expand Down
13 changes: 8 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
"files": [
"dist/source"
],
"main": "dist/source",
"type": "module",
"exports": {
".": "./dist/source/index.js"
},
"types": "dist/source/index.d.ts",
"scripts": {
"build": "del-cli dist && tsc",
Expand All @@ -26,17 +29,17 @@
"repository": "avajs/get-port",
"license": "MIT",
"devDependencies": {
"@ava/typescript": "^1.1.1",
"@sindresorhus/tsconfig": "^0.7.0",
"ava": "^3.13.0",
"@ava/typescript": "^3.0.0",
"@sindresorhus/tsconfig": "^2.0.0",
"ava": "4.0.0-rc.1",
"c8": "^7.10.0",
"del-cli": "^4.0.1",
"tsd": "^0.18.0",
"typescript": "^4.4.4",
"xo": "^0.46.4"
},
"dependencies": {
"@ava/cooperate": "^0.1.0"
"@ava/cooperate": "^1.0.0"
},
"peerDependencies": {
"ava": "*"
Expand Down
2 changes: 1 addition & 1 deletion source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import crypto from 'node:crypto';
import net from 'node:net';
import {SharedContext} from '@ava/cooperate';

const context = new SharedContext(__filename);
const context = new SharedContext(import.meta.url);

// Reserve a range of 16 addresses at a random offset.
const reserveRange = async (): Promise<number[]> => {
Expand Down
2 changes: 1 addition & 1 deletion test-d/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {expectError} from 'tsd';
import getPort from '..';
import getPort from '../source/index.js';

expectError(await getPort({port: 1024}));
7 changes: 4 additions & 3 deletions test/test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import net from 'node:net';
import {promisify} from 'node:util';
import test from 'ava';
import getPort from '../source';
import getPort from '../source/index.js';

test('gets up to 16 ports in a block', async t => {
const first = await getPort();
Expand All @@ -28,8 +28,9 @@ test('port can be bound', async t => {
t.teardown(() => server.close());

const port = await getPort();
await promisify(server.listen.bind(server))(port);
t.is((server.address() as any).port, port);
const listen: (port: number) => Promise<void> = promisify(server.listen.bind(server));
await listen(port);
t.is((server.address() as net.AddressInfo).port, port);
});

test('can get ports simultaneously', async t => {
Expand Down
7 changes: 1 addition & 6 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
{
"extends": "@sindresorhus/tsconfig",
"extends": "@sindresorhus/tsconfig/tsconfig.json",
"compilerOptions": {
"outDir": "dist",
"target": "es2019", // Node.js 12
"lib": [
"es2019"
],
"esModuleInterop": true,
"sourceMap": true
},
"include": [
Expand Down

0 comments on commit 0562f4c

Please sign in to comment.