From eb2a967f31ebcc4c1ce688ccf13be66f53e609bc Mon Sep 17 00:00:00 2001 From: Steve Gravrock Date: Wed, 13 Nov 2024 08:10:01 -0800 Subject: [PATCH] Move async higher up in Your First Suite tutorial --- _tutorials/src/your_first_suite.js | 135 +++++++++++---------- _tutorials/your_first_suite.md | 189 +++++++++++++++-------------- 2 files changed, 163 insertions(+), 161 deletions(-) diff --git a/_tutorials/src/your_first_suite.js b/_tutorials/src/your_first_suite.js index ebc5f41..bab6048 100644 --- a/_tutorials/src/your_first_suite.js +++ b/_tutorials/src/your_first_suite.js @@ -272,6 +272,74 @@ describe("A suite with some shared setup", function() { will run. */ + +/** + ## Asynchronous Support + + Jasmine also has support for running specs that require testing + asynchronous operations. The functions that you pass to `beforeAll`, + `afterAll`, `beforeEach`, `afterEach`, and `it` can be declared async. + + Jasmine also supports asynchronous functions that explicitly return + promises or that take a callback. See the + [Asynchronous Work tutorial](/tutorials/async) for more information. + */ +describe("Using async/await", function () { + beforeEach(async function () { + await soon(); + value = 0; + }); + + /** + This spec will not start until the promise returned from the call to + `beforeEach` above is settled. And this spec will not complete until + the promise that it returns is settled. + */ + it("supports async execution of test preparation and expectations", + async function () { + await soon(); + value++; + expect(value).toBeGreaterThan(0); + } + ); + + function soon() { + return new Promise(function(resolve, reject) { + setTimeout(function() { + resolve(); + }, 1); + }); + } +}); + + +/** + By default jasmine will wait for 5 seconds for an asynchronous spec to + finish before causing a timeout failure. If the timeout expires before + `done` is called, the current spec will be marked as failed and suite + execution will continue as if `done` was called. + + If specific specs should fail faster or need more time this can be + adjusted by passing a timeout value to `it`, etc. + + If the entire suite should have a different timeout, + `jasmine.DEFAULT_TIMEOUT_INTERVAL` can be set globally, outside of any + given describe. + */ +describe("long asynchronous specs", function() { + beforeEach(async function() { + await somethingSlow(); + }, 1000); + + it("takes a long time", async function() { + await somethingReallySlow(); + }, 10000); + + afterEach(async function() { + await somethingSlow(); + }, 1000); +}); + /** ## Spies @@ -658,73 +726,6 @@ describe("Matching with finesse", function() { }); }); }); - - /** - ## Asynchronous Support - - Jasmine also has support for running specs that require testing - asynchronous operations. The functions that you pass to `beforeAll`, - `afterAll`, `beforeEach`, `afterEach`, and `it` can be declared async. - - Jasmine also supports asynchronous functions that explicitly return - promises or that take a callback. See the - [Asynchronous Work tutorial](/tutorials/async) for more information. - */ - describe("Using async/await", function () { - beforeEach(async function () { - await soon(); - value = 0; - }); - - /** - This spec will not start until the promise returned from the call to - `beforeEach` above is settled. And this spec will not complete until - the promise that it returns is settled. - */ - it("supports async execution of test preparation and expectations", - async function () { - await soon(); - value++; - expect(value).toBeGreaterThan(0); - } - ); - }); - - - /** - By default jasmine will wait for 5 seconds for an asynchronous spec to - finish before causing a timeout failure. If the timeout expires before - `done` is called, the current spec will be marked as failed and suite - execution will continue as if `done` was called. - - If specific specs should fail faster or need more time this can be - adjusted by passing a timeout value to `it`, etc. - - If the entire suite should have a different timeout, - `jasmine.DEFAULT_TIMEOUT_INTERVAL` can be set globally, outside of any - given describe. - */ - describe("long asynchronous specs", function() { - beforeEach(async function() { - await somethingSlow(); - }, 1000); - - it("takes a long time", async function() { - await somethingReallySlow(); - }, 10000); - - afterEach(async function() { - await somethingSlow(); - }, 1000); - }); - - function soon() { - return new Promise(function(resolve, reject) { - setTimeout(function() { - resolve(); - }, 1); - }); - } }); diff --git a/_tutorials/your_first_suite.md b/_tutorials/your_first_suite.md index 7f05855..c45fec4 100644 --- a/_tutorials/your_first_suite.md +++ b/_tutorials/your_first_suite.md @@ -482,7 +482,101 @@ will run.

- {% highlight javascript %} {% endhighlight %} + {% highlight javascript %} + {% endhighlight %} + + + + +
+ +
+
+

Asynchronous Support

+

Jasmine also has support for running specs that require testing +asynchronous operations. The functions that you pass to beforeAll, +afterAll, beforeEach, afterEach, and it can be declared async.

+

Jasmine also supports asynchronous functions that explicitly return +promises or that take a callback. See the +Asynchronous Work tutorial for more information.

+ +
+ + + {% highlight javascript %}describe("Using async/await", function () { + beforeEach(async function () { + await soon(); + value = 0; + }); + {% endhighlight %} + + + + +
+ +
+
+

This spec will not start until the promise returned from the call to +beforeEach above is settled. And this spec will not complete until +the promise that it returns is settled.

+ +
+ + + {% highlight javascript %} it("supports async execution of test preparation and expectations", + async function () { + await soon(); + value++; + expect(value).toBeGreaterThan(0); + } + ); + + function soon() { + return new Promise(function(resolve, reject) { + setTimeout(function() { + resolve(); + }, 1); + }); + } +}); + + {% endhighlight %} + + + + +
+ +
+
+

By default jasmine will wait for 5 seconds for an asynchronous spec to +finish before causing a timeout failure. If the timeout expires before +done is called, the current spec will be marked as failed and suite +execution will continue as if done was called.

+

If specific specs should fail faster or need more time this can be +adjusted by passing a timeout value to it, etc.

+

If the entire suite should have a different timeout, +jasmine.DEFAULT_TIMEOUT_INTERVAL can be set globally, outside of any +given describe.

+ +
+ + + {% highlight javascript %}describe("long asynchronous specs", function() { + beforeEach(async function() { + await somethingSlow(); + }, 1000); + + it("takes a long time", async function() { + await somethingReallySlow(); + }, 10000); + + afterEach(async function() { + await somethingSlow(); + }, 1000); +}); + {% endhighlight %} @@ -1070,99 +1164,6 @@ the current date.

}); }); }); - {% endhighlight %} - - - - -
- -
-
-

Asynchronous Support

-

Jasmine also has support for running specs that require testing -asynchronous operations. The functions that you pass to beforeAll, -afterAll, beforeEach, afterEach, and it can be declared async.

-

Jasmine also supports asynchronous functions that explicitly return -promises or that take a callback. See the -Asynchronous Work tutorial for more information.

- -
- - - {% highlight javascript %} describe("Using async/await", function () { - beforeEach(async function () { - await soon(); - value = 0; - }); - {% endhighlight %} - - - - -
- -
-
-

This spec will not start until the promise returned from the call to -beforeEach above is settled. And this spec will not complete until -the promise that it returns is settled.

- -
- - - {% highlight javascript %} it("supports async execution of test preparation and expectations", - async function () { - await soon(); - value++; - expect(value).toBeGreaterThan(0); - } - ); - }); - - {% endhighlight %} - - - - -
- -
-
-

By default jasmine will wait for 5 seconds for an asynchronous spec to -finish before causing a timeout failure. If the timeout expires before -done is called, the current spec will be marked as failed and suite -execution will continue as if done was called.

-

If specific specs should fail faster or need more time this can be -adjusted by passing a timeout value to it, etc.

-

If the entire suite should have a different timeout, -jasmine.DEFAULT_TIMEOUT_INTERVAL can be set globally, outside of any -given describe.

- -
- - - {% highlight javascript %} describe("long asynchronous specs", function() { - beforeEach(async function() { - await somethingSlow(); - }, 1000); - - it("takes a long time", async function() { - await somethingReallySlow(); - }, 10000); - - afterEach(async function() { - await somethingSlow(); - }, 1000); - }); - - function soon() { - return new Promise(function(resolve, reject) { - setTimeout(function() { - resolve(); - }, 1); - }); - } });