Skip to content

Commit

Permalink
test: fix up async bug in test setup
Browse files Browse the repository at this point in the history
The dynamic import causes the `describe` blocks to happen later, which
means the `after` and `afterEach` have already run (and cleaned up) at
that point.

It also turns out the old version of rimraf wasn't awaitable, so it has
been upgraded in this change.
  • Loading branch information
43081j committed Feb 5, 2024
1 parent 22a5ec9 commit 585d968
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"eslint": "^8.7.0",
"mocha": "^9.1.4",
"nyc": "15.1.0",
"rimraf": "3.0.2",
"rimraf": "^5.0.5",
"sinon": "12.0.1",
"sinon-chai": "3.7.0",
"typescript": "~4.5.3",
Expand Down
35 changes: 21 additions & 14 deletions test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@

import fs from 'node:fs';
import sysPath from 'node:path';
import {fileURLToPath} from 'node:url';
import {promisify} from 'node:util';
import childProcess from 'node:child_process';
import chai from 'chai';
import rimraf from 'rimraf';
import {rimraf} from 'rimraf';
import sinon from 'sinon';
import sinonChai from 'sinon-chai';
import upath from 'upath';
Expand All @@ -18,9 +19,9 @@ import { isWindows, isMacos, isIBMi } from './lib/constants.js';

import { URL } from 'url'; // in Browser, the URL in native accessible on window

const __filename = new URL('', import.meta.url).pathname;
const __filename = fileURLToPath(new URL('', import.meta.url));
// Will contain trailing slash
const __dirname = new URL('.', import.meta.url).pathname;
const __dirname = fileURLToPath(new URL('.', import.meta.url));

const {expect} = chai;
chai.use(sinonChai);
Expand All @@ -33,7 +34,6 @@ const fs_rename = promisify(fs.rename);
const fs_mkdir = promisify(fs.mkdir);
const fs_rmdir = promisify(fs.rmdir);
const fs_unlink = promisify(fs.unlink);
const pRimraf = promisify(rimraf);

const FIXTURES_PATH_REL = 'test-fixtures';
const FIXTURES_PATH = sysPath.join(__dirname, FIXTURES_PATH_REL);
Expand Down Expand Up @@ -407,7 +407,7 @@ const runTests = (baseopts) => {
fs.mkdirSync(testDir3, PERM_ARR);
const spy = await aspy(watcher, EV.UNLINK_DIR);
await waitFor([spy]);
await pRimraf(testDir2); // test removing in one
await rimraf(testDir2); // test removing in one
await waitFor([spy]);
spy.should.have.been.calledWith(testDir2);
spy.should.have.been.calledWith(testDir3);
Expand Down Expand Up @@ -2021,12 +2021,14 @@ const runTests = (baseopts) => {
it('should not prevent the process from exiting', async () => {
const scriptFile = getFixturePath('script.js');
const scriptContent = `
const chokidar = require("${__dirname.replace(/\\/g, '\\\\')}");
(async () => {
const chokidar = await import("${__dirname.replace(/\\/g, '\\\\')}");
const watcher = chokidar.watch("${scriptFile.replace(/\\/g, '\\\\')}");
watcher.on("ready", () => {
watcher.close();
process.stdout.write("closed");
});`;
});
})();`;
await write(scriptFile, scriptContent);
const obj = await exec(`node ${scriptFile}`);
const {stdout} = obj;
Expand All @@ -2043,8 +2045,15 @@ const runTests = (baseopts) => {
};

describe('chokidar', async () => {
let canUseFsEvents = undefined;

before(async () => {
await pRimraf(FIXTURES_PATH);
if (isMacos) {
const {default: FSEventsHandler} = await import('./lib/fsevents-handler.js')
canUseFsEvents = FSEventsHandler.canUse();
}

await rimraf(FIXTURES_PATH);
const _content = fs.readFileSync(__filename, 'utf-8');
const _only = _content.match(/\sit\.only\(/g);
const itCount = _only && _only.length || _content.match(/\sit\(/g).length;
Expand All @@ -2058,8 +2067,9 @@ describe('chokidar', async () => {
}
subdirId = 0;
});

after(async () => {
await pRimraf(FIXTURES_PATH);
await rimraf(FIXTURES_PATH);
});

beforeEach(() => {
Expand All @@ -2079,11 +2089,8 @@ describe('chokidar', async () => {
chokidar.watch.should.be.a('function');
});

if (isMacos) {
const FsEventsHandler = await import('./lib/fsevents-handler.js')
if (FsEventsHandler.default.canUse()) {
describe('fsevents (native extension)', runTests.bind(this, {useFsEvents: true}));
}
if (canUseFsEvents) {
describe('fsevents (native extension)', runTests.bind(this, {useFsEvents: true}));
}
if (!isIBMi) {
describe('fs.watch (non-polling)', runTests.bind(this, {usePolling: false, useFsEvents: false}));
Expand Down

0 comments on commit 585d968

Please sign in to comment.