Skip to content

Commit

Permalink
Test: Prep require-qunit.js test for ESM package
Browse files Browse the repository at this point in the history
Extracted from #1798 to minimize
the diff.
  • Loading branch information
Krinkle committed Aug 28, 2024
1 parent efae08d commit f5a7160
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/cli/require-qunit.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ module.exports = function requireQUnit (resolve = require.resolve) {
// If you get "Error: Cannot find module ../../qunit/qunit", it probably
// means you haven't yet run "npm run build".
//
delete require.cache[resolve('../../qunit/qunit')];
return require('../../qunit/qunit');
delete require.cache[resolve('../../qunit/qunit.js')];
return require('../../qunit/qunit.js');
}

throw e;
Expand Down

This file was deleted.

48 changes: 38 additions & 10 deletions test/cli/require-qunit-test.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,69 @@
const proxyquire = require('proxyquire').noCallThru();
const fs = require('fs');
const os = require('os');
const path = require('path');
const resolveStub = (path) => {

const proxyquire = require('proxyquire').noCallThru();

function resolveStub (path) {
return path;
};
}

QUnit.module('requireQUnit', function (hooks) {
const cwd = process.cwd();
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'qunit-test-'));

hooks.before(() => {
process.chdir(cwd);
});
hooks.afterEach(() => {
process.chdir(cwd);
fs.rmSync(tmpDir, { force: true, recursive: true });
});

QUnit.test('finds QUnit package in the current working directory', function (assert) {
const requireQUnit = require('../../src/cli/require-qunit');
process.chdir(path.join(__dirname, './fixtures/require-from-cwd'));
QUnit.test('find local qunit in the current working directory', function (assert) {
// We can't use a fixture inside the project directory (e.g. /test/cli/fixtures)
// because Node.js will always find 'qunit' from inside our own package, no
// matter what paths you pass to require.resolve() or even Module.createRequire(),
// it will always look for LOAD_PACKAGE_SELF first [1], and return from there if
// it has "exports" set in package.json.
//
// [1]: https://nodejs.org/docs/latest-v18.x/api/modules.html#all-together from inside the a inside a project that uses "exports" in package.json
//
// create:
// - tmpDir/
// - tmpDir/node_modules/
// - tmpDir/node_modules/qunit/
// - tmpDir/node_modules/qunit/index.js
fs.mkdirSync(
path.join(tmpDir, 'node_modules', 'qunit'),
{ recursive: true }
);
fs.writeFileSync(
path.join(tmpDir, 'node_modules', 'qunit', 'index.js'),
'module.exports = { version: "from-cwd" };'
);
const requireQUnit = require('../../src/cli/require-qunit.js');

process.chdir(tmpDir);

assert.strictEqual(requireQUnit().version, 'from-cwd');
});

// For development mode invoked locally,
// or for global install without local dependency installed.
QUnit.test('finds relative self', function (assert) {
QUnit.test('find relative self', function (assert) {
const fakeQUnit = {
version: 'from-fake'
};
const requireQUnit = proxyquire('../../src/cli/require-qunit', {
const requireQUnit = proxyquire('../../src/cli/require-qunit.js', {
qunit: null,
'../../qunit/qunit': fakeQUnit
});
assert.strictEqual(requireQUnit(resolveStub).version, 'from-fake');
});

QUnit.test('throws error if none of the modules are found', function (assert) {
const requireQUnit = proxyquire('../../src/cli/require-qunit', {
QUnit.test('throw if none found', function (assert) {
const requireQUnit = proxyquire('../../src/cli/require-qunit.js', {
qunit: null,
'../../qunit/qunit': null
});
Expand Down

0 comments on commit f5a7160

Please sign in to comment.