Skip to content

Commit dcefa58

Browse files
authored
test: migrated generate.test.js from tap to node:test (#823)
1 parent 44a2221 commit dcefa58

File tree

1 file changed

+43
-41
lines changed

1 file changed

+43
-41
lines changed

test/generate.test.js

Lines changed: 43 additions & 41 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,
@@ -53,8 +50,8 @@ const initVersion = execSync('npm get init-version').toString().trim()
5350
})
5451
.on('error', cb)
5552
})(function (err) {
56-
t.error(err)
57-
define(t)
53+
assert.ifError(err)
54+
define(test)
5855
})
5956

6057
function define (t) {
@@ -65,43 +62,48 @@ function define (t) {
6562
mkdirSync(workdir, { recursive: true })
6663
})
6764

68-
test('errors if directory exists', (t) => {
65+
test('errors if directory exists', (t, done) => {
6966
t.plan(2)
7067
exec('node generate.js ./test/workdir', (err, stdout) => {
71-
t.equal('directory ./test/workdir already exists', strip(stdout.toString().trim()))
72-
t.equal(1, err.code)
68+
t.assert.strictEqual('directory ./test/workdir already exists', strip(stdout.toString().trim()))
69+
t.assert.strictEqual(1, err.code)
70+
done()
7371
})
7472
})
7573

76-
test('errors if generate doesn\'t have <folder> arguments', (t) => {
74+
test('errors if generate doesn\'t have <folder> arguments', (t, done) => {
7775
t.plan(2)
7876
exec('node generate.js', (err, stdout) => {
79-
t.equal('must specify a directory to \'fastify generate\'', strip(stdout.toString().trim()))
80-
t.equal(1, err.code)
77+
t.assert.strictEqual('must specify a directory to \'fastify generate\'', strip(stdout.toString().trim()))
78+
t.assert.strictEqual(1, err.code)
79+
done()
8180
})
8281
})
8382

84-
test('errors if package.json exists when use generate . and integrate flag is not set', (t) => {
83+
test('errors if package.json exists when use generate . and integrate flag is not set', (t, done) => {
8584
t.plan(2)
8685
exec('node generate.js .', (err, stdout) => {
87-
t.equal('a package.json file already exists in target directory. retry with the --integrate flag to proceed', strip(stdout.toString().trim()))
88-
t.equal(1, err.code)
86+
t.assert.strictEqual('a package.json file already exists in target directory. retry with the --integrate flag to proceed', strip(stdout.toString().trim()))
87+
t.assert.strictEqual(1, err.code)
88+
done()
8989
})
9090
})
9191

92-
test('errors if package.json exists when use generate ./ and integrate flag is not set', (t) => {
92+
test('errors if package.json exists when use generate ./ and integrate flag is not set', (t, done) => {
9393
t.plan(2)
9494
exec('node generate.js ./', (err, stdout) => {
95-
t.equal('a package.json file already exists in target directory. retry with the --integrate flag to proceed', strip(stdout.toString().trim()))
96-
t.equal(1, err.code)
95+
t.assert.strictEqual('a package.json file already exists in target directory. retry with the --integrate flag to proceed', strip(stdout.toString().trim()))
96+
t.assert.strictEqual(1, err.code)
97+
done()
9798
})
9899
})
99100

100-
test('errors if folder exists', (t) => {
101+
test('errors if folder exists', (t, done) => {
101102
t.plan(2)
102103
exec('node generate.js test', (err, stdout) => {
103-
t.equal('directory test already exists', strip(stdout.toString().trim()))
104-
t.equal(1, err.code)
104+
t.assert.strictEqual('directory test already exists', strip(stdout.toString().trim()))
105+
t.assert.strictEqual(1, err.code)
106+
done()
105107
})
106108
})
107109

@@ -112,7 +114,7 @@ function define (t) {
112114
await verifyPkg(t)
113115
await verifyCopy(t, expected)
114116
} catch (err) {
115-
t.error(err)
117+
t.assert.ifError(err)
116118
}
117119
})
118120

@@ -126,7 +128,7 @@ function define (t) {
126128
await verifyPkg(t)
127129
await verifyCopy(t, expected)
128130
} catch (err) {
129-
t.error(err)
131+
t.assert.ifError(err)
130132
}
131133
})
132134

@@ -141,9 +143,9 @@ function define (t) {
141143

142144
const data = await fsPromises.readFile(path.join(dir, 'package.json'))
143145
const pkg = JSON.parse(data)
144-
t.equal(pkg.scripts.pretest, 'standard')
145-
t.equal(pkg.scripts.lint, 'standard --fix')
146-
t.equal(pkg.devDependencies.standard, cliPkg.devDependencies.standard)
146+
t.assert.strictEqual(pkg.scripts.pretest, 'standard')
147+
t.assert.strictEqual(pkg.scripts.lint, 'standard --fix')
148+
t.assert.strictEqual(pkg.devDependencies.standard, cliPkg.devDependencies.standard)
147149
})
148150

149151
function verifyPkg (t, dir = workdir, pkgName = 'workdir') {
@@ -153,24 +155,24 @@ function define (t) {
153155
readFile(pkgFile, function (err, data) {
154156
err && reject(err)
155157
const pkg = JSON.parse(data)
156-
t.equal(pkg.name, pkgName)
158+
t.assert.strictEqual(pkg.name, pkgName)
157159
// we are not checking author because it depends on global npm configs
158-
t.equal(pkg.version, initVersion)
159-
t.equal(pkg.description, 'This project was bootstrapped with Fastify-CLI.')
160+
t.assert.strictEqual(pkg.version, initVersion)
161+
t.assert.strictEqual(pkg.description, 'This project was bootstrapped with Fastify-CLI.')
160162
// 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
161163
// so for local tests we need to accept MIT as well
162-
t.ok(pkg.license === 'ISC' || pkg.license === 'MIT')
163-
t.equal(pkg.scripts.test, 'node --test test/**/*.test.js')
164-
t.equal(pkg.scripts.start, 'fastify start -l info app.js')
165-
t.equal(pkg.scripts.dev, 'fastify start -w -l info -P app.js')
166-
t.equal(pkg.dependencies['fastify-cli'], '^' + cliPkg.version)
167-
t.equal(pkg.dependencies.fastify, cliPkg.dependencies.fastify)
168-
t.equal(pkg.dependencies['fastify-plugin'], cliPkg.devDependencies['fastify-plugin'] || cliPkg.dependencies['fastify-plugin'])
169-
t.equal(pkg.dependencies['@fastify/autoload'], cliPkg.devDependencies['@fastify/autoload'])
170-
t.equal(pkg.dependencies['@fastify/sensible'], cliPkg.devDependencies['@fastify/sensible'])
164+
t.assert.ok(pkg.license === 'ISC' || pkg.license === 'MIT')
165+
t.assert.strictEqual(pkg.scripts.test, 'node --test test/**/*.test.js')
166+
t.assert.strictEqual(pkg.scripts.start, 'fastify start -l info app.js')
167+
t.assert.strictEqual(pkg.scripts.dev, 'fastify start -w -l info -P app.js')
168+
t.assert.strictEqual(pkg.dependencies['fastify-cli'], '^' + cliPkg.version)
169+
t.assert.strictEqual(pkg.dependencies.fastify, cliPkg.dependencies.fastify)
170+
t.assert.strictEqual(pkg.dependencies['fastify-plugin'], cliPkg.devDependencies['fastify-plugin'] || cliPkg.dependencies['fastify-plugin'])
171+
t.assert.strictEqual(pkg.dependencies['@fastify/autoload'], cliPkg.devDependencies['@fastify/autoload'])
172+
t.assert.strictEqual(pkg.dependencies['@fastify/sensible'], cliPkg.devDependencies['@fastify/sensible'])
171173

172174
const testGlob = pkg.scripts.test.split(' ', 3)[2]
173-
t.equal(minimatch.match(['test/services/plugins/more/test/here/ok.test.js'], testGlob).length, 1)
175+
t.assert.strictEqual(minimatch.match(['test/services/plugins/more/test/here/ok.test.js'], testGlob).length, 1)
174176
resolve()
175177
})
176178
})
@@ -187,7 +189,7 @@ function define (t) {
187189
try {
188190
const data = readFileSync(file)
189191
file = file.replace(workdir, '')
190-
t.same(data.toString().replace(/\r\n/g, '\n'), expected[file], file + ' matching')
192+
t.assert.deepStrictEqual(data.toString().replace(/\r\n/g, '\n'), expected[file], file + ' matching')
191193
} catch (err) {
192194
reject(err)
193195
}

0 commit comments

Comments
 (0)