Skip to content

Commit 923ad19

Browse files
authored
test: migrated generate-esm.test.js from tap to node:test (#819)
1 parent 5160568 commit 923ad19

File tree

1 file changed

+44
-42
lines changed

1 file changed

+44
-42
lines changed

test/generate-esm.test.js

Lines changed: 44 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
'use strict'
22

3-
// bailout if a test is broken
4-
// so that the folder can be inspected
5-
process.env.TAP_BAIL = true
6-
7-
const t = require('tap')
3+
const { test } = require('node:test')
4+
const assert = require('node:assert')
85
const {
96
mkdirSync,
107
readFileSync,
@@ -58,8 +55,8 @@ javascriptTemplate.scripts.test = 'node --test test/**/*.test.js'
5855
})
5956
.on('error', cb)
6057
})(function (err) {
61-
t.error(err)
62-
define(t)
58+
assert.ifError(err)
59+
define(test)
6360
})
6461

6562
function define (t) {
@@ -70,43 +67,48 @@ function define (t) {
7067
mkdirSync(workdir, { recursive: true })
7168
})
7269

73-
test('errors if directory exists', (t) => {
70+
test('errors if directory exists', (t, done) => {
7471
t.plan(2)
7572
exec('node generate.js ./test/workdir --esm', (err, stdout) => {
76-
t.equal('directory ./test/workdir already exists', strip(stdout.toString().trim()))
77-
t.equal(1, err.code)
73+
t.assert.strictEqual('directory ./test/workdir already exists', strip(stdout.toString().trim()))
74+
t.assert.strictEqual(1, err.code)
75+
done()
7876
})
7977
})
8078

81-
test('errors if generate doesn\'t have <folder> arguments', (t) => {
79+
test('errors if generate doesn\'t have <folder> arguments', (t, done) => {
8280
t.plan(2)
8381
exec('node generate.js --esm', (err, stdout) => {
84-
t.equal('must specify a directory to \'fastify generate\'', strip(stdout.toString().trim()))
85-
t.equal(1, err.code)
82+
t.assert.strictEqual('must specify a directory to \'fastify generate\'', strip(stdout.toString().trim()))
83+
t.assert.strictEqual(1, err.code)
84+
done()
8685
})
8786
})
8887

89-
test('errors if package.json exists when use generate . and integrate flag is not set', (t) => {
88+
test('errors if package.json exists when use generate . and integrate flag is not set', (t, done) => {
9089
t.plan(2)
9190
exec('node generate.js . --esm', (err, stdout) => {
92-
t.equal('a package.json file already exists in target directory. retry with the --integrate flag to proceed', strip(stdout.toString().trim()))
93-
t.equal(1, err.code)
91+
t.assert.strictEqual('a package.json file already exists in target directory. retry with the --integrate flag to proceed', strip(stdout.toString().trim()))
92+
t.assert.strictEqual(1, err.code)
93+
done()
9494
})
9595
})
9696

97-
test('errors if package.json exists when use generate ./ and integrate flag is not set', (t) => {
97+
test('errors if package.json exists when use generate ./ and integrate flag is not set', (t, done) => {
9898
t.plan(2)
9999
exec('node generate.js ./ --esm', (err, stdout) => {
100-
t.equal('a package.json file already exists in target directory. retry with the --integrate flag to proceed', strip(stdout.toString().trim()))
101-
t.equal(1, err.code)
100+
t.assert.strictEqual('a package.json file already exists in target directory. retry with the --integrate flag to proceed', strip(stdout.toString().trim()))
101+
t.assert.strictEqual(1, err.code)
102+
done()
102103
})
103104
})
104105

105-
test('errors if folder exists', (t) => {
106+
test('errors if folder exists', (t, done) => {
106107
t.plan(2)
107108
exec('node generate.js test --esm', (err, stdout) => {
108-
t.equal('directory test already exists', strip(stdout.toString().trim()))
109-
t.equal(1, err.code)
109+
t.assert.strictEqual('directory test already exists', strip(stdout.toString().trim()))
110+
t.assert.strictEqual(1, err.code)
111+
done()
110112
})
111113
})
112114

@@ -117,7 +119,7 @@ function define (t) {
117119
await verifyPkg(t)
118120
await verifyCopy(t, expected)
119121
} catch (err) {
120-
t.error(err)
122+
t.assert.ifError(err)
121123
}
122124
})
123125

@@ -131,7 +133,7 @@ function define (t) {
131133
await verifyPkg(t)
132134
await verifyCopy(t, expected)
133135
} catch (err) {
134-
t.error(err)
136+
t.assert.ifError(err)
135137
}
136138
})
137139

@@ -146,9 +148,9 @@ function define (t) {
146148

147149
const data = await fsPromises.readFile(path.join(dir, 'package.json'))
148150
const pkg = JSON.parse(data)
149-
t.equal(pkg.scripts.pretest, 'standard')
150-
t.equal(pkg.scripts.lint, 'standard --fix')
151-
t.equal(pkg.devDependencies.standard, cliPkg.devDependencies.standard)
151+
t.assert.strictEqual(pkg.scripts.pretest, 'standard')
152+
t.assert.strictEqual(pkg.scripts.lint, 'standard --fix')
153+
t.assert.strictEqual(pkg.devDependencies.standard, cliPkg.devDependencies.standard)
152154
})
153155

154156
function verifyPkg (t, dir = workdir, pkgName = 'workdir') {
@@ -158,26 +160,26 @@ function define (t) {
158160
readFile(pkgFile, function (err, data) {
159161
err && reject(err)
160162
const pkg = JSON.parse(data)
161-
t.equal(pkg.name, pkgName)
163+
t.assert.strictEqual(pkg.name, pkgName)
162164
// we are not checking author because it depends on global npm configs
163-
t.equal(pkg.version, initVersion)
164-
t.equal(pkg.description, 'This project was bootstrapped with Fastify-CLI.')
165+
t.assert.strictEqual(pkg.version, initVersion)
166+
t.assert.strictEqual(pkg.description, 'This project was bootstrapped with Fastify-CLI.')
165167
// by default this will be ISC but since we have a MIT licensed pkg file in upper dir, npm will set the license to MIT in this case
166168
// so for local tests we need to accept MIT as well
167-
t.ok(pkg.license === 'ISC' || pkg.license === 'MIT')
168-
t.equal(pkg.scripts.test, 'node --test test/**/*.test.js')
169-
t.equal(pkg.scripts.start, 'fastify start -l info app.js')
170-
t.equal(pkg.scripts.dev, 'fastify start -w -l info -P app.js')
171-
t.equal(pkg.dependencies['fastify-cli'], '^' + cliPkg.version)
172-
t.equal(pkg.dependencies.fastify, cliPkg.dependencies.fastify)
173-
t.equal(pkg.dependencies['fastify-plugin'], cliPkg.devDependencies['fastify-plugin'] || cliPkg.dependencies['fastify-plugin'])
174-
t.equal(pkg.dependencies['@fastify/autoload'], cliPkg.devDependencies['@fastify/autoload'])
175-
t.equal(pkg.dependencies['@fastify/sensible'], cliPkg.devDependencies['@fastify/sensible'])
169+
t.assert.ok(pkg.license === 'ISC' || pkg.license === 'MIT')
170+
t.assert.strictEqual(pkg.scripts.test, 'node --test test/**/*.test.js')
171+
t.assert.strictEqual(pkg.scripts.start, 'fastify start -l info app.js')
172+
t.assert.strictEqual(pkg.scripts.dev, 'fastify start -w -l info -P app.js')
173+
t.assert.strictEqual(pkg.dependencies['fastify-cli'], '^' + cliPkg.version)
174+
t.assert.strictEqual(pkg.dependencies.fastify, cliPkg.dependencies.fastify)
175+
t.assert.strictEqual(pkg.dependencies['fastify-plugin'], cliPkg.devDependencies['fastify-plugin'] || cliPkg.dependencies['fastify-plugin'])
176+
t.assert.strictEqual(pkg.dependencies['@fastify/autoload'], cliPkg.devDependencies['@fastify/autoload'])
177+
t.assert.strictEqual(pkg.dependencies['@fastify/sensible'], cliPkg.devDependencies['@fastify/sensible'])
176178
// Test for "type:module"
177-
t.equal(pkg.type, 'module')
179+
t.assert.strictEqual(pkg.type, 'module')
178180

179181
const testGlob = pkg.scripts.test.split(' ', 3)[2].replace(/"/g, '')
180-
t.equal(minimatch.match(['test/services/plugins/more/test/here/ok.test.js'], testGlob).length, 1)
182+
t.assert.strictEqual(minimatch.match(['test/services/plugins/more/test/here/ok.test.js'], testGlob).length, 1)
181183
resolve()
182184
})
183185
})
@@ -194,7 +196,7 @@ function define (t) {
194196
try {
195197
const data = readFileSync(file)
196198
file = file.replace(workdir, '')
197-
t.same(data.toString().replace(/\r\n/g, '\n'), expected[file], file + ' matching')
199+
t.assert.strictEqual(data.toString().replace(/\r\n/g, '\n'), expected[file], file + ' matching')
198200
} catch (err) {
199201
reject(err)
200202
}

0 commit comments

Comments
 (0)