Skip to content

Commit

Permalink
fix watch mode (#482)
Browse files Browse the repository at this point in the history
  • Loading branch information
fatso83 authored Oct 5, 2023
1 parent 9b2b474 commit cfb257f
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 7 deletions.
17 changes: 17 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,23 @@ permissions:
contents: read

jobs:
check-tests-are-watchable:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: "lts/*"
cache: npm
- name: Install dependencies
run: |
npm ci
env:
PUPPETEER_SKIP_CHROMIUM_DOWNLOAD: 1
- name: Tests that tests are watchable
run: |
./scripts/test-watch-mode-works.sh
prettier:
runs-on: ubuntu-latest
steps:
Expand Down
28 changes: 28 additions & 0 deletions scripts/test-watch-mode-works.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/bin/sh

out=$(mktemp)
# make silent background
npx mocha --watch 2>>$out.err 1>>$out &

PID=$!
#echo pid of mocha: $PID

echo "Round 1" >> out
touch src/fake-timers-src.js
sleep 3
echo "Round 2" >> out
touch src/fake-timers-src.js
sleep 2
echo "Cleanup" >> out

# Kill the process manually
kill $PID >/dev/null
kill -9 $PID >/dev/null

#cat $out.err
if grep failing $out; then
echo "This means some tests do not clean up after themselves or otherwise do not work in Mocha's watch mode"
echo "See $out for details or run $0 yourself"
exit 1
fi
exit 0
41 changes: 34 additions & 7 deletions test/fake-timers-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@ const {
utilPromisifyAvailable,
} = require("./helpers/setup-tests");

var timersModule;
let timersModule;

/* eslint-disable no-underscore-dangle */
globalObject.__runs = globalObject.__runs || 0;

const isRunningInWatchMode = ++globalObject.__runs > 1;
/* eslint-enable no-underscore-dangle */

if (typeof require === "function" && typeof module === "object") {
try {
Expand Down Expand Up @@ -3582,6 +3588,9 @@ describe("FakeTimers", function () {
return this.skip();
}

const backupDescriptors =
Object.getOwnPropertyDescriptors(Performance);

function noop() {
return ["foo"];
}
Expand Down Expand Up @@ -3612,9 +3621,13 @@ describe("FakeTimers", function () {
assert.equals(performance.getEntriesByName(), ["foo"]);
assert.equals(performance.getEntriesByType(), ["foo"]);

delete Performance.prototype.getEntries;
delete Performance.prototype.getEntriesByName;
delete Performance.prototype.getEntriesByTime;
Object.keys(backupDescriptors).forEach((key) => {
Object.defineProperty(
Performance.prototype,
key,
backupDescriptors[key]
);
});
});
}

Expand Down Expand Up @@ -3803,7 +3816,7 @@ describe("FakeTimers", function () {
const origSetInterval = globalObject.setInterval;
const origClearInterval = globalObject.clearInterval;

var clock = FakeTimers.install({
const clock = FakeTimers.install({
shouldAdvanceTime: true,
toFake: ["setTimeout"],
});
Expand All @@ -3830,16 +3843,23 @@ describe("FakeTimers", function () {
}

afterEach(function () {
if (this.clock) {
if (this.clock?.uninstall) {
this.clock.uninstall();
}
sinon.restore();
});

it("outputs a warning once if not enabled", function (done) {
// This test does not work well in watch mode, as Chokidar sets up timers
// that trips up this test
if (isRunningInWatchMode) {
this.skip();
}

const timer = globalObject.setTimeout(createCallback(done, true));
const stub = sinon.stub(globalObject.console, "warn");

this.clock = FakeTimers.install();

globalObject.clearTimeout(timer);
globalObject.clearTimeout(timer);
assert.equals(stub.callCount, 1);
Expand Down Expand Up @@ -4826,6 +4846,7 @@ describe("loop limit stack trace", function () {
function recursiveQueueMicroTask() {
test.clock.queueMicrotask(recursiveQueueMicroTask);
}

recursiveQueueMicroTask();
});

Expand Down Expand Up @@ -4853,6 +4874,7 @@ describe("loop limit stack trace", function () {
function recursiveQueueMicroTask() {
test.clock.nextTick(recursiveQueueMicroTask);
}

recursiveQueueMicroTask();
});

Expand Down Expand Up @@ -4882,6 +4904,7 @@ describe("loop limit stack trace", function () {
recursiveCreateTimer();
}, 10);
}

recursiveCreateTimer();
});

Expand Down Expand Up @@ -4933,6 +4956,7 @@ describe("loop limit stack trace", function () {
10
);
}

recursiveCreateTimer();
});

Expand Down Expand Up @@ -4981,6 +5005,7 @@ describe("loop limit stack trace", function () {
recursiveCreateTimer();
}, 10);
}

recursiveCreateTimer();
});

Expand Down Expand Up @@ -5035,6 +5060,7 @@ describe("loop limit stack trace", function () {
recursiveCreateTimer();
});
}

recursiveCreateTimer();
});

Expand Down Expand Up @@ -5085,6 +5111,7 @@ describe("loop limit stack trace", function () {
}
);
}

recursiveCreateTimer();
});

Expand Down

0 comments on commit cfb257f

Please sign in to comment.