Lines Matching defs:this
124 this.#test = test;
128 return this.#test.signal;
132 return this.#test.name;
136 this.#test.diagnostic(message);
140 this.#test.mock ??= new MockTracker();
141 return this.#test.mock;
145 this.#test.runOnlySubtests = !!value;
149 this.#test.skip(message);
153 this.#test.todo(message);
162 const subtest = this.#test.createSubtest(
171 this.#test.createHook('before', fn, options);
175 this.#test.createHook('after', fn, options);
179 this.#test.createHook('beforeEach', fn, options);
183 this.#test.createHook('afterEach', fn, options);
191 this.#suite = suite;
195 return this.#suite.signal;
199 return this.#suite.name;
227 this.concurrency = 1;
228 this.nesting = 0;
229 this.only = testOnlyFlag;
230 this.reporter = new TestsStream();
231 this.runOnlySubtests = this.only;
232 this.testNumber = 0;
233 this.timeout = kDefaultTimeout;
234 this.root = this;
235 this.hooks = {
246 this.concurrency = parent.concurrency;
247 this.nesting = nesting;
248 this.only = only ?? !parent.runOnlySubtests;
249 this.reporter = parent.reporter;
250 this.runOnlySubtests = !this.only;
251 this.testNumber = parent.subtests.length + 1;
252 this.timeout = parent.timeout;
253 this.root = parent.root;
254 this.hooks = {
266 this.concurrency = concurrency;
271 this.concurrency = parent === null ?
274 this.concurrency = 1;
285 this.timeout = timeout;
288 this.name = name;
289 this.parent = parent;
291 if (testNamePatterns !== null && !this.matchesTestNamePatterns()) {
295 if (testOnlyFlag && !this.only) {
303 this.abortController = new AbortController();
304 this.outerSignal = signal;
305 this.signal = this.abortController.signal;
312 this.outerSignal?.addEventListener(
314 this.#abortHandler,
318 this.fn = fn;
319 this.harness = null; // Configured on the root test by the test harness.
320 this.mock = null;
321 this.cancelled = false;
322 this.skipped = skip !== undefined && skip !== false;
323 this.isTodo = todo !== undefined && todo !== false;
324 this.startTime = null;
325 this.endTime = null;
326 this.passed = false;
327 this.error = null;
328 this.diagnostics = [];
329 this.message = typeof skip === 'string' ? skip :
331 this.activeSubtests = 0;
332 this.pendingSubtests = [];
333 this.readySubtests = new SafeMap();
334 this.subtests = [];
335 this.waitingOn = 0;
336 this.finished = false;
338 if (!testOnlyFlag && (only || this.runOnlySubtests)) {
341 this.diagnostic(warning);
345 this.loc = undefined;
347 this.loc = {
357 return ArrayPrototypeSome(testNamePatterns, (re) => RegExpPrototypeExec(re, this.name) !== null) ||
358 this.parent?.matchesTestNamePatterns();
362 return this.concurrency > this.activeSubtests;
366 ArrayPrototypePush(this.pendingSubtests, deferred);
370 while (this.pendingSubtests.length > 0 && this.hasConcurrency()) {
371 const deferred = ArrayPrototypeShift(this.pendingSubtests);
373 this.reporter.dequeue(test.nesting, test.loc, test.name);
380 this.readySubtests.set(subtest.testNumber, subtest);
384 const start = this.waitingOn;
385 const end = start + this.readySubtests.size;
388 const subtest = this.readySubtests.get(i);
401 canSend = canSend || this.isClearToSend();
407 if (i === 1 && this.parent !== null) {
408 this.reportStarted();
413 this.readySubtests.delete(i);
431 let parent = this;
433 // If this test has already ended, attach this test to the root test so
435 const preventAddingSubtests = this.finished || this.buildPhaseFinished;
463 const error = this.outerSignal?.reason || new AbortError('The test was aborted');
465 this.#cancel(error);
469 if (this.endTime !== null) {
473 this.fail(error ||
479 this.startTime = this.startTime || this.endTime; // If a test was canceled before it was started, e.g inside a hook
480 this.cancelled = true;
481 this.abortController.abort();
491 ArrayPrototypePush(this.hooks[name], hook);
496 if (this.error !== null) {
500 this.endTime = hrtime();
501 this.passed = false;
502 this.error = err;
506 if (this.endTime !== null) {
510 this.endTime = hrtime();
511 this.passed = true;
515 this.skipped = true;
516 this.message = message;
520 this.isTodo = true;
521 this.message = message;
525 ArrayPrototypePush(this.diagnostics, message);
532 this.reporter.enqueue(this.nesting, this.loc, this.name);
533 if (!this.parent.hasConcurrency()) {
536 deferred.test = this;
537 this.parent.addPendingSubtest(deferred);
541 this.reporter.dequeue(this.nesting, this.loc, this.name);
542 return this.run();
546 if (this.signal.aborted) {
549 if (this.outerSignal?.aborted) {
550 this.#abortHandler();
556 const ctx = new TestContext(this);
563 await ArrayPrototypeReduce(this.hooks[hook], async (prev, hook) => {
578 if (this.parent !== null) {
579 this.parent.activeSubtests++;
581 this.startTime = hrtime();
583 if (this[kShouldAbort]()) {
584 this.postRun();
588 const { args, ctx } = this.getRunArgs();
590 if (this.hooks.after.length > 0) {
591 await this.runHook('after', { __proto__: null, args, ctx });
595 if (this.parent?.hooks.afterEach.length > 0) {
596 await this.parent.runHook('afterEach', { __proto__: null, args, ctx });
603 if (this.parent?.hooks.before.length > 0) {
604 await this.parent.runHook('before', this.parent.getRunArgs());
606 if (this.parent?.hooks.beforeEach.length > 0) {
607 await this.parent.runHook('beforeEach', { __proto__: null, args, ctx });
609 stopPromise = stopTest(this.timeout, this.signal);
611 ArrayPrototypeUnshift(runArgs, this.fn, ctx);
613 if (this.fn.length === runArgs.length - 1) {
618 const ret = ReflectApply(this.runInAsyncScope, this, runArgs);
621 this.fail(new ERR_TEST_FAILURE(
631 const promise = ReflectApply(this.runInAsyncScope, this, runArgs);
635 if (this[kShouldAbort]()) {
636 this.postRun();
642 this.pass();
648 this.#cancel(err);
650 this.fail(err);
653 this.fail(new ERR_TEST_FAILURE(err, kTestCodeFailure));
660 if (this.parent !== null) {
661 this.abortController.abort();
665 if (this.parent !== null || typeof this.hookType === 'string') {
673 this.postRun();
680 if (this.endTime < this.startTime) {
681 this.endTime = hrtime();
683 this.startTime ??= this.endTime;
686 // mark this test as failed if any subtests failed.
687 this.pendingSubtests = [];
689 for (let i = 0; i < this.subtests.length; i++) {
690 const subtest = this.subtests[i];
701 if ((this.passed || this.parent === null) && failed > 0) {
705 this.fail(new ERR_TEST_FAILURE(msg, kSubtestsFailed));
708 this.outerSignal?.removeEventListener('abort', this.#abortHandler);
709 this.mock?.reset();
711 if (this.parent !== null) {
712 this.parent.activeSubtests--;
713 this.parent.addReadySubtest(this);
714 this.parent.processReadySubtestRange(false);
715 this.parent.processPendingSubtests();
717 if (this.parent === this.root &&
718 this.root.activeSubtests === 0 &&
719 this.root.pendingSubtests.length === 0 &&
720 this.root.readySubtests.size === 0 &&
721 this.root.hooks.after.length > 0) {
722 // This is done so that any global after() hooks are run. At this point
726 this.root.run();
728 } else if (!this.reported) {
735 } = this;
737 this.reported = true;
740 // Call this harness.coverage() before collecting diagnostics, since failure to collect coverage is a diagnostic.
753 reporter.diagnostic(nesting, loc, `duration_ms ${this.duration()}`);
764 return this.parent === null ||
766 this.parent.waitingOn === this.testNumber && this.parent.isClearToSend()
771 // By the time this function is called, the following can be relied on:
773 // - All of this test's subtests have completed or been cancelled.
779 this.processReadySubtestRange(true);
781 // Output this test's results and update the parent's waiting counter.
782 this.report();
783 this.parent.waitingOn++;
784 this.finished = true;
789 return Number(this.endTime - this.startTime) / 1_000_000;
793 countCompletedTest(this);
794 if (this.subtests.length > 0) {
795 this.reporter.plan(this.subtests[0].nesting, this.loc, this.subtests.length);
797 this.reportStarted();
800 const details = { __proto__: null, duration_ms: this.duration() };
802 if (this.skipped) {
803 directive = this.reporter.getSkip(this.message);
804 } else if (this.isTodo) {
805 directive = this.reporter.getTodo(this.message);
808 if (this.reportedType) {
809 details.type = this.reportedType;
812 if (this.passed) {
813 this.reporter.ok(this.nesting, this.loc, this.testNumber, this.name, details, directive);
815 details.error = this.error;
816 this.reporter.fail(this.nesting, this.loc, this.testNumber, this.name, details, directive);
819 for (let i = 0; i < this.diagnostics.length; i++) {
820 this.reporter.diagnostic(this.nesting, this.loc, this.diagnostics[i]);
825 if (this.#reportedSubtest || this.parent === null) {
828 this.#reportedSubtest = true;
829 this.parent.reportStarted();
830 this.reporter.start(this.nesting, this.loc, this.name);
843 this.parentTest = options.parent ?? null;
844 this.hookType = options.hookType;
847 if (this.error && !this.outerSignal?.aborted) {
848 this.passed = false;
849 this.error = null;
850 this.abortController.abort();
851 this.abortController = new AbortController();
852 this.signal = this.abortController.signal;
855 this.#args = args;
859 return this.#args;
865 const { error, loc, parentTest: parent } = this;
869 parent === parent.root && this.hookType === 'after') {
877 duration_ms: this.duration(),
890 this.fn = options.fn || this.fn;
891 this.skipped = false;
893 this.runOnlySubtests = testOnlyFlag;
896 const { ctx, args } = this.getRunArgs();
897 const runArgs = [this.fn, ctx];
899 this.buildSuite = SafePromisePrototypeFinally(
901 PromiseResolve(ReflectApply(this.runInAsyncScope, this, runArgs)),
904 this.fail(new ERR_TEST_FAILURE(err, kTestCodeFailure));
907 this.buildPhaseFinished = true;
911 this.fail(new ERR_TEST_FAILURE(err, kTestCodeFailure));
913 this.buildPhaseFinished = true;
915 this.fn = () => {};
919 const ctx = new SuiteContext(this);
924 const hookArgs = this.getRunArgs();
928 this.parent.activeSubtests++;
929 await this.buildSuite;
930 this.startTime = hrtime();
932 if (this[kShouldAbort]()) {
933 this.subtests = [];
934 this.postRun();
938 if (this.parent.hooks.before.length > 0) {
939 await this.parent.runHook('before', this.parent.getRunArgs());
942 await this.runHook('before', hookArgs);
944 stopPromise = stopTest(this.timeout, this.signal);
945 const subtests = this.skipped || this.error ? [] : this.subtests;
949 await this.runHook('after', hookArgs);
951 this.pass();
954 this.fail(err);
956 this.fail(new ERR_TEST_FAILURE(err, kTestCodeFailure));
962 this.postRun();