Skip to content

Commit 7b9e739

Browse files
Improve tests (sindresorhus#255)
1 parent c97c031 commit 7b9e739

39 files changed

Lines changed: 1361 additions & 1137 deletions

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ jobs:
1010
fail-fast: false
1111
matrix:
1212
node-version:
13+
- 21
1314
- 20
1415
- 18
15-
- 16
1616
steps:
1717
- uses: actions/checkout@v4
1818
- uses: actions/setup-node@v4

estest/index.js

Lines changed: 0 additions & 24 deletions
This file was deleted.

estest/package.json

Lines changed: 0 additions & 5 deletions
This file was deleted.

package.json

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -64,28 +64,29 @@
6464
"@rollup/plugin-json": "^6.1.0",
6565
"@rollup/plugin-node-resolve": "^15.2.3",
6666
"@types/minimist": "^1.2.5",
67-
"ava": "^6.0.1",
68-
"camelcase-keys": "^9.1.2",
67+
"ava": "^6.1.1",
68+
"camelcase-keys": "^9.1.3",
6969
"common-tags": "^2.0.0-alpha.1",
7070
"decamelize": "^6.0.0",
7171
"decamelize-keys": "^2.0.1",
7272
"delete_comments": "^0.0.2",
7373
"execa": "^8.0.1",
74-
"globby": "^14.0.0",
74+
"globby": "^14.0.1",
7575
"indent-string": "^5.0.0",
7676
"minimist-options": "4.1.0",
7777
"normalize-package-data": "^6.0.0",
7878
"read-package-up": "^11.0.0",
7979
"read-pkg": "^9.0.1",
8080
"redent": "^4.0.0",
81-
"rollup": "^4.9.2",
81+
"rollup": "^4.12.0",
8282
"rollup-plugin-dts": "^6.1.0",
8383
"rollup-plugin-license": "^3.2.0",
84+
"stack-utils": "^2.0.6",
8485
"trim-newlines": "^5.0.0",
85-
"tsd": "^0.30.3",
86-
"type-fest": "^4.9.0",
87-
"typescript": "^5.3.3",
88-
"xo": "^0.56.0",
86+
"tsd": "^0.30.7",
87+
"type-fest": "^4.10.3",
88+
"typescript": "~5.3.3",
89+
"xo": "^0.57.0",
8990
"yargs-parser": "^21.1.1"
9091
},
9192
"xo": {
@@ -96,10 +97,5 @@
9697
"ignores": [
9798
"build"
9899
]
99-
},
100-
"ava": {
101-
"files": [
102-
"test/*"
103-
]
104100
}
105101
}

source/index.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,7 @@ const buildResult = (options, parserOptions) => {
2525

2626
normalizePackageData(package_);
2727

28-
let {description} = options;
29-
if (!description && description !== false) {
30-
({description} = package_);
31-
}
28+
let description = options.description ?? package_.description;
3229

3330
description &&= help ? redent(`\n${description}\n`, options.helpIndent) : `\n${description}`;
3431
help = `${description || ''}${help}\n`;

source/parser.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const buildParserFlags = ({flags, booleanDefault}) => {
2323

2424
if (flag.isMultiple) {
2525
flag.type = flag.type ? `${flag.type}-array` : 'array';
26-
flag.default = flag.default ?? [];
26+
flag.default ??= [];
2727
delete flag.isMultiple;
2828
}
2929

test/_utils.js

Lines changed: 55 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,70 @@
1-
import path from 'node:path';
1+
/* eslint-disable ava/no-ignored-test-files */
22
import {fileURLToPath} from 'node:url';
3+
import test from 'ava';
34
import {execa} from 'execa';
5+
import {readPackage} from 'read-pkg';
46
import {createTag, stripIndentTransformer, trimResultTransformer} from 'common-tags';
7+
import StackUtils from 'stack-utils';
58

6-
export const __dirname = path.dirname(fileURLToPath(import.meta.url));
9+
export const defaultFixture = 'fixture.js';
710

8-
const getFixture = fixture => path.join(__dirname, 'fixtures', fixture);
11+
const getFixture = fixture => fileURLToPath(new URL(`fixtures/${fixture}`, import.meta.url));
912

10-
export const spawnFixture = async (fixture = 'fixture.js', args = []) => {
11-
// Allow calling with args first
13+
export const spawnFixture = async (fixture = defaultFixture, arguments_ = [], options = {}) => {
14+
// Allow calling with arguments first
1215
if (Array.isArray(fixture)) {
13-
args = fixture;
14-
fixture = 'fixture.js';
16+
arguments_ = fixture;
17+
fixture = defaultFixture;
1518
}
1619

17-
return execa(getFixture(fixture), args);
20+
return execa(getFixture(fixture), arguments_, options);
1821
};
1922

23+
export {stripIndent} from 'common-tags';
24+
2025
// Use old behavior prior to zspecza/common-tags#165
21-
export const stripIndent = createTag(
26+
export const stripIndentTrim = createTag(
2227
stripIndentTransformer(),
2328
trimResultTransformer(),
2429
);
30+
31+
export const meowPackage = await readPackage();
32+
export const meowVersion = meowPackage.version;
33+
34+
const stackUtils = new StackUtils();
35+
36+
export const stackToErrorMessage = stack => stackUtils.clean(stack).split('\n').at(0);
37+
38+
export const _verifyCli = (baseFixture = defaultFixture) => test.macro(
39+
async (t, {fixture = baseFixture, args, execaOptions, expected, error}) => {
40+
const assertions = await t.try(async tt => {
41+
const arguments_ = args ? args.split(' ') : [];
42+
const {all: output, exitCode} = await spawnFixture(fixture, arguments_, {reject: false, all: true, ...execaOptions});
43+
tt.log('args:', arguments_);
44+
45+
if (error) {
46+
tt.log(`error (code ${exitCode}):\n`, output);
47+
48+
if (typeof error === 'string') {
49+
tt.is(output, error);
50+
tt.is(exitCode, 2);
51+
} else {
52+
const error_ = error.clean ? stackToErrorMessage(output) : output;
53+
54+
tt.is(error_, error.message);
55+
tt.is(exitCode, error.code);
56+
}
57+
} else {
58+
tt.log('output:\n', output);
59+
60+
if (expected) {
61+
tt.is(output, expected);
62+
} else {
63+
tt.pass();
64+
}
65+
}
66+
});
67+
68+
assertions.commit({retainLogs: !assertions.passed});
69+
},
70+
);

test/allow-unknown-flags.js

Lines changed: 0 additions & 68 deletions
This file was deleted.

test/build.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import test from 'ava';
2-
import {readPackage} from 'read-pkg';
32
import meow from '../build/index.js';
4-
import {spawnFixture} from './_utils.js';
3+
import {_verifyCli, meowVersion} from './_utils.js';
4+
5+
const verifyCli = _verifyCli();
56

67
test('main', t => {
78
const cli = meow(`
@@ -39,8 +40,7 @@ test('main', t => {
3940
});
4041
});
4142

42-
test('spawn cli and show version', async t => {
43-
const pkg = await readPackage();
44-
const {stdout} = await spawnFixture(['--version']);
45-
t.is(stdout, pkg.version);
43+
test('spawn cli and show version', verifyCli, {
44+
args: '--version',
45+
expected: meowVersion,
4646
});

0 commit comments

Comments
 (0)