Skip to content

Commit ecc1a49

Browse files
committed
feat(test): adopt node:test
1 parent 7d883b5 commit ecc1a49

14 files changed

+119
-101
lines changed

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,12 @@
4040
"bindings": "^1.5.0",
4141
"cross-env": "^7.0.3",
4242
"eslint": "^9.16.0",
43-
"mocha": "^11.0.1",
4443
"nan": "^2.14.2",
4544
"neostandard": "^0.11.9",
4645
"require-inject": "^1.4.4"
4746
},
4847
"scripts": {
4948
"lint": "eslint \"*/*.js\" \"test/**/*.js\" \".github/**/*.js\"",
50-
"test": "cross-env NODE_GYP_NULL_LOGGER=true mocha --timeout 15000 test/test-download.js test/test-*"
49+
"test": "cross-env NODE_GYP_NULL_LOGGER=true node --test test/test-*"
5150
}
5251
}

test/test-addon.js

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

3-
const { describe, it } = require('mocha')
3+
const { describe, it } = require('node:test')
44
const assert = require('assert')
55
const path = require('path')
66
const fs = require('graceful-fs')
@@ -42,9 +42,9 @@ function checkCharmapValid () {
4242
}
4343

4444
describe('addon', function () {
45-
it('build simple addon', async function () {
46-
this.timeout(platformTimeout(1, { win32: 5 }))
45+
const timeout = platformTimeout(1, { win32: 5 })
4746

47+
it('build simple addon', { timeout }, async function () {
4848
// Set the loglevel otherwise the output disappears when run via 'npm test'
4949
const cmd = [nodeGyp, 'rebuild', '-C', addonPath, '--loglevel=verbose']
5050
const [err, logLines] = await execFile(cmd)
@@ -54,9 +54,9 @@ describe('addon', function () {
5454
assert.strictEqual(runHello(), 'world')
5555
})
5656

57-
it('build simple addon in path with non-ascii characters', async function () {
57+
it('build simple addon in path with non-ascii characters', { timeout }, async function (t) {
5858
if (!checkCharmapValid()) {
59-
return this.skip('python console app can\'t encode non-ascii character.')
59+
return t.skip('python console app can\'t encode non-ascii character.')
6060
}
6161

6262
// Select non-ascii characters by current encoding
@@ -67,11 +67,9 @@ describe('addon', function () {
6767
}[getEncoding()]
6868
// If encoding is UTF-8 or other then no need to test
6969
if (!testDirName) {
70-
return this.skip('no need to test')
70+
return t.skip('no need to test')
7171
}
7272

73-
this.timeout(platformTimeout(1, { win32: 5 }))
74-
7573
let data
7674
const configPath = path.join(addonPath, 'build', 'config.gypi')
7775
try {
@@ -115,9 +113,7 @@ describe('addon', function () {
115113
assert.strictEqual(runHello(), 'world')
116114
})
117115

118-
it('addon works with renamed host executable', async function () {
119-
this.timeout(platformTimeout(1, { win32: 5 }))
120-
116+
it('addon works with renamed host executable', { timeout }, async function () {
121117
const notNodePath = path.join(os.tmpdir(), 'notnode' + path.extname(process.execPath))
122118
fs.copyFileSync(process.execPath, notNodePath)
123119

test/test-configure-nodedir.js

Lines changed: 46 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict'
22

3-
const { describe, it } = require('mocha')
4-
const assert = require('assert')
3+
const { describe, it } = require('node:test')
54
const path = require('path')
65
const os = require('os')
76
const gyp = require('../lib/node-gyp')
@@ -46,8 +45,7 @@ const SPAWN_RESULT = cb => ({ on: function () { cb() } })
4645

4746
const driveLetter = os.platform() === 'win32' ? `${process.cwd().split(path.sep)[0]}` : ''
4847
function checkTargetPath (target, value) {
49-
let targetPath = path.join(path.sep, target, 'include',
50-
'node', 'common.gypi')
48+
let targetPath = path.join(path.sep, target, 'include', 'node', 'common.gypi')
5149
if (process.platform === 'win32') {
5250
targetPath = driveLetter + targetPath
5351
}
@@ -56,68 +54,76 @@ function checkTargetPath (target, value) {
5654
}
5755

5856
describe('configure-nodedir', function () {
59-
it('configure nodedir with node-gyp command line', function (done) {
57+
it('configure nodedir with node-gyp command line', async function () {
6058
const prog = gyp()
6159
prog.parseArgv(['dummy_prog', 'dummy_script', '--nodedir=' + path.sep + 'usr'])
6260

63-
prog.spawn = function (program, args) {
64-
for (let i = 0; i < args.length; i++) {
65-
if (checkTargetPath('usr', args[i])) {
66-
return SPAWN_RESULT(done)
61+
await new Promise((resolve, reject) => {
62+
prog.spawn = function (program, args) {
63+
for (let i = 0; i < args.length; i++) {
64+
if (checkTargetPath('usr', args[i])) {
65+
return SPAWN_RESULT(resolve)
66+
}
6767
}
68-
};
69-
assert.fail()
70-
}
71-
configure(prog, [], assert.fail)
68+
reject(new Error('Expected nodedir path not found'))
69+
}
70+
configure(prog, [], reject)
71+
})
7272
})
7373

7474
if (process.config.variables.use_prefix_to_find_headers) {
75-
it('use-prefix-to-find-headers build time option - match', function (done) {
75+
it('use-prefix-to-find-headers build time option - match', async function () {
7676
const prog = gyp()
7777
prog.parseArgv(['dummy_prog', 'dummy_script'])
7878

79-
prog.spawn = function (program, args) {
80-
for (let i = 0; i < args.length; i++) {
79+
await new Promise((resolve, reject) => {
80+
prog.spawn = function (program, args) {
8181
const nodedir = process.config.variables.node_prefix
82-
if (checkTargetPath(nodedir, args[i])) {
83-
return SPAWN_RESULT(done)
82+
for (let i = 0; i < args.length; i++) {
83+
if (checkTargetPath(nodedir, args[i])) {
84+
return SPAWN_RESULT(resolve)
85+
}
8486
}
85-
};
86-
assert.fail()
87-
}
88-
configure(prog, [], assert.fail)
87+
reject(new Error('Expected nodedir path not found'))
88+
}
89+
configure(prog, [], reject)
90+
})
8991
})
9092

91-
it('use-prefix-to-find-headers build time option - no match', function (done) {
93+
it('use-prefix-to-find-headers build time option - no match', async function () {
9294
const prog = gyp()
9395
prog.parseArgv(['dummy_prog', 'dummy_script'])
9496

95-
prog.spawn = function (program, args) {
96-
for (let i = 0; i < args.length; i++) {
97+
await new Promise((resolve, reject) => {
98+
prog.spawn = function (program, args) {
9799
const nodedir = process.config.variables.node_prefix
98-
if (checkTargetPath(nodedir, args[i])) {
99-
assert.fail()
100+
for (let i = 0; i < args.length; i++) {
101+
if (checkTargetPath(nodedir, args[i])) {
102+
return reject(new Error('Unexpected match found'))
103+
}
100104
}
101-
};
102-
return SPAWN_RESULT(done)
103-
}
104-
configure2(prog, [], assert.fail)
105+
return SPAWN_RESULT(resolve)
106+
}
107+
configure2(prog, [], reject)
108+
})
105109
})
106110

107-
it('use-prefix-to-find-headers build time option, target specified', function (done) {
111+
it('use-prefix-to-find-headers build time option, target specified', async function () {
108112
const prog = gyp()
109113
prog.parseArgv(['dummy_prog', 'dummy_script', '--target=8.0.0'])
110114

111-
prog.spawn = function (program, args) {
112-
for (let i = 0; i < args.length; i++) {
115+
await new Promise((resolve, reject) => {
116+
prog.spawn = function (program, args) {
113117
const nodedir = process.config.variables.node_prefix
114-
if (checkTargetPath(nodedir, args[i])) {
115-
assert.fail()
118+
for (let i = 0; i < args.length; i++) {
119+
if (checkTargetPath(nodedir, args[i])) {
120+
return reject(new Error('Unexpected match found for target'))
121+
}
116122
}
117-
};
118-
return SPAWN_RESULT(done)
119-
}
120-
configure(prog, [], assert.fail)
123+
return SPAWN_RESULT(resolve)
124+
}
125+
configure(prog, [], reject)
126+
})
121127
})
122128
}
123129
})

test/test-configure-python.js

Lines changed: 47 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict'
22

3-
const { describe, it } = require('mocha')
3+
const { describe, it } = require('node:test')
44
const assert = require('assert')
55
const path = require('path')
66
const { devDir } = require('./common')
@@ -25,54 +25,75 @@ const SEPARATOR = process.platform === 'win32' ? ';' : ':'
2525
const SPAWN_RESULT = cb => ({ on: function () { cb() } })
2626

2727
describe('configure-python', function () {
28-
it('configure PYTHONPATH with no existing env', function (done) {
28+
it('configure PYTHONPATH with no existing env', async function () {
2929
delete process.env.PYTHONPATH
3030

3131
const prog = gyp()
3232
prog.parseArgv([])
33-
prog.spawn = function () {
34-
assert.strictEqual(process.env.PYTHONPATH, EXPECTED_PYPATH)
35-
return SPAWN_RESULT(done)
36-
}
37-
prog.devDir = devDir
38-
configure(prog, [], assert.fail)
33+
34+
await new Promise((resolve, reject) => {
35+
prog.spawn = function () {
36+
try {
37+
assert.strictEqual(process.env.PYTHONPATH, EXPECTED_PYPATH)
38+
return SPAWN_RESULT(resolve)
39+
} catch (err) {
40+
reject(err)
41+
}
42+
}
43+
prog.devDir = devDir
44+
configure(prog, [], reject)
45+
})
3946
})
4047

41-
it('configure PYTHONPATH with existing env of one dir', function (done) {
48+
it('configure PYTHONPATH with existing env of one dir', async function () {
4249
const existingPath = path.join('a', 'b')
4350
process.env.PYTHONPATH = existingPath
4451

4552
const prog = gyp()
4653
prog.parseArgv([])
47-
prog.spawn = function () {
48-
assert.strictEqual(process.env.PYTHONPATH, [EXPECTED_PYPATH, existingPath].join(SEPARATOR))
4954

50-
const dirs = process.env.PYTHONPATH.split(SEPARATOR)
51-
assert.deepStrictEqual(dirs, [EXPECTED_PYPATH, existingPath])
55+
await new Promise((resolve, reject) => {
56+
prog.spawn = function () {
57+
try {
58+
assert.strictEqual(process.env.PYTHONPATH, [EXPECTED_PYPATH, existingPath].join(SEPARATOR))
5259

53-
return SPAWN_RESULT(done)
54-
}
55-
prog.devDir = devDir
56-
configure(prog, [], assert.fail)
60+
const dirs = process.env.PYTHONPATH.split(SEPARATOR)
61+
assert.deepStrictEqual(dirs, [EXPECTED_PYPATH, existingPath])
62+
63+
return SPAWN_RESULT(resolve)
64+
} catch (err) {
65+
reject(err)
66+
}
67+
}
68+
prog.devDir = devDir
69+
configure(prog, [], reject)
70+
})
5771
})
5872

59-
it('configure PYTHONPATH with existing env of multiple dirs', function (done) {
73+
it('configure PYTHONPATH with existing env of multiple dirs', async function () {
6074
const pythonDir1 = path.join('a', 'b')
6175
const pythonDir2 = path.join('b', 'c')
6276
const existingPath = [pythonDir1, pythonDir2].join(SEPARATOR)
6377
process.env.PYTHONPATH = existingPath
6478

6579
const prog = gyp()
6680
prog.parseArgv([])
67-
prog.spawn = function () {
68-
assert.strictEqual(process.env.PYTHONPATH, [EXPECTED_PYPATH, existingPath].join(SEPARATOR))
6981

70-
const dirs = process.env.PYTHONPATH.split(SEPARATOR)
71-
assert.deepStrictEqual(dirs, [EXPECTED_PYPATH, pythonDir1, pythonDir2])
82+
await new Promise((resolve, reject) => {
83+
prog.spawn = function () {
84+
try {
85+
assert.strictEqual(process.env.PYTHONPATH, [EXPECTED_PYPATH, existingPath].join(SEPARATOR))
7286

73-
return SPAWN_RESULT(done)
74-
}
75-
prog.devDir = devDir
76-
configure(prog, [], assert.fail)
87+
const dirs = process.env.PYTHONPATH.split(SEPARATOR)
88+
assert.deepStrictEqual(dirs, [EXPECTED_PYPATH, pythonDir1, pythonDir2])
89+
90+
return SPAWN_RESULT(resolve)
91+
} catch (err) {
92+
reject(err)
93+
}
94+
}
95+
prog.devDir = devDir
96+
configure(prog, [], reject)
97+
})
7798
})
7899
})

test/test-create-config-gypi.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22

33
const path = require('path')
4-
const { describe, it } = require('mocha')
4+
const { describe, it } = require('node:test')
55
const assert = require('assert')
66
const gyp = require('../lib/node-gyp')
77
const { parseConfigGypi, getCurrentConfigGypi } = require('../lib/create-config-gypi')

test/test-download.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict'
22

3-
const { describe, it, after } = require('mocha')
3+
const { describe, it, after } = require('node:test')
44
const assert = require('assert')
55
const fs = require('fs/promises')
66
const path = require('path')
@@ -155,13 +155,11 @@ describe('download', function () {
155155

156156
// only run this test if we are running a version of Node with predictable version path behavior
157157

158-
it('download headers (actual)', async function () {
158+
it('download headers (actual)', { timeout: platformTimeout(1, { win32: 5 }) }, async function (t) {
159159
if (!FULL_TEST) {
160-
return this.skip('Skipping actual download of headers due to test environment configuration')
160+
return t.skip('Skipping actual download of headers due to test environment configuration')
161161
}
162162

163-
this.timeout(platformTimeout(1, { win32: 5 }))
164-
165163
const expectedDir = path.join(devDir, process.version.replace(/^v/, ''))
166164
await fs.rm(expectedDir, { recursive: true, force: true, maxRetries: 3 })
167165

test/test-find-accessible-sync.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict'
22

3-
const { describe, it } = require('mocha')
3+
const { describe, it } = require('node:test')
44
const assert = require('assert')
55
const path = require('path')
66
const requireInject = require('require-inject')

test/test-find-node-directory.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict'
22

3-
const { describe, it } = require('mocha')
3+
const { describe, it } = require('node:test')
44
const assert = require('assert')
55
const path = require('path')
66
const findNodeDirectory = require('../lib/find-node-directory')

test/test-find-python.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
delete process.env.PYTHON
44

5-
const { describe, it, after } = require('mocha')
5+
const { describe, it, after } = require('node:test')
66
const assert = require('assert')
77
const PythonFinder = require('../lib/find-python')
88
const { execFile } = require('../lib/util')

test/test-find-visualstudio.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict'
22

3-
const { describe, it } = require('mocha')
3+
const { describe, it, before } = require('node:test')
44
const assert = require('assert')
55
const fs = require('fs')
66
const path = require('path')
@@ -22,7 +22,7 @@ class TestVisualStudioFinder extends VisualStudioFinder {
2222
}
2323

2424
describe('find-visualstudio', function () {
25-
this.beforeAll(function () {
25+
before(function () {
2626
// Condition to skip the test suite
2727
if (process.env.SystemRoot === undefined) {
2828
process.env.SystemRoot = '/'

0 commit comments

Comments
 (0)