From 8662ac9fcb9d6eb012b7cc8a7863ec4b19923482 Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Wed, 2 Nov 2022 12:53:28 +0100 Subject: [PATCH] Use Node test runner --- .github/workflows/main.yml | 6 +- package.json | 3 +- test/index.js | 163 ++++++++++++++++++------------------- 3 files changed, 83 insertions(+), 89 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 69924a4..638184c 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -7,8 +7,8 @@ jobs: name: ${{matrix.node}} runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 - - uses: dcodeIO/setup-node-nvm@master + - uses: actions/checkout@v3 + - uses: actions/setup-node@v3 with: node-version: ${{matrix.node}} - run: npm install @@ -17,5 +17,5 @@ jobs: strategy: matrix: node: - - lts/fermium + - lts/hydrogen - node diff --git a/package.json b/package.json index 00dcf6c..7913d71 100644 --- a/package.json +++ b/package.json @@ -39,12 +39,11 @@ "pluralize": "^8.0.0" }, "devDependencies": { - "@types/tape": "^4.0.0", + "@types/node": "^18.0.0", "c8": "^7.0.0", "prettier": "^2.0.0", "remark-cli": "^11.0.0", "remark-preset-wooorm": "^9.0.0", - "tape": "^5.0.0", "type-coverage": "^2.0.0", "typescript": "^4.0.0", "xo": "^0.52.0" diff --git a/test/index.js b/test/index.js index 63e0f07..78d131b 100644 --- a/test/index.js +++ b/test/index.js @@ -1,11 +1,14 @@ import assert from 'node:assert/strict' -import {exec} from 'node:child_process' +import cp from 'node:child_process' import fs from 'node:fs' import {URL} from 'node:url' +import util from 'node:util' import {PassThrough} from 'node:stream' -import test from 'tape' +import test from 'node:test' import {syllable} from '../index.js' +const exec = util.promisify(cp.exec) + const own = {}.hasOwnProperty /** @type {Record} */ @@ -18,44 +21,48 @@ const fixtures = JSON.parse( String(fs.readFileSync(new URL('fixture.json', import.meta.url))) ) -test('api', function (t) { +test('api', function () { const result = syllable('syllables') - t.equal(syllable('SYLLABLES'), result, 'should be case insensitive (1)') - t.equal(syllable('SyLlABlEs'), result, 'should be case insensitive (2)') + assert.equal(syllable('SYLLABLES'), result, 'should be case insensitive (1)') + assert.equal(syllable('SyLlABlEs'), result, 'should be case insensitive (2)') - t.equal(syllable(''), 0, 'should return `0` when empty') + assert.equal(syllable(''), 0, 'should return `0` when empty') - t.equal(syllable('syllables'), 3, 'should work (1)') - t.equal(syllable('hoopty'), 2, 'should work (2)') - t.equal(syllable('mmmm'), 1, 'should work (3)') - t.equal(syllable('am'), 1, 'should work (4)') + assert.equal(syllable('syllables'), 3, 'should work (1)') + assert.equal(syllable('hoopty'), 2, 'should work (2)') + assert.equal(syllable('mmmm'), 1, 'should work (3)') + assert.equal(syllable('am'), 1, 'should work (4)') - t.equal(syllable('wine'), 1, 'should support multiple word-parts (1)') - t.equal(syllable('bottle'), 2, 'should support multiple word-parts (2)') - t.equal(syllable('wine-bottle'), 3, 'should support multiple word-parts (3)') + assert.equal(syllable('wine'), 1, 'should support multiple word-parts (1)') + assert.equal(syllable('bottle'), 2, 'should support multiple word-parts (2)') + assert.equal( + syllable('wine-bottle'), + 3, + 'should support multiple word-parts (3)' + ) - t.equal( + assert.equal( syllable('Zoe'), syllable('Zoë'), 'should support non-ascii characters (1)' ) - t.equal( + assert.equal( syllable('Åland'), syllable('Aland'), 'should support non-ascii characters (2)' ) - t.equal(syllable('Snuffleupagus'), 5, 'GH-25 (snuffleupagus)') - t.equal(syllable('queue'), 1, 'GH-26 (queue)') + assert.equal(syllable('Snuffleupagus'), 5, 'GH-25 (snuffleupagus)') + assert.equal(syllable('queue'), 1, 'GH-26 (queue)') - t.deepEqual( + assert.deepEqual( ['real', 'deal', 'really'].map((d) => syllable(d)), [1, 1, 2], 'GH-31 (real/deal/really)' ) - t.deepEqual( + assert.deepEqual( [ 'awe', 'awearied', @@ -77,7 +84,7 @@ test('api', function (t) { 'GH-32 (awe)' ) - t.deepEqual( + assert.deepEqual( [ 'communion', 'contagion', @@ -105,7 +112,7 @@ test('api', function (t) { 'GH-36 ([gnst]ion$)' ) - t.deepEqual( + assert.deepEqual( [ 'anybody', 'anymore', @@ -123,7 +130,7 @@ test('api', function (t) { 'GH-36 ^any' ) - t.deepEqual( + assert.deepEqual( [ 'anyone', 'everyone', @@ -138,7 +145,7 @@ test('api', function (t) { 'GH-36 one$' ) - t.deepEqual( + assert.deepEqual( [ 'embodying', 'annoying', @@ -177,7 +184,7 @@ test('api', function (t) { 'GH-37 (ying$)' ) - t.deepEqual( + assert.deepEqual( [ 'shreds', 'shredded', @@ -190,7 +197,7 @@ test('api', function (t) { 'GH-37 shredless' ) - t.deepEqual( + assert.deepEqual( [ 'monotheist', 'monotheists', @@ -216,7 +223,7 @@ test('api', function (t) { 'GH-37 (th|d)iest(s|ic)?)' ) - t.deepEqual( + assert.deepEqual( [ 'reminiscense', 'commonsense', @@ -245,72 +252,62 @@ test('api', function (t) { [4, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1], 'GH-39 ([aeiouy]nse$)' ) - - t.end() }) -test('cli', function (t) { - const input = new PassThrough() - - t.plan(8) - - exec('./cli.js syllables', function (error, stdout, stderr) { - t.deepEqual([error, stdout, stderr], [null, '3\n', ''], 'one') - }) - - exec('./cli.js syllables unicorns', function (error, stdout, stderr) { - t.deepEqual([error, stdout, stderr], [null, '6\n', ''], 'two') - }) +test('cli', async function () { + try { + await exec('./cli.js ""') + assert.fail('should not pass') + } catch (error) { + assert.ok(/Usage: syllable/.test(String(error)), 'not enough arguments') + } - exec('./cli.js ""', function (error, stdout, stderr) { - t.deepEqual( - [Boolean(error), stdout, /Usage: syllable/.test(stderr)], - [true, '', true], - 'no arguments' - ) - }) + assert.deepEqual( + await exec('./cli.js syllables'), + {stdout: '3\n', stderr: ''}, + 'one' + ) - const subprocess = exec('./cli.js', function (error, stdout, stderr) { - t.deepEqual([error, stdout, stderr], [null, '6\n', ''], 'stdin') - }) + assert.deepEqual( + await exec('./cli.js syllables unicorns'), + {stdout: '6\n', stderr: ''}, + 'two' + ) - assert(subprocess.stdin, 'expected `stdin` on child process') - input.pipe(subprocess.stdin) - input.write('syllab') - setImmediate(function () { - input.write('les uni') + await new Promise(function (resolve) { + const input = new PassThrough() + const subprocess = cp.exec('./cli.js', function (error, stdout, stderr) { + assert.deepEqual([error, stdout, stderr], [null, '6\n', ''], 'stdin') + setImmediate(resolve) + }) + assert(subprocess.stdin, 'expected stdin on `subprocess`') + input.pipe(subprocess.stdin) + input.write('syllab') setImmediate(function () { - input.end('corns') + input.write(' les uni') + setImmediate(function () { + input.end('corns') + }) }) }) - exec('./cli.js -h', function (error, stdout, stderr) { - t.deepEqual( - [error, /\sUsage: syllable/.test(stdout), stderr], - [null, true, ''], - '-h' - ) - }) + const h = await exec('./cli.js -h') + assert.ok(/\sUsage: syllable/.test(h.stdout), '-h') - exec('./cli.js --help', function (error, stdout, stderr) { - t.deepEqual( - [error, /\sUsage: syllable/.test(stdout), stderr], - [null, true, ''], - '--help' - ) - }) + const help = await exec('./cli.js --help') + assert.ok(/\sUsage: syllable/.test(help.stdout), '-h') - exec('./cli.js -v', function (error, stdout, stderr) { - t.deepEqual([error, stdout, stderr], [null, pack.version + '\n', ''], '-v') - }) + assert.deepEqual( + await exec('./cli.js -v'), + {stdout: pack.version + '\n', stderr: ''}, + '-v' + ) - exec('./cli.js --version', function (error, stdout, stderr) { - t.deepEqual( - [error, stdout, stderr], - [null, pack.version + '\n', ''], - '--version' - ) - }) + assert.deepEqual( + await exec('./cli.js --version'), + {stdout: pack.version + '\n', stderr: ''}, + '--version' + ) }) // Fixtures. @@ -323,7 +320,7 @@ test('cli', function (t) { // // This library focusses on the required Text-Statistics tests (the library // provides both optional and required tests). -test('fixtures', function (t) { +test('fixtures', function () { const overwrite = { // GH-22: , // Barbed is one syllable as well: @@ -336,9 +333,7 @@ test('fixtures', function (t) { for (key in fixtures) { if (own.call(fixtures, key)) { const expected = (key in overwrite ? overwrite : fixtures)[key] - t.equal(syllable(key), expected, key) + assert.equal(syllable(key), expected, key) } } - - t.end() })