Skip to content

feat(test): adopt node:test #3159

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ jobs:
mkdir -p $NODE_GYP_TEMP_DIR
npm pack
tar xzf *.tgz -C $NODE_GYP_TEMP_DIR --strip-components=1
cp -r test/ $NODE_GYP_TEMP_DIR/test/
cp -r tests/ $NODE_GYP_TEMP_DIR/tests/
echo "dir=$NODE_GYP_TEMP_DIR" >> "$GITHUB_OUTPUT"
- name: Test
working-directory: ${{ steps.pack.outputs.dir }}
Expand Down
4 changes: 2 additions & 2 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
/gyp/test
/gyp/tools/
/node-gyp-*.tgz
/test/
/test/.node-gyp
/tests/
/tests/.node-gyp
/update-gyp.py
node-gyp-*.tgz
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,12 @@
"bindings": "^1.5.0",
"cross-env": "^7.0.3",
"eslint": "^9.16.0",
"mocha": "^11.0.1",
"nan": "^2.14.2",
"neostandard": "^0.11.9",
"require-inject": "^1.4.4"
},
"scripts": {
"lint": "eslint \"*/*.js\" \"test/**/*.js\" \".github/**/*.js\"",
"test": "cross-env NODE_GYP_NULL_LOGGER=true mocha --timeout 15000 test/test-download.js test/test-*"
"lint": "eslint \"*/*.js\" \"tests/**/*.js\" \".github/**/*.js\"",
"test": "cross-env NODE_GYP_NULL_LOGGER=true node --test"
}
}
33 changes: 0 additions & 33 deletions test/common.js

This file was deleted.

27 changes: 0 additions & 27 deletions test/simple-proxy.js

This file was deleted.

78 changes: 0 additions & 78 deletions test/test-configure-python.js

This file was deleted.

2 changes: 2 additions & 0 deletions tests/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
!node_modules
node_modules/hello_world/build
21 changes: 21 additions & 0 deletions tests/common.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const { cache } = require('env-paths')('node-gyp', { suffix: '' })
const semver = require('semver')

module.exports = {
devDir: cache,

poison (object, property) {
const fail = () => {
console.error(new Error(`Property ${property} should not have been accessed.`))
process.abort()
}
Object.defineProperty(object, property, { configurable: false, enumerable: false, get: fail, set: fail })
},

FULL_TEST: process.env.FULL_TEST === '1' && process.release.name === 'node' && !semver.prerelease(process.version),

platformTimeout (def, obj) {
const platformTimeout = obj[process.platform]
return platformTimeout ? platformTimeout * 60 * 1000 : def * 60 * 1000
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
18 changes: 7 additions & 11 deletions test/test-addon.js → tests/test-addon.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const { describe, it } = require('mocha')
const { describe, it } = require('node:test')
const assert = require('assert')
const path = require('path')
const fs = require('graceful-fs')
Expand Down Expand Up @@ -42,9 +42,9 @@ function checkCharmapValid () {
}

describe('addon', function () {
it('build simple addon', async function () {
this.timeout(platformTimeout(1, { win32: 5 }))
const timeout = platformTimeout(1, { win32: 5 })

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

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

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

this.timeout(platformTimeout(1, { win32: 5 }))

let data
const configPath = path.join(addonPath, 'build', 'config.gypi')
try {
Expand Down Expand Up @@ -115,9 +113,7 @@ describe('addon', function () {
assert.strictEqual(runHello(), 'world')
})

it('addon works with renamed host executable', async function () {
this.timeout(platformTimeout(1, { win32: 5 }))

it('addon works with renamed host executable', { timeout }, async function () {
const notNodePath = path.join(os.tmpdir(), 'notnode' + path.extname(process.execPath))
fs.copyFileSync(process.execPath, notNodePath)

Expand Down
86 changes: 46 additions & 40 deletions test/test-configure-nodedir.js → tests/test-configure-nodedir.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict'

const { describe, it } = require('mocha')
const assert = require('assert')
const { describe, it } = require('node:test')
const path = require('path')
const os = require('os')
const gyp = require('../lib/node-gyp')
Expand Down Expand Up @@ -46,8 +45,7 @@ const SPAWN_RESULT = cb => ({ on: function () { cb() } })

const driveLetter = os.platform() === 'win32' ? `${process.cwd().split(path.sep)[0]}` : ''
function checkTargetPath (target, value) {
let targetPath = path.join(path.sep, target, 'include',
'node', 'common.gypi')
let targetPath = path.join(path.sep, target, 'include', 'node', 'common.gypi')
if (process.platform === 'win32') {
targetPath = driveLetter + targetPath
}
Expand All @@ -56,68 +54,76 @@ function checkTargetPath (target, value) {
}

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

prog.spawn = function (program, args) {
for (let i = 0; i < args.length; i++) {
if (checkTargetPath('usr', args[i])) {
return SPAWN_RESULT(done)
await new Promise((resolve, reject) => {
prog.spawn = function (program, args) {
for (let i = 0; i < args.length; i++) {
if (checkTargetPath('usr', args[i])) {
return SPAWN_RESULT(resolve)
}
}
};
assert.fail()
}
configure(prog, [], assert.fail)
reject(new Error('Expected nodedir path not found'))
}
configure(prog, [], reject)
})
})

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

prog.spawn = function (program, args) {
for (let i = 0; i < args.length; i++) {
await new Promise((resolve, reject) => {
prog.spawn = function (program, args) {
const nodedir = process.config.variables.node_prefix
if (checkTargetPath(nodedir, args[i])) {
return SPAWN_RESULT(done)
for (let i = 0; i < args.length; i++) {
if (checkTargetPath(nodedir, args[i])) {
return SPAWN_RESULT(resolve)
}
}
};
assert.fail()
}
configure(prog, [], assert.fail)
reject(new Error('Expected nodedir path not found'))
}
configure(prog, [], reject)
})
})

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

prog.spawn = function (program, args) {
for (let i = 0; i < args.length; i++) {
await new Promise((resolve, reject) => {
prog.spawn = function (program, args) {
const nodedir = process.config.variables.node_prefix
if (checkTargetPath(nodedir, args[i])) {
assert.fail()
for (let i = 0; i < args.length; i++) {
if (checkTargetPath(nodedir, args[i])) {
return reject(new Error('Unexpected match found'))
}
}
};
return SPAWN_RESULT(done)
}
configure2(prog, [], assert.fail)
return SPAWN_RESULT(resolve)
}
configure2(prog, [], reject)
})
})

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

prog.spawn = function (program, args) {
for (let i = 0; i < args.length; i++) {
await new Promise((resolve, reject) => {
prog.spawn = function (program, args) {
const nodedir = process.config.variables.node_prefix
if (checkTargetPath(nodedir, args[i])) {
assert.fail()
for (let i = 0; i < args.length; i++) {
if (checkTargetPath(nodedir, args[i])) {
return reject(new Error('Unexpected match found for target'))
}
}
};
return SPAWN_RESULT(done)
}
configure(prog, [], assert.fail)
return SPAWN_RESULT(resolve)
}
configure(prog, [], reject)
})
})
}
})
Loading
Loading